Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I implemented something very similar to this for another project. This form allows you to popup a modal dialog from within a worker thread:</p> <pre><code>public partial class NotificationForm : Form { public static SynchronizationContext SyncContext { get; set; } public string Message { get { return lblNotification.Text; } set { lblNotification.Text = value; } } public bool CloseOnClick { get; set; } public NotificationForm() { InitializeComponent(); } public static NotificationForm AsyncShowDialog(string message, bool closeOnClick) { if (SyncContext == null) throw new ArgumentNullException("SyncContext", "NotificationForm requires a SyncContext in order to execute AsyncShowDialog"); NotificationForm form = null; //Create the form synchronously on the SyncContext thread SyncContext.Send(s =&gt; form = CreateForm(message, closeOnClick), null); //Call ShowDialog on the SyncContext thread and return immediately to calling thread SyncContext.Post(s =&gt; form.ShowDialog(), null); return form; } public static void ShowDialog(string message) { //Perform a blocking ShowDialog call in the calling thread var form = CreateForm(message, true); form.ShowDialog(); } private static NotificationForm CreateForm(string message, bool closeOnClick) { NotificationForm form = new NotificationForm(); form.Message = message; form.CloseOnClick = closeOnClick; return form; } public void AsyncClose() { SyncContext.Post(s =&gt; Close(), null); } private void NotificationForm_Load(object sender, EventArgs e) { } private void lblNotification_Click(object sender, EventArgs e) { if (CloseOnClick) Close(); } } </code></pre> <p>To use, you'll need to set the SyncContext from somewhere in your GUI thread:</p> <pre><code>NotificationForm.SyncContext = SynchronizationContext.Current; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload