使用TaskScheduler.FromCurrentSynchronizationContext任务更新UI [英] update UI in Task using TaskScheduler.FromCurrentSynchronizationContext

查看:1077
本文介绍了使用TaskScheduler.FromCurrentSynchronizationContext任务更新UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加一些文字使用到列表框中工作和我简单地用一个按钮,发生在click事件代码:

 的TaskScheduler uiScheduler = TaskScheduler.FromCurrentSynchronizationContext(); 
Task.Factory.StartNew(()=>
{
的for(int i = 0;我小于10;我++)
{
listBox1.Items 。新增(问题中的一些城市=+ i.ToString());
System.Threading.Thread.Sleep(1000);
}
},CancellationToken.None,TaskCreationOptions。无,uiScheduler);



但它不工作和UI锁定直至月底循环。



问题出在哪里。



感谢:?)


解决方案

在哪里的问题?




那么你明确地说你要执行在UI线程的...然后你在任务中沉睡任务的,所以它的阻塞UI线程。你是怎么想到要在UI线程,但 Thread.sleep代码的出现问题了?



如果你可以使用C#5和异步​​/的await,这将使事情变得更加简单:

 私人静态异步任务ShowCitiesAsync()
{
的for(int i = 0;我小于10;我++)的问题
{
listBox1.Items.Add(数字城市= + I);
等待Task.Delay(1000);
}
}

如果您不能使用C#5(的建议通过您的代码),这是显著棘手。你可能会最好关闭使用定时

  //注意:你可能想System.Windows.Forms.Timer,使其
//会自动闪光UI线程。
定时器定时器=新定时器{间隔= 1000; }
INT I = 0;
timer.Tick + =代表
{
listBox1.Items.Add(问题中的一些城市=+ I);
I ++;
如果(我== 10)
{
timer.Stop();
timer.Dispose();
}
};
timer.Start();



正如你所看到的,这是很丑陋......它假定你不想竟做任何的工作UI更新之间



另一个方法是模拟长时间运行在不同的线程任务(目前睡觉)使用 的BackgroundWorker ,并使用 ReportProgress 回来到UI线程来添加列表项。


I want to add some text to list box using Task and I simply use a button and place in click event this code:

TaskScheduler uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
Task.Factory.StartNew(() =>
{
    for (int i = 0; i < 10; i++)
    {
        listBox1.Items.Add("Number cities in problem = " + i.ToString());
        System.Threading.Thread.Sleep(1000);
    }
}, CancellationToken.None, TaskCreationOptions.None, uiScheduler);

but it does not work and UI locked until the end of the for loop.

Where is the problem ?

thanks :)

解决方案

where is problem?

Well you're explicitly saying that you want to execute the task in the UI thread... and then you're sleeping within the task, so it's blocking the UI thread. How did you expect to be in the UI thread, but for Thread.Sleep not to cause a problem?

If you can use C# 5 and async/await, that would make things much easier:

private static async Task ShowCitiesAsync()
{
    for (int i = 0; i < 10; i++)
    {
        listBox1.Items.Add("Number cities in problem = " + i);
        await Task.Delay(1000);
    }
}

If you can't use C# 5 (as suggested by your tags), it's significantly trickier. You might be best off using a Timer:

// Note: you probably want System.Windows.Forms.Timer, so that it
// will automatically fire on the UI thread.
Timer timer = new Timer { Interval = 1000; }
int i = 0;
timer.Tick += delegate
{
    listBox1.Items.Add("Number cities in problem = " + i);
    i++;
    if (i == 10)
    {
        timer.Stop();
        timer.Dispose();
    }
};
timer.Start();

As you can see, it's pretty ugly... and it assumes you don't want to actually do any work between UI updates.

Another alternative would be to simulate your long-running task (sleeping at the moment) on a different thread using BackgroundWorker, and use ReportProgress to come back to the UI thread to add the list item.

这篇关于使用TaskScheduler.FromCurrentSynchronizationContext任务更新UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆