Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between async and multithreading in C #

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "what is the difference between async and multithreading in C#". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. differences and connections

What's the difference between async and multithreading? In fact, asynchronism is the goal, and multithreading is the way to achieve this. Async means that after An initiates an operation (usually a time-consuming operation, there is no need for asynchrony if it is not time-consuming), it can continue to deal with its own business without waiting for the time-consuming operation to return. This asynchronous programming model in .net simplifies multithreaded programming, and we can do an asynchronous operation without even caring about the Thread class.

Async sometimes uses ordinary threads and sometimes uses the system's asynchronous invocation function. Some IO operations are also asynchronous, but do not necessarily require a thread to run. For example: the hardware has DMA function, when calling DMA to transfer data, CPU does not need to perform processing, just need to initiate the transmission and wait for the end of the transmission. Specific to the. Net platform, such as Socket's BeginSend, if it is running on a platform after Windows 2000, the asynchronous completion port will be called at the bottom to send it.

Asynchronous execution in .net actually uses asynchronous delegates. The method to be executed by the asynchronous delegate is submitted to the thread pool of. Net, and the thread in the thread pool executes the asynchronous method.

II. Scope of application

It is more appropriate to use asynchronous operations when you need to perform an Iwhite O operation. ICompo operation includes not only direct reading and writing of files and networks, but also cross-process calls such as database operations, Web Service, HttpRequest and. Net Remoting.

The scope of application of threads is the kind of situations that require long-time CPU operations, such as time-consuming graphics processing and algorithm execution.

Third, an example of async

As you probably know, using delegate can "automatically" enable a method to be called asynchronously. Intuitively, I think the compiler or CLR uses another thread to execute the target method. Is this the case or not? Let's prove it with a piece of code.

Delegate void AsyncFoo (int I); static void Main (string [] args) {PrintCurrThreadInfo ("Main ()"); for (int I = 0; I < 10; iTunes +) {PostAsync ();} Console.ReadLine () } / output the current thread's information / method name static void PrintCurrThreadInfo (string name) {Console.WriteLine ("ThreadId of" + name + "is:" + Thread.CurrentThread.ManagedThreadId + ", current thread is" + (Thread.CurrentThread.IsThreadPoolThread? "": "not") + "thread pool thread.") } / deliver an asynchronous call / static void PostAsync () {AsyncFoo caller = new AsyncFoo (Foo); caller.BeginInvoke (1000, new AsyncCallback (FooCallBack), caller);} / Test method, Sleep certain time / Sleep time static void Foo (int I) {PrintCurrThreadInfo ("Foo ()"); Thread.Sleep (I);} static void FooCallBack (IAsyncResult ar) {PrintCurrThreadInfo ("FooCallBack ()") AsyncFoo caller = (AsyncFoo) ar.AsyncState; caller.EndInvoke (ar);}

The output of this code is as follows:

Thread Id of Main () is: 1, current thread is not thread pool thread.Thread Id of Foo () is: 3, current thread is thread pool thread.Thread Id of FooCallBack () is: 3, current thread is thread pool thread.Thread Id of Foo () is: 3, current thread is thread pool thread.Thread Id of Foo () is: 4, current thread is thread pool thread.Thread Id of Foo () is: 5, current thread is thread pool thread.Thread Id of FooCallBack () is: 3, current thread is thread pool thread.Thread Id of Foo () is: 3 Current thread is thread pool thread.Thread Id of FooCallBack () is: 4, current thread is thread pool thread.Thread Id of Foo () is: 4, current thread is thread pool thread.Thread Id of Foo () is: 6, current thread is thread pool thread.Thread Id of FooCallBack () is: 5, current thread is thread pool thread.Thread Id of Foo () is: 5, current thread is thread pool thread.Thread Id of Foo () is: 7, current thread is thread pool thread.Thread Id of FooCallBack () is: 3 Current thread is thread pool thread.Thread Id of Foo () is: 3, current thread is thread pool thread.Thread Id of FooCallBack () is: 4, current thread is thread pool thread.Thread Id of FooCallBack () is: 6, current thread is thread pool thread.Thread Id of FooCallBack () is: 5, current thread is thread pool thread.Thread Id of FooCallBack () is: 7, current thread is thread pool thread.Thread Id of FooCallBack () is: 3, current thread is thread pool thread.

As you can see from the output, the asynchronous calls that A. net uses delegate to "automatically" generate are using a different thread (and a thread pool thread).

This is the end of the content of "what's the difference between async and multithreading in C#". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report