I'm getting a stackverflow error while executing InvokeRequired.
System.StackOverflowException was unhandled
How to fix it? There is no info i View Details.
FIXED VERSION:
public DialogResult ShowMessage(string msg, string caption, MessageBoxButtons buttons)
{
if (InvokeRequired)
{
Func<DialogResult> m = () => MessageBox.Show(msg, caption, buttons);
return (DialogResult)Invoke(m);
}
else
{
return MessageBox.Show(msg, caption, buttons);
}
}
It's because when
InvokeRequired
is true, you call the exact same method again and again. You need to useInvoke
in order to get the method scheduled to run on the UI thread. In this case,InvokeRequired
will be false, and your code will run into theif
branch where you actually show the dialog.Change your code to something along the lines of: