I have
a: A click event.
b: A task which takes a long time.
And the click is supposed to (among other things) stop the task.
It seems (correct me if I’m wrong) that this would cause trouble in multi-threading. So I thought of checking, from time to time, from within the task to see whether the event was fired.
How do I do that?
Checking if an event has been fired
Page 1 of 18 Replies - 386 Views - Last Post: 28 June 2011 - 10:28 AM
Replies To: Checking if an event has been fired
#2
Re: Checking if an event has been fired
Posted 28 June 2011 - 09:00 AM
Have a global variable accessible to your thread that returns a boolean on if the event was triggered.
#3
Re: Checking if an event has been fired
Posted 28 June 2011 - 09:01 AM
When the event fires it causes a handler to execute.
So set a scope/global bool in the handler to indicate it has fired. Of course you'll need to reset that to false before the next expected firing.
I would probably set a DateTime to the moment the event was fired. That way I can tell if it has been handled.
Fired at: 28jun11 0900 hrs.
It is now: 28jun11 0930hrs - Have not yet reacted
That sort of thing.
So set a scope/global bool in the handler to indicate it has fired. Of course you'll need to reset that to false before the next expected firing.
I would probably set a DateTime to the moment the event was fired. That way I can tell if it has been handled.
Fired at: 28jun11 0900 hrs.
It is now: 28jun11 0930hrs - Have not yet reacted
That sort of thing.
#4
Re: Checking if an event has been fired
Posted 28 June 2011 - 09:02 AM
Well, in a single thread, the click would never happen, because the long-running task will block the GUI thread, so that the click never happens until the task is done.
BackgroundWorker has a mechanism for cancellation. In this example, I have two buttons, one to start work, one to stop it. I also have a progress bar.
BackgroundWorker has a mechanism for cancellation. In this example, I have two buttons, one to start work, one to stop it. I also have a progress bar.
BackgroundWorker worker;
public Form1() {
InitializeComponent();
worker = new BackgroundWorker() {
WorkerReportsProgress = true,
WorkerSupportsCancellation = true
};
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
}
private void button1_Click(object sender, EventArgs e) {
progressBar1.Value = 0;
worker.RunWorkerAsync();
}
private void button2_Click(object sender, EventArgs e) {
worker.CancelAsync();
}
void worker_DoWork(object sender, DoWorkEventArgs e) {
for (int i = 0; i < 100; i++) {
Thread.Sleep(250);
worker.ReportProgress(i + 1);
if (worker.CancellationPending) {
e.Cancel = true;
break;
}
}
}
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e) {
this.Invoke(new Action(() => progressBar1.Value = e.ProgressPercentage));
}
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
if (e.Cancelled) {
MessageBox.Show("Task Cancelled");
}
else if (e.Error != null) {
MessageBox.Show(e.Error.Message);
}
else {
MessageBox.Show("Completed");
}
}
#5
Re: Checking if an event has been fired
Posted 28 June 2011 - 09:27 AM
#6
Re: Checking if an event has been fired
Posted 28 June 2011 - 09:33 AM
Possibly, possibly not. When you're performing a long operation on your GUI thread, the GUI becomes locked and unresponsive. Just like every other program you've had freeze, that's what happens to yours when you don't use a background thread to do long operations. So, even if it is queued, it'll only be reported after your task completes. What's the point of a cancel button that can't actually be used until the operation's already done?
#7
Re: Checking if an event has been fired
Posted 28 June 2011 - 09:42 AM
What I meant was:
Is there a way that the task can check to see whether the click has been queued, so that the task will know to stop itself even before the click has been “reported”?
Is there a way that the task can check to see whether the click has been queued, so that the task will know to stop itself even before the click has been “reported”?
#8
Re: Checking if an event has been fired
Posted 28 June 2011 - 10:25 AM
No, because if the click hasn't been reported, the code can't know about it. Basically, on one thread, the program can only do one thing at a time. If your task is running, the GUI can't even register that a click has happened. The OS might buffer input to your application so that it happens all at once when it becomes responsive, but to your application, nothing exists but the task at hand.
That's why I gave you an example of a background worker, so that you can use a background thread for your processing, report progress, and check for cancellation.
That's why I gave you an example of a background worker, so that you can use a background thread for your processing, report progress, and check for cancellation.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote








|