Hi,

Please excuse me if this has already been asked numerous times on this
list (I'm new to the list).

I'm trying to implement a very simple Win32::GUI application which is
able to listen on an open socket for incoming connection requests (this
problem doesn't *really* have to do with sockets, though).  My problem
is that the script doesn't get to the socket code until the GUI is
destroyed or I outright call it with a UI action (button click or
something).  The script does respond fine to UI actions but even when I
create a button to instantiate the socket code as a subroutine (instead
of being called at script execution), the socket code blocks and then I
can't use the UI until I kill the socket portion.  :-(

Is there a way to do what I'm trying to do without creating a separate
process?  Since there's no fork() option under Win32-Perl, I'll be stuck
using Win32::Process (or even Spawn) which means I have to have a
"hidden" process which I may not be able to kill properly if something
goes wrong or my main script dies for some reason - I'm trying to avoid
that.

First, is there a way to "unblock" the initial Window state of a
Win32::GUI app?
Second, is there a way to return control to the UI portion from a
potentially blocking subroutine?

Here's a simple example of a problematic UI block:

use Win32::GUI;
use IO::Select;
use IO::Socket;
$MW = new Win32::GUI::Window(
    -title   => 'blah',
    -left    => 100,
    -top     => 100,
    -width   => 175,
    -height  => 78,
    -name    => 'MainWindow',
    -visible => 1,
);
$bye = $MW->AddButton(
    -text    => 'Quit', 
    -name    => 'Quit',
    -left    => 60,
    -top     => 15,
);
Win32::GUI::Dialog();
sub MainWindow_Terminate {
  $MW->PostQuitMessage(1);
}
sub Quit_Click {
  $MW->PostQuitMessage(0);
}
$lsn = new IO::Socket::INET(Listen => 1, LocalPort => 2345);
$sel = new IO::Select($lsn);
while (@ready = $sel->can_read) {
  foreach $fh (@ready) {
    if ($fh == $lsn) {
      $new = $lsn->accept;
      $sel->add($new);
    } else {
      @data = <$fh>;
      $data = join ('', @data);
      print "Data sent: $data\n";
      $sel->remove($fh);
      $fh->close;
    }
  }
}

Here's a sample where the subroutine blocks:

use Win32::GUI;
use IO::Select;
use IO::Socket;
$MW = new Win32::GUI::Window(
    -title   => 'blah',
    -left    => 100,
    -top     => 100,
    -width   => 175,
    -height  => 78,
    -name    => 'MainWindow',
    -visible => 1,
);
$bye = $MW->AddButton(
    -text    => 'Quit', 
    -name    => 'Quit',
    -left    => 20,
    -top     => 15,
);
$start = $MW->AddButton(
    -text    => 'Start', 
    -name    => 'Start',
    -left    => 80,
    -top     => 15,
);
Win32::GUI::Dialog();
sub MainWindow_Terminate {
  $MW->PostQuitMessage(1);
}
sub Quit_Click {
  $MW->PostQuitMessage(0);
}
sub Start_Click {
  $lsn = new IO::Socket::INET(Listen => 1, LocalPort => 2345);
  $sel = new IO::Select($lsn);
  while (@ready = $sel->can_read) {
    foreach $fh (@ready) {
      if ($fh == $lsn) {
        $new = $lsn->accept;
        $sel->add($new);
      } else {
        @data = <$fh>;
        $data = join ('', @data);
        print "Data sent: $data\n";
        $sel->remove($fh);
        $fh->close;
      }
    }
  }
}

I can't win either way and Win32::Process is going to be a pain to
implement just for the sake of handling a potentially blocking routine. 
Please be kind in your responses as I'm simply an artist who knows just
enough code to be dangerous...  :-)

~jihad

-- 
<>>>===========================================================<<<>
 Jihad Battikha <[EMAIL PROTECTED]> http://www.highsynth.com
 -=< graphics . 2d/3d animation . illustration . web authoring >=-
<>>>===========================================================<<<>
 Before sending me commercial e-mail, the sender must first agree
 to my LEGAL NOTICE located at: http://www.highsynth.com/sig.html.
<>>>===========================================================<<<>

Reply via email to