Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 取消线程睡眠_C#_Multithreading_Sleep - Fatal编程技术网

C# 取消线程睡眠

C# 取消线程睡眠,c#,multithreading,sleep,C#,Multithreading,Sleep,我有一个简单的控制台应用程序,有两个运行线程 第一个线程正在测量一些值,第二个线程查找用户输入并执行一些鼠标移动 while (true) { if (Input.IsKeyDown(VC_L)) { Mouse.Move(300, 500); Thread.Sleep(thread1_delay); Mouse.Move(670, 300); Thread.Sleep(thread1_delay);

我有一个简单的控制台应用程序,有两个运行线程

第一个线程正在测量一些值,第二个线程查找用户输入并执行一些鼠标移动

while (true)
{
    if (Input.IsKeyDown(VC_L))
    {
        Mouse.Move(300, 500);
        Thread.Sleep(thread1_delay);
        Mouse.Move(670, 300);
        Thread.Sleep(thread1_delay);
        Mouse.Move(870, 700);
        Thread.Sleep(thread1_delay);
    }
}

问题是,当我得到另一个键作为输入时,我想停止第二个线程。但是它不工作,因为线程仍然处于睡眠状态并且没有反应。

第二个线程应该在醒来时检查布尔值。当满足条件时,应将此值设置为false。现在,当第二个线程唤醒时,它将完成它的执行。

只需使用一个线程,就可以完成它

传播应取消操作的通知

示例

public static async Task DoFunkyStuff(CancellationToken token)
{
   // a logical escape for the loop
   while (!token.IsCancellationRequested)
   {
      try
      {
         Console.WriteLine("Waiting");
         await Task.Delay(1000, token);
      }
      catch (OperationCanceledException e)
      {
         Console.WriteLine("Task Cancelled");
      }
   }
   Console.WriteLine("Finished");
}
static async Task Main(string[] args)
{

   var ts = new CancellationTokenSource();

   Console.WriteLine("Press key to cancel tasks");
   var task = DoFunkyStuff(ts.Token);

   // user input
   Console.ReadKey();

   Console.WriteLine("Cancelling token");

   // this is how to cancel
   ts.Cancel();

   // just to prove the task has been cancelled
   await task;

   // because i can
   Console.WriteLine("Elvis has left the building");
   Console.ReadKey();
}
用法

public static async Task DoFunkyStuff(CancellationToken token)
{
   // a logical escape for the loop
   while (!token.IsCancellationRequested)
   {
      try
      {
         Console.WriteLine("Waiting");
         await Task.Delay(1000, token);
      }
      catch (OperationCanceledException e)
      {
         Console.WriteLine("Task Cancelled");
      }
   }
   Console.WriteLine("Finished");
}
static async Task Main(string[] args)
{

   var ts = new CancellationTokenSource();

   Console.WriteLine("Press key to cancel tasks");
   var task = DoFunkyStuff(ts.Token);

   // user input
   Console.ReadKey();

   Console.WriteLine("Cancelling token");

   // this is how to cancel
   ts.Cancel();

   // just to prove the task has been cancelled
   await task;

   // because i can
   Console.WriteLine("Elvis has left the building");
   Console.ReadKey();
}
结果

Press key to cancel tasks
Waiting
Waiting
Waiting
Waiting
Waiting
Cancelling token
Task Cancelled
Finished
Elvis has left the building

一个选项是使用
Task.Delay
,以及
CancellationToken
如果需要取消睡眠,则不应使用线程。睡眠时,应使用任务和取消令牌。要回答您的问题,您可以使用
线程。中断
将其从当前/下一次睡眠中唤醒,但这就像用“服用止痛药”回答“如何在不伤害食物的情况下停止进食”的问题一样我认为你可以使用线程的中止method@RamilAliyev让Eric Lippert代替线程。中断和线程。中止都是错误的答案。正确的答案是,你不应该尝试取消一个线程。相反,你应该使用其他可以取消的线程。如果您不想/不能使用任务,请使用WaitHandle,如ManualResetEvent,并在给定的超时时间内等待它。当超时发生时,等待结束,或者如果向句柄发送信号,等待结束。