At 18:16 2004-01-15, Nigel Cannings wrote:
Is there any way to display the icon, but allow the main application to
carry on doing its thing in the background (my application is listening for
a socket connection)
There are a couple of ways to do this:
1. Run your application in the spare time. Use a Win32::GUI::Timer and call
your application from the event that gets fired by the Timer.
2. Run the GUI in the spare time. If your program has an idle loop that
never blocks for more than fractions of a second, you can call
Win32::GUI::DoEvents() to have the GUI process Windows events. But this
doesn't work if there are things that take longer than that, causing the
GUI to be unresponsive.
3. Run the GUI in a separate thread. I haven't done this in a real program,
but below is a simple example I whipped up out of curiosity. Perl
threading, available from 5.8.0, is limited and possibly unstable
(especially >= 5.8.2 seems very fragile). But I would imagine it's useful
for implementing a simple messaging system so commands can be passed from
the GUI and information+events be passed to the GUI.
There are two files: test32.pl and test32-gui.pl
The Win32::GUI stuff isn't brought in until after the GUI thread is
started. I'm not sure it won't work to do it before, but I suspect some XS
or Windows structures will get screwed up by the two threads both using
them in inconsistent ways. But maybe not since nothing is shared by default.
The shared variables are declared and shared before the thread is started.
That may not be necessary, I don't know.
Without the final join(), Perl crashes. So it's probably wrong to not do it
:) I don't really know threads or how they work, so please fill in the
blanks here.
#!/usr/local/bin/perl -w #test32.pl
use strict;
use threads;
use threads::shared;
our $randomNumber = 37; #The least random number
share($randomNumber);
our $exitProgram = 0;
share($exitProgram);
my $oGuiThread = threads->create( sub { eval q{require "test32-gui.pl"} });
while(! $exitProgram) {
print "In main, random is ($randomNumber)\n";
sleep(1);
}
$oGuiThread->join();
__END__
#!/usr/local/bin/perl -w #test32-gui.pl
use strict;
use Win32::GUI;
my $winMain = new Win32::GUI::Window(
-left => 13,
-top => 32,
-width => 439,
-height => 260,
-name => "winMain",
-text => "Autoscroller",
);
$winMain->AddButton(
-text => "&Random",
-name => "btnRandom",
-width => 150,
-height => 21,
-left => 30,
-top => 20,
);
$winMain->Show();
sub ::winMain_Terminate {
$::exitProgram = 1;
return(-1);
}
sub ::btnRandom_Click {
$::randomNumber = int(rand(1000));
print "Button handler, setting random number to ($::randomNumber)\n";
return(1);
}
Win32::GUI::Dialog();
__END__
/J
-------- ------ ---- --- -- -- -- - - - - -
Johan Lindström Sourcerer @ Boss Casinos [EMAIL PROTECTED]
Latest bookmark: "Professor Lives Life As a Cyborg"
<http://story.news.yahoo.com/news?tmpl=story&u=/ap/20040110/ap_on_hi_te/internet_profile_cyborg_1>
dmoz (1 of 30): /Computers/Software/Fonts/ 96