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 concept of async and threading in C #

2025-03-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of what the concept of asynchronism and threading of C# is, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on the concept of asynchronism and threading in C#. Let's take a look.

Recently I wrote a system in C # (Framework 2. 0), which uses a lot of async and threads. When I use exceptions or threads to operate and update control information, I will prompt an exception message: "the operation between threads is invalid, and it is never the thread that created the control to access the control." I have encountered this problem before, and finally solved it with Invoke. But when you encounter the same problem this time, the solution uses the attribute Control.CheckForIllegalCrossThreadCalls, which is set to false; to view the notes of MSDN, as explained below:

Accessing Windows forms controls is not thread-safe in nature. If two or more threads manipulate the state of a control, it may force the control into an inconsistent state. There may also be other thread-related bug, including race conditions and deadlocks. It is important to ensure that the control is accessed in a thread-safe manner.

The .NET Framework helps detect this problem when accessing controls in a non-thread-safe manner. When running an application in the debugger, if a thread other than the thread that created the control attempts to call the control, the debugger raises an InvalidOperationException and prompts the message: "it is never accessed by the thread that created the control control name."

This exception occurs reliably during debugging and in some cases at run time. It is strongly recommended that you fix this problem when this error message is displayed. This exception may occur when debugging applications written in the .NET Framework prior to .NET Framework version 2.0.

Be careful

You can disable this exception by setting the value of the CheckForIllegalCrossThreadCalls property to false. This causes the control to run in the same way as under Visual Studio 2003.

From the above explanation, in fact, asynchrony is based on the delegated operation method, with the thread mechanism to complete. Of course, on the surface, we can understand it in this way, but what is the specific implementation mechanism of the two, we still need to explore in depth. In terms of programming experience, there are similarities and differences in the use of the two:

Similarities:

1) they can all do tasks to perform, so that there will be no fake death in the software world.

2) when operating controls in exceptions and threads, if there is no special handling, there will be "invalid inter-thread operations"

Never the thread that creates the control accesses the control exception

Differences:

1) Asynchronous is a task at a certain point in time; a thread can be used as a real-time task.

2) if you see the thread asynchronously, you can pass parameters to the thread at the beginning of the thread.

The quantity parameter comes from the global variable of the object.

3) the asynchronous end uses the callback function to release resources without human intervention, while the thread is used as a real-time task.

I need more control in terms of resource control.

4) Asynchronous is easier to control; threads need more detailed knowledge when controlling.

This is the end of the article on "what is the concept of async and threading in C#". Thank you for reading! I believe you all have a certain understanding of "what is the concept of async and threading in C#". If you want to learn more, you are welcome to follow the industry information channel.

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

Internet Technology

Wechat

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

12
Report