c# - Is there any way to disable a windows.form.timer inside a Task? -


is there way disable windows.form.timer inside task?

here example code have.

private void timer1_tick(object sender, eventargs e) {      task.factory.startnew( () => {runtask();} ); } private void runtask() {      //process here....      timer1.enabled = false;      messagebox.show(timer1.enabled.tostring()); } 

when run program value of timer1.enabled still true. there anyway disable timer inside task?

disabling timer works fine, can tell not getting screenful of message boxes. test code problem, has threading race bug. easy see sleeping bit:

    private void runtask() {         //process here....         timer1.enabled = false;         system.threading.thread.sleep(100);         messagebox.show(timer1.enabled.tostring());     } 

you see "false". see exact same bug changing @ron's snippet , use begininvoke instead of invoke.

the timer class thread-safe stopping , doesn't need help. can see reference source, note call postmessage(). not safe starting timer, worker threads don't run dispatcher loop tick event won't fire.

you don't have problem, don't fix it.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -