我的代码可以改变导航条项目的标题:
OverviewOrgan_NavBarHelper cNavBarHelper = new OverviewOrgan_NavBarHelper(Organization);
foreach (NavBarItem item in GetNavigationBar.Items)
{                       
    string cCaption = cNavBarHelper.UpdateNavBarItemCaption(item);
    item.Caption = cCaption;
}现在它在主线程上,但我必须将它移到另一个线程。因为我知道UI不应该从另一个威胁中改变,所以它被创建了,所以我考虑使用BackgroundWorker。事实上,我不是在使用线程,有人能为我的任务提供最佳解决方案吗?
发布于 2013-01-04 18:46:48
have a look at the first answer in this thread.它解释了如何使用BackgroundWorker。
但问题是,导航栏的项目是在UI-Thread中创建的(假设您使用的是WPF),并且不能从另一个线程操作这些项目。
为什么你必须在另一个线程中做呢?
发布于 2013-01-04 18:24:57
您可以使用Invoke从另一个线程更新UI。
看看这篇文章:How to update the GUI from another thread in C#?
例如:
MethodInvoker NavBarItemInvoker = (delegate
{
   OverviewOrgan_NavBarHelper cNavBarHelper = new OverviewOrgan_NavBarHelper(Organization);
   foreach (NavBarItem item in GetNavigationBar.Items)
   {                       
       string cCaption = cNavBarHelper.UpdateNavBarItemCaption(item);
       item.Caption = cCaption;
   }
});
if (InvokeRequired)
{
   Invoke(NavBarItemInvoker);
}
else
{
    NavBarItemInvoker();
}发布于 2013-01-04 18:27:32
我想你应该用一个调度程序
在本文中阅读更多关于它的内容:http://msdn.microsoft.com/en-us/magazine/cc163328.aspx
https://stackoverflow.com/questions/14155257
复制相似问题