Home > Net >  Asynchronous (async) open a separate thread, isn't it?
Asynchronous (async) open a separate thread, isn't it?

Time:04-06

Qingming festival under the electricity charge, learning under the asynchronous and multi-threaded,

Put a form button, a text box, after the test, the following code is not enough;
 
Private void button1_Click (object sender, EventArgs e)
{
Console. WriteLine ($" of the current main Thread ID is {Thread. CurrentThread. ManagedThreadId} ");
for (int i=0; I & lt; 100; I++)
{
Console. WriteLine ($" current ID is {I} {Thread. CurrentThread. ManagedThreadId} ");
Thread.sleep (100);
Task. Run (()=& gt;
{
TextBox1Set (I);
});
}
Console. WriteLine ($" over ");
}

Public void textBox1Set (int I)//for the current and subsequent writing log
{
Console. WriteLine ($" the current textBox1SetID {I} {Thread. CurrentThread. ManagedThreadId} ");
TextBox1. Invoke ((EventHandler delegate)
{
TextBox1. Text=I.T oString ();
Console. WriteLine ($" currently {I} textBox1SetID the BeginInvoke is {Thread. CurrentThread. ManagedThreadId} ");
});
}


Must use asynchronous method, combined with async and await, as follows:
 
Private async void button1_Click (object sender, EventArgs e)
{
Console. WriteLine ($" of the current main Thread ID is {Thread. CurrentThread. ManagedThreadId} ");
for (int i=0; I & lt; 100; I++)
{
Console. WriteLine ($" current ID is {I} {Thread. CurrentThread. ManagedThreadId} ");
Thread.sleep (100);
Await Task. Run (()=& gt;
{
TextBox1Set (I);
});
}
Console. WriteLine ($" over ");
}

Public void textBox1Set (int I)//for the current and subsequent writing log
{
Console. WriteLine ($" the current textBox1SetID {I} {Thread. CurrentThread. ManagedThreadId} ");
TextBox1. Invoke ((EventHandler delegate)
{
TextBox1. Text=I.T oString ();
Console. WriteLine ($" currently {I} textBox1SetID the BeginInvoke is {Thread. CurrentThread. ManagedThreadId} ");
});
}


And my question is, with the asynchronous, not is equivalent to the other opened a thread, and the form of the main thread should be different, as the chart

Please master, thank you,

CodePudding user response:

The great god, the most like to do is unilateral forget output,
They forgot to tell you what is the full name of async/await called asynchronous I/o and waiting for

So he doesn't have anything to do with threads, but their task on unilateral output of the time like to drag, so with thread hung up the side, because the task is updating the thread, async not, async is asynchronous I/o upgrade package

CodePudding user response:

From computer in principle, the thread is concentrated northbridge, CPU use CPU,
IO occurrence, without the use of CPU, that is to say, in principle, wait for an IO event, originally will not consume CPU, also need not thread, he just simply waiting for an IO signal

The great god tell you, you can wait for rice cooker cooking, side to wash dishes, chopping vegetables,

Something that's right, you can let the CPU to cooking, just forget to tell you, asynchronous waiting for itself is not opened a thread, he just waiting for the CPU itself after completion of the cooking thread to IO events, initiate a cook you complete you actually waiting for just the IO events, rather than cooking thread

CodePudding user response:

Task is good encapsulation of thread pool for the system, but also a variety of language supports standard "language", for example, you need 1 million relatively independent tasks, but in practice due to various reasons, in fact, as long as the reuse in the process of 15 child thread is enough, it's just use simple Task concepts to programming, you need to "think" logically 100000 threads, thread and actually use only 15, so is not to say that the asynchronous statement of different parts of the code (method) must be running in different threads,

This Task is not operating system concept, but the.net in mechanism,

"Asynchronous" is a logical concept, use "statement" in the form of the input output is separated into two parts, you can think it is the prototype of all "callback, event" concept,

Async/await is an asynchronous statement syntax, so the c # compiler will know how to write a thread pool code,

"Await a Task object" such code is not blocking threads, but release the current thread, this is an asynchronous programming -- use the async/await implementation - with the past, the difference in using thread blocking the way of programming when you use a blocking the way of thinking, so if you want to do 1 million independent tasks, then you really have to create 1 million threads, it needs 1000 nuclear CPU and memory of the future computer 100 t, and the Task is lightweight,

CodePudding user response:

refer to the second floor wanghui0380 response:
from computer in principle, the thread is concentrated northbridge, CPU use CPU,
IO occurrence, without the use of CPU, that is to say, in principle, wait for an IO event, originally will not consume CPU, also need not thread, he just simply waiting for an IO signal

The great god tell you, you can wait for rice cooker cooking, side to wash dishes, chopping vegetables,

Something that's right, you can let the CPU to cooking, just forget to tell you, asynchronous waiting for itself is not opened a thread, he just waiting for the CPU itself after completion of the cooking thread to IO events, initiate a cook you complete you actually waiting for is the IO events, not cooking thread

Thank you, to tell the truth not to understand, but at least let me I know not multi-threaded asynchronous,

CodePudding user response:

You can run the demo:
 using System; 
Using System. The Threading;
Using System. The Threading. The Tasks;

The namespace ConsoleApp1
{
Class Program
{
The static void Main (string [] args)
{
Console. WriteLine ($" start, the thread id={thread. CurrentThread. ManagedThreadId} ");
The test ();
Console. WriteLine ($" the main thread after the test, the thread id={thread. CurrentThread. ManagedThreadId} ");
Console. WriteLine (" press any key to end... ");
Console.ReadKey();
}

Private static async void test ()
{
Console. WriteLine ($" test, the thread id={thread. CurrentThread. ManagedThreadId} ");
Var task=task. Run (async ()=& gt;
{
int cnt=0;
While (cnt++ & lt; 20)
{
Await Task. Delay (1000);
Console. WriteLine ($", while the thread id={thread. CurrentThread. ManagedThreadId} ");
}
});
Await Task. WhenAll (Task);//await execution of the statement did not block 4
Console. WriteLine ($" while finish, thread id={thread. CurrentThread. ManagedThreadId} ");
}
}
}

In my here it output
, thread id=1 
The test, the thread id=1
The main thread after the test, the thread id=1
Press any key...
While the thread id=4
While the thread id=4
While the thread id=4
While the thread id=7
While the thread id=4
While the thread id=4
While the thread id=4
While the thread id=4
While the thread id=4
While the thread id=6
While the thread id=6
While the thread id=4
While the thread id=4
While the thread id=6
While the thread id=6
While the thread id=6
While the thread id=7
While the thread id=7
While the thread id=7
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related