Home > Net >  Multithreaded change Ui interface
Multithreaded change Ui interface

Time:04-06

The following process is the process of a code, I
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} ");

List TaskList=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:

refer to the second floor wanghui0380 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
,,,,,,,,,, I Cancel


Thank you very much, I figure, as the problem itself, you give I pointed out the fundamental reason, that is my own code has a problem, the time-consuming process (thread.sleep (100);) I put the wrong position, causing the multithreading has run out, did not end the Ui, so definitely show the final result of 49, as a solution, in fact as long as I keep this process a change of position is not to go, as follows, have the tests pass, satisfies the requirement of I
 
Public async Task Do ()
{
Console. WriteLine ($" of the current main Thread ID is {Thread. CurrentThread. ManagedThreadId} ");

List TaskList=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} ");

TaskList. Add (Task. Factory. StartNew (t=& gt;
{
Thread.sleep (100);
TextBox1Set (Convert ToInt32 (t));
}, I, CancellationToken. None, TaskCreationOptions. None, the scheduler));
}
Await Task. WhenAll (TaskList. ToArray ());
Console. WriteLine ($" over ");
}


As the extensions, I can't understand, why do you want to put the "task. Factory. Startnew" instead of "task. Run", although you explained (to prevent the waitall don't you into problems), but still don't understand,
Also, why the "thread.sleep (1000);" Instead of "asycn await Task. Delay (2000)"
I use above also quite good, isn't it?

Thank you again!

CodePudding user response:

The last question is this,

Front said, is the essence of the asynchronous IO wait, IO wait for not consume CPU, IO operation theory and the thread is the essence of "CPU concurrent" belongs to the need to consume CPU, so we just choose not to consume CPU will not consume CPU,

Because await operating at the same time, so using the run, because the run is simplifying startnew encapsulation, his internal actually adapted into await task This way, at the same time also rewrite the launch parameters, so will not affect waitall behind you

  •  Tags:  
  • C#
  • Related