I am trying to display error message notification using MessageBox.Show() method. But I am getting Cross thread operation issue. I used the below code. How can I resolve the cross thread issue? I tried with MethodInvoker but it is not solving my issue. Kindly suggest me the guidelines to resolve this issue.
public static class Notification()
{
public static void ShowErrorMessage(IWin32Window owner, String msg)
{
MessageBox.Show(owner, msg, Caption+ " - " + "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Issue :
Cross-thread operation not valid:Control 'MainPage' accessed from a thread other than the thread it was created on.
It seems there is an answer here MessageBox.show() is it not safe to call in worker thread? , though the question is a bit misleading.
Also, this Microsoft documentation page explains in detail how to make thread-safe calls to WinForm controls, and provides you with code examples.