Generally means a text box, want to open a few multi-threaded tasks, after completion of each task is displayed in the text box at the same time,
But now the result is a text box didn't show, show only the last result, 49, and I demand from 1 to 49 rolling (of course, I also know that is not necessarily in order to),
Private async void button1_Click (object sender, EventArgs e)
{
Await the Do ();//
Console. WriteLine (" all done ");
}
Public async Task Do ()
{
Console. WriteLine ($" of the current main Thread ID is {Thread. CurrentThread. ManagedThreadId} ");
ListTaskList=new List (a);
Var scheduler=new LimitedConcurrencyLevelTaskScheduler (70);
for (int i=0; I & lt; 50; I++)
{
Console. WriteLine ($" current ID is {I} {Thread. CurrentThread. ManagedThreadId} ");
Thread.sleep (100);
TaskList. Add (Task. Factory. StartNew (t=& gt;
{
//insert here a lot of time consuming process
TextBox1Set (Convert ToInt32 (t));
}, I, CancellationToken. None, TaskCreationOptions. None, the scheduler));
}
Await Task. WhenAll (TaskList. ToArray ());
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} ");
});
}
Online search along while, generally understand the reason is that multiple threads up the UI (specific also don't understand what reason)
I want to "await the Do ();//"to await Task. Run (()=& gt; {Do (); });
Has a prompt "this call won't wait"; Can't use, please expert advice,
CodePudding user response:
Private class UISync{
Private static ISynchronizeInvoke Sync;
Public static void Init (ISynchronizeInvoke sync)
{
Sync=Sync;
}
Public static void the Execute (Action Action)
{
Sync. The BeginInvoke (action, null);
}
}
Void XXXXX (object sender, EventArgs e)
{
UISync. Execute (()=& gt; XXXXXXXXXXXXXXXXXXX);
}
CodePudding user response:
Thread.sleep (100);Please remove the this first, because it is as you say, a lot of time consuming action "simulation", but actually code behind you wouldn't take
And every time you to a sleep cycles is the main thread to sleep, but after the face task have been completed, so the end result is actually the son is to complete the task early to 49, and your main Cheng Cai refresh,
You a post as we've said before, if less than the other start a task, he is just a IO wait, he won't fuck start a new thread, so this sleep is for the main thread actually sleep
Do you want to achieve the purpose of you, please write
TaskList. Add ( Task. Run asycn t=& gt;
{
//insert here a lot of time consuming process
Await Task. Delay (2000);//to this Task, he would enter the thread pool, the time-consuming action at the same time, the simulation in this Task, of course, here with the Task. The run is to prevent the waitall don't you to the problem, we know the Task. Factory. Startnew default without context, especially with await asynchronous
TextBox1Set (Convert ToInt32 (t));
}, I Cancel
CodePudding user response: