Jump to content

Recommended Way to execute on UI Thread


ArgusMagnus
 Share

Recommended Posts

I know I read the solution somewhere some time ago but I can't remember where..

 

What is the recommended way to execute a method on the UI Thread, e.g. for showing a MessageBox?

 

Im currently going with

Process process = Process.GetCurrentProcess();
Form form = Form.FromHandle(process.MainWindowHandle) as Form;
form?.Invoke(new Action(() =>
{
    MessageBox.Show("Some Message");
}));

But this seems a bit ugly..

 

My batch Image Processor: https://imagenator.codeplex.com

Link to comment
Share on other sites

void ShowMessage(string msg, string caption)
{
    PaintDotNet.Threading.PdnSynchronizationContext.Instance.Send(
        new System.Threading.SendOrPostCallback(delegate(object state)
        {
            // This line runs on the UI thread and not on the Render thread
            System.Windows.Forms.MessageBox.Show(msg, caption);
        }), null);
}

 

  • Upvote 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...