Click here to Skip to main content
16,015,258 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I need to make pause button and resume button for winform application.
And I found ManualResetEvent is easy way to create them.
Is that right?
I added ManualResetEvent in my source code but pausing the foreach loop is not working.
What am I missing?
Please give me advice
Thank you

C#
static ManualResetEvent mr = new ManualResetEvent(true);


       public Form1()
       {
           InitializeComponent();
       }

       private void btn_start_Click(object sender, EventArgs e)
       {
         Thread  _thread = new Thread(work);
           _thread.Start();

       }
       private void work()
       {
           foreach (TreeNode node in treeview1.Nodes)
           {
                   mr.WaitOne();

                   if (node.Checked)
                   {
                      some code
                   }


           }
          
       }

       private void btn_pause_Click(object sender, EventArgs e)
       {
           mr.Reset();
       }

       private void btn_resume_Click(object sender, EventArgs e)
       {
           mr.Set();
       }


What I have tried:

I have tried put the MenaulResetEvent.waitone(); at the end of loop.
But still not working
Posted
Updated 22-Apr-16 13:48pm
v2
Comments
Rob Philpott 6-Apr-16 8:51am    
Breaking the oldest rule in the book there - only the GUI thread can manipulate controls. Regardless of the practicality of ManualResetEvent, you definitely should not be attempting control updates from a worker thread.
Sergey Alexandrovich Kryukov 22-Apr-16 19:37pm    
You are right, but where the inquirer does it?
The real problem is different: invalid cross-thread calls of the UI elements.
—SA
Sergey Alexandrovich Kryukov 22-Apr-16 19:34pm    
Yes, that's right, event wait handle is good thing, because it's safe, unlike thread pause — you keep control over the point in code where it can be paused. But it should not be done on UI thread.
That's all, basically.
—SA

1 solution

Your idea is quite good.

You are missing one thing unrelated to pausing, but related to UI thread. You should not manipulate objects added to currently running UI added to it in UI thread. You try to do it in your additional non-UI thread, and this is bad cross-thread operation. You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke(),
Problem with Treeview Scanner And MD5.

See also more references on threading:
.NET event on main thread,
How to get a keydown event to operate on a different thread in vb.net,
Control events not firing after enable disable + multithreading.

In addition to that, you should exclude the possibilities of modifying the state of your control from two threads in some random manner. In particular, you should not update the Nodes from UI events directly while the loop is traversing the set of nodes. Combined with your pausing feature, which is itself is quite correct, it may lead to a bit too complicated thread synchronization schema you should study theoretically very accurately (such problems are undetectable using the usual debugging method, no testing methods at all). That said, I would consider the whole problem in a holistic manner and probably think about total redesign of your code. At this moment, I cannot advise anything certain, because I don't know your ultimate goals.

One more note. Never ever use names like treeview1, btn_pause_Click or any other auto-generated names. They violate (good) Microsoft naming conventions (authors of automatic code generation simply had no choice) and makes no sense. Names should be semantically sensible. You are supposed to rename every auto-generated name using the refactoring engine, which makes is easy. Also, it's a matter of some discussion, but I usually advice not to generate any event handlers in the designer. The code generated for adding such handlers is ugly and even obsolete.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900