以下是我的 ThreadSafe 函数的代码
public static void ThreadSafe(Action action)
{
System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
new MethodInvoker(action));
}
我正在使用此ThreadSafe
函数来访问各种跨线程操作。它在其他类中工作正常,但是当我Comment
在面板中添加用户控件时commentContainer
,它显示以下错误。
跨线程操作无效异常
按照它的屏幕截图和代码:
我想通过comment
字符串传递给这个控件。控制处理基于该值。我认为我使用此功能的方式是错误的。如果我的代码不正确,任何人都可以改进我的代码吗?如果正确,请告诉我如何解决这个问题?
另请注意,当我删除foreach
循环时,后台工作人员工作正常。
setTopicInfo()
代码:
public void setTopicInfo()
{
try
{
TopicInfo.Text = "Topic Views: " + dops.getVisiteds(tid) + " | " +
"People Involved: " + dops.getPeopleInvolved(tid) + " | ";
}
catch (Exception)
{ TopicInfo.Text = "[Some error occured while loading Topic Information. Please reopen or refresh this topic.] | "; }
}