You need to check the return code of fork().  Here's a sample Click event that 
forks to do its calculating:

sub Button1_Click {
  my $pid = fork(); die "Fork failed: $!" if not defined $pid;
  if ($pid == 0) {
    do_stuff();
    exit;
  }
}

Now, whenever the button is clicked, a child process is created that runs the 
do_stuff() code and then exits.  The child process will be able to talk to all 
the widgets that existed when fork() was called.

An important thing to note is that do_stuff() could be running more than once 
if you let Button1_Click get fired again before it finishes.  The safest way to 
code against that is to check if it's running before you fork, and if it's 
still running, don't call fork().  I'll leave it up to you to best decide how 
to know if your event is running.

Disclaimer: The above code has not been directly tested.

Hope that helps.

Trevor S Garside
[EMAIL PROTECTED]


> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED]
> Sent: Tuesday, April 01, 2003 2:01 PM
> To: Glenn Linderman
> Cc: perl-win32-gui-users@lists.sourceforge.net;
> [EMAIL PROTECTED]
> Subject: Re: [perl-win32-gui-users] Unresponsive Window
> 
> 
> 
> I'm still confused about how to use fork to keep the window 
> from locking
> up.  When the script makes the connection to the server it sits their
> waiting for a response.  Which means you can't do anything to 
> the window,
> move it, minimize it, resize it, go to another window (word, 
> ie, etc..),
> until you get a reply back from the server.  Then it writes 
> the reply to
> the richedit field and calls "Update" and "DoEvents".  With 
> fork I should
> be able to create the socket connection as a child process.  The child
> process sit their waiting for the replies from the server.  
> When the server
> does reply it writes that reply to the richedit field and 
> calls "Update"
> and "DoEvents".  But while the child process is doing all 
> this I should
> still be able to "play" with the window (move it, minimize 
> it, resize it).
> My problem and it's because I'm just learning fork(), where 
> in the code do
> you define the the child process, where do you call it, etc...
> 
> Sorry for being idiotic about all this, but I truly am confused.
> 
> 
> 
>                                                               
>                                                               
>                          
>                     Glenn Linderman                           
>                                                               
>                          
>                     <[EMAIL PROTECTED]>             
>    To:     [EMAIL PROTECTED]               
>                          
>                     Sent by:                                  
>    cc:     perl-win32-gui-users@lists.sourceforge.net         
>                          
>                     [EMAIL PROTECTED]    
>    Subject:     Re: [perl-win32-gui-users] Unresponsive 
> Window                         
>                     eforge.net                                
>                                                               
>                          
>                                                               
>                                                               
>                          
>                                                               
>                                                               
>                          
>                     04/01/2003 03:09 AM                       
>                                                               
>                          
>                                                               
>                                                               
>                          
>                                                               
>                                                               
>                          
> 
> 
> 
> 
> On approximately 3/31/2003 2:59 PM, came the following characters from
> the keyboard of [EMAIL PROTECTED]:
> > Hello,
> >
> > I have a script that gives the user options of which 
> application to run
> on
> > a server from a drop down box, then when they pick the 
> option and hit an
> > execute button the script opens a socket connection to a 
> server on the
> > network.  The server on the network than runs the 
> application and sends
> > periodic updates back to the client (ex. Stage 1 complete, Stage 2
> > complete, etc..., Finished).  These updates are printed out 
> on a rich
> edit
> > field.  When the execute button is clicked and the socket 
> connection is
> > created the window becomes unresponsive except when a 
> message comes back
> > from the server and is put on the rich edit field.  For 
> instance when I
> hit
> > execute I get a message back pretty quickly that the 
> connection is made
> and
> > the server has started the application and is on stage 1.  
> However, it
> > takes about 5 minutes for stage 1 to complete and during 
> that time you
> > can't minimize the window, move it, or anything else.  And 
> if you go to
> > another app on the computer (IE, Word, email) and then back 
> to the window
> > it won't refresh until another message comes back from the 
> server.  This
> is
> > a very annoying bug.  Is there a way around this.
> 
> You can use a "DoEvents" loop...  or you can use fork().  When you use
> fork(), it is necessary for the parent to process the windows, and the
> child to do the other activities.  If the windows are created 
> before the
> fork(), it is possible for the child to see, access, and update the
> window components, but not for it to respond to window messages.  That
> might be enough for your application, or you might need to define some
> hidden "widgets" to be used for communication between parent 
> and child.
> 
> >                                                      I was 
> thinking of
> using
> > fork() to call the socket connection that way technically 
> it's running on
> > its own and the window remains responsive.  I haven't used 
> fork before
> and
> > I get two problems, the window is still unresponsive waiting for the
> server
> > to reply, and when I execute an app a second time I get a 
> memory error
> > message but the program still runs and the window is 
> updated as long as I
> > don't click "OK" to terminate the program from the error 
> window.  either
> > I'm doing something wrong or fork doesn't work with 
> Win32::GUI (usually
> > when things don't work it's because I'm doing something wrong).
> 
> I have an application using fork() that doesn't get a memory error
> message, so that is probably a function of your application, 
> rather than
> being endemic to Win32::GUI and fork().
> 
> >                                                             
>        Anyone
> > have a way to make this work.  I checked the archives and 
> it seems some
> > people have managed to get fork to work but they didn't 
> post the code to
> > show how it's done.  Also if there is a way to get this to 
> work without
> > using fork even better.  Eventually I will convert the script to an
> > executable using PerlApp from Activestate and I'm not sure 
> how well fork
> > works with that.
> >
> > Also is it possible to make a Rich Edit Field read only?  I 
> don't want
> the
> > user to be able to add, edit, or delete anything.  I just want the
> > application itself to add text to the field.
> >
> > Thanks,
> >
> > Joe
> >
> > Here are the relevant parts of the code:
> >
> > #==================
> > sub XButton_Click {
> >   $MainWin->XButton->Disable();
> >   $apppicked = $applist[$Appdropdown->SelectedItem];
> >   print "Execute button clicked, got data: $apppicked\n";
> >   CallServerApp($apppicked);    # This calls the function 
> that initiates
> > the socket connection
> >   $MainWin->XButton->Enable();
> > }
> >
> >
> > #==================
> > sub CallServerApp {
> >
> >   use IO::Socket;
> >
> > # create the fork() pid
> >   if(fork()) {
> >     #create socket
> >     $sock = new IO::Socket::INET(PeerAddr => 'xxx.xxx.xxx.xxx',
> >                     PeerPort => someport,
> >                     Proto    => 'tcp'
> >                    );
> >     die "Socket could not be created. Reason: $!\n" unless $sock;
> >
> >     # send the name of the app that the user wants the server to run
> >     print $sock $_[0] . "\n";
> >     $sock->flush();
> >
> >     # print the replies from the server about the status of 
> the app to
> the
> > RichEdit Field
> >     while(<$sock>) {
> >       $ResponseTF->ReplaceSel($_."\r\n");
> >       $MainWin->Update();
> >       $MainWin->DoEvents();
> >     }
> >
> >     close($sock);
> >   }
> >   return 1;
> > }
> >
> >
> >
> >
> >
> >
> >
> > -------------------------------------------------------
> > This SF.net email is sponsored by: ValueWeb:
> > Dedicated Hosting for just $79/mo with 500 GB of bandwidth!
> > No other company gives more support or power for your 
> dedicated server
> > http://click.atdmt.com/AFF/go/sdnxxaff00300020aff/direct/01/
> > _______________________________________________
> > Perl-Win32-GUI-Users mailing list
> > Perl-Win32-GUI-Users@lists.sourceforge.net
> > https://lists.sourceforge.net/lists/listinfo/perl-win32-gui-users
> >
> >
> 
> 
> --
> Glenn
> =====
> If the grass is greener on the other side of the fence,
> try taking better care of your own side.     -- Unknown
> 
> 
> 
> -------------------------------------------------------
> This SF.net email is sponsored by: ValueWeb:
> Dedicated Hosting for just $79/mo with 500 GB of bandwidth!
> No other company gives more support or power for your dedicated server
> http://click.atdmt.com/AFF/go/sdnxxaff00300020aff/direct/01/
> _______________________________________________
> Perl-Win32-GUI-Users mailing list
> Perl-Win32-GUI-Users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/perl-win32-gui-users
> 
> 
> 
> 
> 
> 
> 
> -------------------------------------------------------
> This SF.net email is sponsored by: ValueWeb: 
> Dedicated Hosting for just $79/mo with 500 GB of bandwidth! 
> No other company gives more support or power for your dedicated server
> http://click.atdmt.com/AFF/go/sdnxxaff00300020aff/direct/01/
> _______________________________________________
> Perl-Win32-GUI-Users mailing list
> Perl-Win32-GUI-Users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/perl-win32-gui-users
> 

Reply via email to