gpt4 book ai didi

c# - 所有任务完成后我的用户界面得到更新

转载 作者:行者123 更新时间:2023-11-30 20:33:27 34 4
gpt4 key购买 nike

查看我下面的代码并告诉我有什么问题,当所有任务完成时我的 UI 正在更新,但根据代码,当我的例程被任务调用时,它应该更新标签值。在我的代码中更改什么。

例如,当 DoSomething1 被调用时,label3 需要更新并反射(reflect)在用户之前。

private void button1_Click(object sender, EventArgs e)
{
TaskScheduler uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
var token = Task.Factory.CancellationToken;

var tasks = new[]
{
Task.Factory.StartNew( DoSomething1,token,TaskCreationOptions.None, uiScheduler),
Task.Factory.StartNew(DoSomething2,token,TaskCreationOptions.None, uiScheduler),
Task.Factory.StartNew(DoSomething3,token,TaskCreationOptions.None, uiScheduler)
};
Task.WaitAll(tasks);
//var things = Task.WhenAll(tasks);

int x=0;
}

public void DoSomething1()
{
//this.label3.BeginInvoke(new Action(() =>
//{
// this.label3.Text = "DoSomething1 -- " + System.Threading.Thread.CurrentThread.ManagedThreadId;
//}));
this.label3.Text = "DoSomething1 -- " + System.Threading.Thread.CurrentThread.ManagedThreadId;
System.Threading.Thread.Sleep(1000);
}

public void DoSomething2()
{
//this.label4.BeginInvoke(new Action(() =>
//{
// this.label4.Text = "DoSomething2 -- " + System.Threading.Thread.CurrentThread.ManagedThreadId;
//}));
this.label4.Text = "DoSomething2 -- " + System.Threading.Thread.CurrentThread.ManagedThreadId;
System.Threading.Thread.Sleep(1000);
}

public void DoSomething3()
{
//this.label5.BeginInvoke(new Action(() =>
//{
// this.label5.Text = "DoSomething3 -- " + System.Threading.Thread.CurrentThread.ManagedThreadId;
//}));
this.label5.Text = "DoSomething3 -- " + System.Threading.Thread.CurrentThread.ManagedThreadId;
System.Threading.Thread.Sleep(1000);
}

最佳答案

The other answer就目前而言是正确的。使用 FromCurrentSynchronizationContext() 会导致正在使用的调度程序始终使用当前线程来运行任务。 IE。用户界面线程。任务应该使用 Task.Run() 运行并且没有特定的调度程序(它将默认为线程池调度程序)。 BeginInvoke() 是正确访问 Label 对象的一个​​选项。

但是,在我看来,最好修复所有代码,使其在当前的 async/await 范例中更加惯用。例如:

private async void button1_Click(object sender, EventArgs e)
{
// Your original example didn't show how you used this value,
// so I don't have anything in this example that uses it. But you
// would probably want to pass it to the "DoSomething..." methods
// so that those methods can then pass the token to whatever async
// operation they are actually doing. See additional comment below.
var token = Task.Factory.CancellationToken;

var tasks = new[]
{
DoSomething1(),
DoSomething2(),
DoSomething3()
};
await Task.WhenAll(tasks);

int x=0;
}

public async Task void DoSomething1()
{
this.label3.Text = "DoSomething1 -- " + System.Threading.Thread.CurrentThread.ManagedThreadId;
// replace with `Task.Run()` or whatever already-asynchronous
// operation you need/want. Likewise in the other methods.
// Don't forget to pass the CancellationToken to these methods,
// and then on to whatever tasks you actually are running.
await Task.Delay(1000);
}

public async Task DoSomething2()
{
this.label4.Text = "DoSomething2 -- " + System.Threading.Thread.CurrentThread.ManagedThreadId;
await Task.Delay(1000);
}

public async Task DoSomething3()
{
this.label5.Text = "DoSomething3 -- " + System.Threading.Thread.CurrentThread.ManagedThreadId;
await Task.Delay(1000);
}

关于c# - 所有任务完成后我的用户界面得到更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40195965/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com