Board index » cppbuilder » What's Best way to Provide a 'Cancel' job button in BDS....
ActuaryOne
CBuilder Developer |
ActuaryOne
CBuilder Developer |
What's Best way to Provide a 'Cancel' job button in BDS....2007-05-27 02:31:18 AM cppbuilder80 I have a long running job that reports intermediate real time results in the window caption...sometimes we want to cancel it midstream...a Cancel Button doesn't seem to be live while its running...I'm sure this is easy, but I'm clueless on the protocol.......thanks. |
Abcidefugian
CBuilder Developer |
2007-05-27 02:50:22 AM
Re:What's Best way to Provide a 'Cancel' job button in BDS....
A good idea is to investigate in multithreading.
If an event function is running, then no input is accepted. "ActuaryOne" < XXXX@XXXXX.COM >wrote in message QuoteI have a long running job that reports intermediate real time results in |
Dennis Jones
CBuilder Developer |
2007-05-27 12:10:28 PM
Re:What's Best way to Provide a 'Cancel' job button in BDS....
"ActuaryOne" < XXXX@XXXXX.COM >wrote in message
QuoteI have a long running job that reports intermediate real time results in the main thread, then mouse clicks, button presses, screen updates, etc. will not be handled until after the task finishes. This is why the button does not appear to be "live" while your task is running. To allow for the cancellation of a task, your task must execute in a separate thread so that the mouse clicks and button presses can still be handled in the main thread. Once your task is running in a thread, it should periodically check to see if there has been a request to cancel it (a simple flag will do), and if so, abort the task and exit the thread. Pressing the cancel button simply sets the cancel flag. In VCL, screen updates must occur in the main thread (not doing so can result in odd behavior or crashes), so if you need to invoke screen updates from within the thread, and you are using the TThread class in the VCL, you must perform your screen updates in a routine that is called via the TThread::Synchronize method (which forces the code to execute within the context of the main thread). If you do not use TThread, or if real-time updates are not critical to your app, then you can use PostMessage to tell the main thread to perform screen updates whenever it gets a chance. - Dennis {smallsort} |
JD
CBuilder Developer |
2007-05-27 07:50:11 PM
Re:What's Best way to Provide a 'Cancel' job button in BDS....
"Dennis Jones" < XXXX@XXXXX.COM >wrote:
Quote
for( int x = 0; x < SomeLimit; ++x ) { if( ! (x % 100) ) { Application->ProcessMessages(); if( SomeFlagSet ) { // abort the process } } } ~ JD |
Dennis Jones
CBuilder Developer |
2007-05-28 12:35:09 AM
Re:What's Best way to Provide a 'Cancel' job button in BDS....
"JD" < XXXX@XXXXX.COM >wrote in message
Quote
the amount of processing that occurs inside the loop and the values of 'x' and the divisor (100 in your example), the program could still appear to be unresponsive for some period of time. Even if only for a moment, that might be enough to make it undesirable. - Dennis |
Duane Hebert
CBuilder Developer |
2007-05-28 03:55:52 AM
Re:What's Best way to Provide a 'Cancel' job button in BDS....
"Dennis Jones" < XXXX@XXXXX.COM >wrote in message
Quote
IMO threads are one of the most difficult things to get right. I'd go for the simpler approach wherever possible. |
Dennis Jones
CBuilder Developer |
2007-05-28 06:28:03 AM
Re:What's Best way to Provide a 'Cancel' job button in BDS....
"Duane Hebert" < XXXX@XXXXX.COM >wrote in message
Quote
QuoteIMO threads are one of the most difficult - Dennis |
JD
CBuilder Developer |
2007-05-28 11:31:09 AM
Re:What's Best way to Provide a 'Cancel' job button in BDS....
"Duane Hebert" < XXXX@XXXXX.COM >wrote:
Quote
executing within the context of the same thread as you suggest, how would the time-out be detected without calling ProcessMessages()? Wouldn't that require a worker thread? QuoteIMO threads are one of the most difficult things to get it is to get the worker thread to work with the main thread or other worker threads. From my perspective, once one understands race conditions and dead-locks, worker threads are a breeze to design and impliment. In this case, it seems that a worker thread is well advised and I say that because every time one calls ProcessMessages, one must also code to account for reenterence by disabling controls and/or setting flags that certain events must check (not to mention the overhead of calling ProcessMessages). QuoteI'd go for the simpler approach wherever possible. |
Duane Hebert
CBuilder Developer |
2007-05-28 06:13:41 PM
Re:What's Best way to Provide a 'Cancel' job button in BDS....
"JD" < XXXX@XXXXX.COM >wrote in message
Quote
time>something sensible and then call ProcessMessages(). Quote
|
JD
CBuilder Developer |
2007-05-30 06:20:30 PM
Re:What's Best way to Provide a 'Cancel' job button in BDS....
"Duane Hebert" < XXXX@XXXXX.COM >wrote:
Quote
~ JD |