CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Wait for and generate a user defined event within Tk windows

 



PapaGeek
User

Mar 21, 2014, 6:47 AM

Post #1 of 6 (3361 views)
Wait for and generate a user defined event within Tk windows Can't Post

My process gives the user the ability to change the date on which reports are generated. This process performs web screen scraping of financial data and can take several minutes to complete. I created a window that asks if you are sure you want to change the date, and if the answer is yes, it uses a progress bar to show how the process is progressing.

Within the menu code that can request the date change the coding looks like:

Code
 	if ($requestedDate ne $lastUpdate) 
{
say "Ask if its OK";
setUpdateDates ($lastUpdate, $requestedDate);
ShowWindow('UpdateWindow');
say "return from update window";
}

The problem is that the “ShowWindow” call returns immediately and does not wait for the actual update to occur. ShowWindow is part of a user written window manager that handles a stack of “named windows” allowing only one window to be visible at a time:


Code
 sub ShowWindow 
{
my ($nextWindow) = @_;
my $prevWindow = $WindowStack[-1];
push @WindowStack, $nextWindow;
getWindow($prevWindow) -> withdraw();
getWindow($nextWindow) -> deiconify();
getWindow($nextWindow) -> raise();
}


I want to change my code to look something like this:

Code
 	if ($requestedDate ne $lastUpdate) 
{
say "check for date change";
setUpdateDates ($lastUpdate, $requestedDate);
CreateEvent(“UpdateIsCompleted”);
ShowWindow('UpdateWindow');
WaitForEvent(“UpdateIsCompleted”);
say "return from update window";
}


Then inside my sub that asks and controls the update, I will add the line:

Code
 GenerateEvent(“UpdateIsCompleted”);

Where needed.

I’m looking for sample code on line for user defined event handling and can’t seem to find any. Can this be done in Perl?


PapaGeek
User

Mar 21, 2014, 7:46 AM

Post #2 of 6 (3358 views)
Re: [PapaGeek] Wait for and generate a user defined event within Tk windows [In reply to] Can't Post

I tried this:


Code
  
my $updateIsDone = 0;

sub setUpdateDone
{
$updateIsDone = 1;
}

sub updateAccount
{

. . . . .
if ($requestedDate ne $lastUpdate)
{
say "Ask if OK";
setUpdateDates ($lastUpdate, $requestedDate);
ShowWindow('UpdateWindow');
while ($updateIsDone == 0)
{
say "sleep 5";
sleep(5);
}
say "return from update window";
}


And the end of the update process:


Code
		 
For each symbol that needs to the updated
{
. . . . .
$percent_done = (($index+1)/$symbolCount) * 100;
$window->update; # otherwise we don't see progress bar.
}
DBaccess::updateLastUpdate($asOf);
setUpdateDone();


But it locks up the Tk window main loop and does not allow the windows to change or refresh!


FishMonger
Veteran / Moderator

Mar 21, 2014, 8:00 AM

Post #3 of 6 (3356 views)
Re: [PapaGeek] Wait for and generate a user defined event within Tk windows [In reply to] Can't Post

Part of the problem is that you're using the sleep() function, which should not be used in Tk event loops. Instead, you should be using Tk::after.
https://metacpan.org/pod/distribution/Tk/pod/after.pod


PapaGeek
User

Mar 21, 2014, 8:23 AM

Post #4 of 6 (3353 views)
Re: [FishMonger] Wait for and generate a user defined event within Tk windows [In reply to] Can't Post

My original design was put the change date option at the top of the generate report window, and if a date change was sensed when a report was requested, ask and change the date first. The suggested Tk::after option works better, I can now kill the process without killing Eclipse to make it stop looping, but the called window still comes up blank and says it is not responding.

Looks like the main loop of Tk will not allow this kind of functionality. I’ll just change program logic to make changing the date a separate step.

Thanks for the suggestion.


(This post was edited by PapaGeek on Mar 21, 2014, 8:24 AM)


FishMonger
Veteran / Moderator

Mar 21, 2014, 8:30 AM

Post #5 of 6 (3349 views)
Re: [PapaGeek] Wait for and generate a user defined event within Tk windows [In reply to] Can't Post

Try testing it outside of Eclipse.


PapaGeek
User

Mar 21, 2014, 10:20 AM

Post #6 of 6 (3347 views)
Re: [FishMonger] Wait for and generate a user defined event within Tk windows [In reply to] Can't Post

Same issue Eclipse and direct, I had to add a call to window->update to get it to respond. I also noticed that when I read the web so I can scrape the financial data, the screens freeze while getting the web pages. I'll look into sending the update financial data process the window ID so it can perform the window->update as in the code that follows:


Code
my $updateIsDone = 0; 

sub setUpdateDone
{
$updateIsDone = 1;
}
. . .
if ($requestedDate ne $lastUpdate)
{
say "Ask if OK";
setUpdateDates ($lastUpdate, $requestedDate);
ShowWindow('UpdateWindow');
$updateIsDone = 0;
while ($updateIsDone == 0)
{
getWindow('UpdateWindow')->update;
getWindow('UpdateWindow')-> after (100);
}
say "return from update window";
}

The subs in the called window call sub setUpdateDone in this window file when their task is completed or aborted:

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives