如何在线程中暂停和恢复? [英] How to Pause and Resume in Threading?

查看:83
本文介绍了如何在线程中暂停和恢复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道Thread.Suspend()和Thread.Resume()不适合程序实践。



我已经构建了一个小的Windows窗体应用程序。

当用户在处理任务时关闭窗口窗体时,我显示警告消息框(FormClosing事件)。如果用户单击是按钮,则它将退出应用程序。如果他们单击否,则它将执行操作。



即使出现警告消息框,应用程序也会执行该任务。但我想如果出现警告消息框,那么我必须暂停线程。如果用户点击是按钮,那么我将终止。如果用户点击否,那么我将继续这个过程。



请指导我。 。 。 ,



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. . . ,

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;
                        }
                    }
                }
            }
        }

推荐答案

这是好的,你了解该线程暂停恢复是坏的。它们确实非常危险,所以这些方法在.NET中已被弃用。正确的方法是:您需要仅在其代码的某个选定点暂停和恢复线程操作,其中线程进行阻塞调用。这样的调用将线程置于特殊的等待状态,不浪费CPU时间;线程被关闭并且没有被安排回执行,直到它被某些条件唤醒。



如果你的目的只是暂停并从其他线程恢复一个线程用来限制线程的执行,适当的类是事件等待句柄, System.Threading.ManualResetEvent System.Threading.EventWaitHandle ,使用构造函数参数指示手动重置控制)。请参阅:

http ://msdn.microsoft.com/en-us/library/system.threading.manualresetevent%28v=vs.110%29.aspx [ ^ ]。



如需进一步说明,请参阅我过去的答案:在webservice中运行一个永远不会终止的作业/线程/进程(asp.net) [ ^ ](请在阻止队列之前查看答案的第一部分,这对于不同的事情也非常有用。)



对于某些背景,另见:我开发的dll上的安全性 [ ^ ]。



-SA
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


这篇关于如何在线程中暂停和恢复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆