本文整理汇总了C#中Worker.RequestStop方法的典型用法代码示例。如果您正苦于以下问题:C# Worker.RequestStop方法的具体用法?C# Worker.RequestStop怎么用?C# Worker.RequestStop使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Worker
的用法示例。
在下文中一共展示了Worker.RequestStop方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main()
{
// 创建线程对象。这不会启动该线程。
Worker workerObject = new Worker();
Thread workerThread = new Thread(workerObject.DoWork);
// 启动辅助线程。
workerThread.Start();
Console.WriteLine("main thread: Starting worker thread...");
// 循环直至辅助线程激活。
while (!workerThread.IsAlive);
// 为主线程设置 1 毫秒的休眠,
// 以使辅助线程完成某项工作。
Thread.Sleep(1);
// 请求辅助线程自行停止:
workerObject.RequestStop();
// 使用 Join 方法阻塞当前线程,
// 直至对象的线程终止。
workerThread.Join();
Console.WriteLine("main thread: Worker thread has terminated.");
}
示例2: Main
static void Main()
{
// Create the thread object. This does not start the thread.
Worker workerObject = new Worker();
Thread workerThread = new Thread(workerObject.DoWork);
// Start the worker thread.
workerThread.Start();
Console.WriteLine("main thread: Starting worker thread...");
// Loop until worker thread activates.
while (!workerThread.IsAlive) ;
// Put the main thread to sleep for 1 millisecond to
// allow the worker thread to do some work:
Thread.Sleep(1);
// Request that the worker thread stop itself:
workerObject.RequestStop();
// Use the Join method to block the current thread
// until the object's thread terminates.
workerThread.Join();
Console.WriteLine("main thread: Worker thread has terminated.");
}
示例3: WaitMessage
public static int WaitMessage()
{
// Create the thread object. This does not start the thread.
Worker workerObject = new Worker();
workerObject.m_connection = m_connection;
Thread workerThread = new Thread(workerObject.DoWork);
// Start the worker thread.
workerThread.Start();
MessageBox.Show("main thread: Starting worker thread...");
// Loop until worker thread activates.
while (!workerThread.IsAlive) ;
// Put the main thread to sleep for 1 millisecond to
// allow the worker thread to do some work:
Thread.Sleep(1);
// Request that the worker thread stop itself:
workerObject.RequestStop();
// Use the Join method to block the current thread
// until the object's thread terminates.
workerThread.Join();
MessageBox.Show("main thread: Worker thread has terminated.");
//while (m_wait_message)
//{
// MessageBox.Show("WaitMessage");
//}
/*
//while (m_wait_message)
while (true)
{
} */
return (0);
}