Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I know Thread.Suspend() and Thread.Resume() is not good one for program practice.

I have built one small windows form application.
When user close the Window Form while processing the task, i'm showing warning message box(FormClosing Event). If user clicks "yes" button then it will Exit the application. If they click "No" then it will perform the action.

Even though warning Message Box appears, application performing the task. But i want if the warning message box appears then i must pause the thread. If user clicked "yes" button then i'll terminate. If user clicked "No" then i'll continue the process.

Please guide me on this. . . ,

C#
private void frmTranxitCCTVJobCreation_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                if (bIsButtonClicked == true)
                {
                    if (_threadProcessCCTV.IsAlive)
                    {
                        dynamic mboxResult = MessageBox.Show("Are you sure you want to exit the form?", "Tranxit CCTV Job Creation Tool", MessageBoxButtons.YesNo);
                        if (mboxResult == DialogResult.Yes)
                        {
                            //e.Cancel = false;
                            Application.Exit();
                        }

                        if (mboxResult == DialogResult.No)
                        {
                            // Cancel the Closing event from closing the form.
                            e.Cancel = true;
                        }
                    }
                }
            }
        }
Posted
Updated 28-Dec-13 23:32pm
v4

1 solution

It's good that you understand that thread Pause and Resume are bad. They are really and critically dangerous, so those methods have been deprecated in .NET. The right approach is: you need to pause and resume the thread operation only in some selected point of its code, where the thread makes a blocking call. Such call puts the thread in a special wait state, wasting no CPU time; the thread is switched off and not scheduled back to execution until it is waken up by some condition.

If your purpose is just pausing and resuming a thread from some other thread use to "throttle" your thread's execution, the adequate class is the event wait handle, System.Threading.ManualResetEvent (System.Threading.EventWaitHandle can be used is well, with the constructor parameter indicating manual reset control). Please see:
http://msdn.microsoft.com/en-us/library/system.threading.manualresetevent%28v=vs.110%29.aspx[^].

For further explanations, please see my past answer: Running exactly one job/thread/process in webservice that will never get terminated (asp.net)[^] (please see the first part of the answer, before the blocking queue, which is also very useful for different things).

For some background, see also: Security on my developed dll[^].

—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