C # abnormal synchronous operation

1. What is asynchronous?

Typically used to perform asynchronous operations may take longer to complete tasks, such as opening large files, connected to a remote computer or database queries = asynchronous operations performed in the threads other than the main application thread. When the application calls the method asynchronously perform an operation, the application can continue to execute in the asynchronous method performs its task.

2, the difference between synchronous and asynchronous

Synchronous (Synchronous): while performing an operation, the application must wait for the operation to continue after the execution is complete.

Asynchronous (Asynchronous): while performing an operation, the application can continue to execute when an asynchronous operation is performed. Essence: asynchronous, start a new thread, the main thread and the thread parallel execution method.

3 difference, asynchronous and multi-threaded

We already know, asynchronous and multi-threading is not an equal relationship, the ultimate goal is asynchronous, multi-threaded just a means to achieve our asynchronous. When a call is asynchronous requests to return to the caller, but the caller without waiting for their results and can do other things. Asynchronous multi-threading technology can be used or another process is to be handled.

Simply put: asynchronous thread from the thread pool is responsible for the management, and multi-threading, we can control their own, of course, in a multi-threaded, we can also use the thread pool.

Take Grilled terms of network worms, if you use asynchronous mode to achieve, it uses the thread pool management. When the asynchronous operation is performed, the operation will work threw a thread pool thread to complete. When the start I / O operations, asynchronous work will be returned to the thread pool thread, which means that the work will not get the page take up any CPU resources a. Until the completion of the asynchronous, that acquires the Web page is completed, asynchronous callback will notice the thread pool by the way. Visible, asynchronous mode by means of the thread pool, greatly saving CPU resources.

Note: DMA (Direct Memory Access) Direct Memory Access, as the name suggests DMA function it is to allow the device to bypass the processor can directly read data from the memory. Direct memory access through the data exchange is almost no loss of CPU resources. In hardware, hard drives, network cards, sound cards, video cards, etc. have a direct memory access function. Asynchronous programming model is to allow us to fully utilize the hardware direct memory access function to release the pressure of the CPU.

Two scenarios:

  • Compute-intensive work, the use of multi-threading.

  • IO-intensive work, the use of asynchronous mechanism.

C # code reference asynchronous (async and await)

using System;
using System.Threading;
using System.Threading.Tasks;

namespace AsyncAwaitDemo { class Program { static void Main(string[] args) { Console.WriteLine("main start.."); AsyncMethod(); Thread.Sleep(1000); Console.WriteLine("main end.."); Console.ReadLine(); } static async void AsyncMethod() { Console.WriteLine("start async"); var result = await MyMethod(); Console.WriteLine("end async"); //return 1; } static async Task<int> MyMethod() { for (int i = 0; i < 5; i++) { Console.WriteLine("Async start:" + i.ToString() + ".."); await Task.Delay(1000); //模拟耗时操作 } return 0; } } }

Use the Wait () and GetAwaiter (). GetResult () method for synchronizing asynchronous execution method

using System;
using System.Threading.Tasks;

namespace AsyncTest
{
    class Program { static void Main(string[] args) { Console.WriteLine("Async Test job:"); Console.WriteLine("main start.."); Console.WriteLine("MyMethod()异步方法同步执行:"); MyMethod().Wait();//在main中等待async方法执行完成 int i = MyMethod().GetAwaiter().GetResult();//用于在main中同步获取async方法的返回结果 Console.WriteLine("i:" + i); Console.WriteLine("main end.."); Console.ReadKey(true); } static async Task<int> MyMethod() { for (int i = 0; i < 5; i++) { Console.WriteLine("Async start:" + i.ToString() + ".."); await Task.Delay(1000); //模拟耗时操作 } return 0; } } }

Guess you like

Origin www.cnblogs.com/feelSku/p/10945793.html