Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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_Asynchronous_Async Await - Fatal编程技术网

C# 线程在异步等待之前屈服

C# 线程在异步等待之前屈服,c#,multithreading,asynchronous,async-await,C#,Multithreading,Asynchronous,Async Await,在C#引入async wait编程之前,如何将网络请求放入另一个线程,并将执行时间返回给CPU,直到收到响应,这样该线程就不会浪费CPU时间 因为当CPU将时间分配给这个线程,而线程空闲等待响应时,这将是CPU时间的浪费,对吗?从几个方面来说,这种类型的异步是可行的 使用IAsyncResult设计模式的异步操作是 作为名为BeginOperationName和 开始和结束异步操作的操作名 操作名称。例如,FileStream类提供 BeginRead和EndRead方法异步读取 文件这些方法实

在C#引入
async wait
编程之前,如何将网络请求放入另一个线程,并将执行时间返回给CPU,直到收到响应,这样该线程就不会浪费CPU时间

因为当CPU将时间分配给这个线程,而线程空闲等待响应时,这将是CPU时间的浪费,对吗?

从几个方面来说,这种类型的异步是可行的

使用IAsyncResult设计模式的异步操作是 作为名为
BeginOperationName
和 开始和结束异步操作的
操作名
操作名称
。例如,
FileStream
类提供
BeginRead
EndRead
方法异步读取 文件这些方法实现了读取的异步版本 方法

回答你的问题

因为当CPU分配时间给这个线程时,线程处于空闲状态 等待响应是浪费CPU时间,对吗

不阻塞线程并等待完成端口回调不会导致CPU周期流失,但是轮询线程会导致CPU周期流失

这有很多工作原理,但是这里可以看到一个示例使用

用法示例

private static void TestWrite()
{ 
   // Must specify FileOptions.Asynchronous otherwise the BeginXxx/EndXxx methods are
   // handled synchronously.
   FileStream fs = new FileStream(Program.FilePath, FileMode.OpenOrCreate,
      FileAccess.Write, FileShare.None, 8, FileOptions.Asynchronous);

   string content = "A quick brown fox jumps over the lazy dog";
   byte[] data = Encoding.Unicode.GetBytes(content);

   // Begins to write content to the file stream.
   Console.WriteLine("Begin to write");
   fs.BeginWrite(data, 0, data.Length, Program.OnWriteCompleted, fs);
   Console.WriteLine("Write queued");
}

private static void OnWriteCompleted(IAsyncResult asyncResult)
{ 
   // End the async operation.
   FileStream fs = (FileStream)asyncResult.AsyncState;
   fs.EndWrite(asyncResult);

   // Close the file stream.
   fs.Close();
   Console.WriteLine("Write completed");

   // Test async read bytes from the file stream.
   Program.TestRead();
}

看看您提供的参考资料,您的示例演示了EAP,而不是APM,对吗?因为似乎没有投票。@JohnL。不完全是,EAP是
MethodAsync(…)
CancelAsync(…)
对。通常有一个已完成的事件。想想这个模式的
BackgroundWorker
。阻塞的线程不使用CPU@PauloMorgado它不使用CPU,但当操作系统看到这个线程被阻塞时,它会切换到队列中的另一个线程吗?还是会等到分配给这个线程的时间片完成?它不会旋转。操作系统将CPU分配给另一个线程。