Following is the code for my ThreadSafe function
public static void ThreadSafe(Action action)
{
System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
new MethodInvoker(action));
}
I am using this ThreadSafe
function to access various cross-thread actions. Its working fine in other classes but when I am adding a user control Comment
in panel commentContainer
, its showing following error.
Cross-thread operation not valid exception
Following its screenshot with the code:
I want to pass the comment
string to this control. The control processing is based on this value.
I think my way of using this function is wrong. Can any one please improve my code if its not correct? And if correct then please tell me what to do to solve this issue?
Also note that when I removed the foreach
loop, the background worker is working fine.
setTopicInfo()
code:
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.] | "; }
}