暂停运行线程 [英] pause running thread

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

问题描述

你好朋友我有曲.那如何停止运行线程.我创建了窗口表单appln.
在该按钮上单击我创建一个线程.因此线程过程包含来自其他dll的函数调用.我的要求是,当我单击取消"按钮时,它将向用户显示您要关闭还是不关闭"的消息,所以请问我的问题.单击取消按钮如何停止我的线程以及执行函数.

hello friend i have Qu. that how to stop running thread. i created window form appln.
in that appln on button click i create one thread .so thread procedure contain function call from other dll.my requiement is when i click on cancel button it will show message to user that "do u want close or not" so my que. is on click of cancel button how to stop my thread as well as my function execution.

推荐答案

暂停线程已被认为是危险的,并且在.NET中不可用.

这是您需要使用而不是暂停/继续的内容:类之一:System.Threading.EventWaitHandleSystem.Threading.AutoResetEventSystem.Threading.ManualResetEvent;请参阅:
http://msdn.microsoft.com/en-us/library/system.threading. eventwaithandle.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.threading. autoresetevent.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.threading. manualresetevent.aspx [ ^ ].

可以使用第一类代替其他两个类.它们只是在事件句柄重置方式上有所不同:自动重置还是仅显式重置.

想法是这样的:事件具有两种状态,有信号或无信号.可以通过调用Set和未信号调用Reset来发出信号,否则等待线程会自动将其重置.现在,您可以通过要暂停的线程调用EventWaitHandle.WaitOne 来暂停线程.如果事件没有信号,线程将进入等待状态:操作系统将其关闭,并且直到唤醒后才安排回执行.如果一些其他线程在事件等待句柄的相同实例上调用Set,它将被唤醒;它也会被超时(可选的wait参数)或Thread.Abort唤醒.

当线程唤醒并且事件句柄使用自动重置时,它将在线程唤醒后立即重置为非信号状态.此过渡是原子的,无法通过其他方法(例如布尔条件)可靠地模拟.这种自动重置机制旨在一次传递一个且仅一个等待线程,并使在同一事件上等待的其他线程在等待状态下处理实例.

-SA
Pausing thread has been recognized as dangerous and is not available in .NET.

Here is what you need to use instead of Pause/Resume: one of the classes: System.Threading.EventWaitHandle, System.Threading.AutoResetEvent or System.Threading.ManualResetEvent; please see:
http://msdn.microsoft.com/en-us/library/system.threading.eventwaithandle.aspx[^],
http://msdn.microsoft.com/en-us/library/system.threading.autoresetevent.aspx[^],
http://msdn.microsoft.com/en-us/library/system.threading.manualresetevent.aspx[^].

The first class can be used instead the other two; they are only different in the way how the event handle is reset: automatically or only explicitly.

The idea is this: the event has two states, signaled or non-signaled. One can signal the event by the call to Set and un-signal calling Reset or it will be reset automatically by a waiting thread. Now, you pause the thread by calling EventWaitHandle.WaitOne by the thread to be paused. If the event is non-signaled, the thread is put in the wait state: it is switched out by the OS and never scheduled back to execution until it is awaken. It will be awaken if some other thread calls Set on the same instance of event wait handle; it also will be awaken by a timeout (optional wait parameter) or Thread.Abort.

When a thread is awaken and the event handle uses auto-reset, it will be reset back to non-signaled state immediately on the wake-up of the thread. This transition is atomic and cannot be reliably simulated by other methods (such as Boolean condition). This auto-reset mechanism is designed to pass one and only one waiting thread at a time and make other threads waiting on the same event handle instance in the wait state.

—SA


How to: Create and Terminate Threads (C# Programming Guide)

This link will be helpful .


这篇关于暂停运行线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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