开发者

Thread safe form manipulation between two forms (WinForms C#)

I have two forms, the main form and one that pops up as a modal dialog. From a process spawned in the main form, I want to dynamically update the text on the modal dialog. Here's what I have:

In the main form, I do this:

// show the wait modal
            var modal = new WaitDialog { Owner = this };

            // thread the packaging
            var thread = new Thread(() => Packager.PackageUpdates(clients, version, modal));
            thread.Start开发者_运维问答();

            // hopefully it worked ...
            if (modal.ShowDialog() != DialogResult.OK)
            {
                throw new Exception("Something failed, miserably.");
            }

The PackageUpdates method takes the modal dialog, and does this:

 // quick update and sleep for a sec ...
             modal.SetWaitLabelText("Downloading update package...");
             Thread.Sleep(2000);

             modal.SetWaitLabelText("Re-packaging update...");

To be thread safe, I do this in the modal dialog:

public void SetWaitLabelText(string text)
        {
            if (lblWaitMessage.InvokeRequired)
            {
                Invoke(new Action<string>(SetWaitLabelText), text);
            }
            else
            {
                lblWaitMessage.Text = text;
            }
        }

Everything works great ... most of the time. Every three or four times that the modal pops up, I get an exception on the lblWaitMessage.Text = text; and it's not invoking the command.

Am I missing something in this setup?


Like @Hans Passant pointed out, you should wait for the modal.Load-event. One good option is to use the ManualResetEvent to inform your thread to wait until that happens.

The WaitOne method will block the thread until the Set method is called. Here's a very simple setup which should do the trick.

public partial class Form1 : Form
{
    ManualResetEvent m_ResetEvent;

    public Form1()
    {
        InitializeComponent();

        m_ResetEvent = new ManualResetEvent(false);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Dialog d = new Dialog { Owner = this, ResetEvent = m_ResetEvent };

        var thread = new Thread(new ParameterizedThreadStart(DoSomething));
        thread.Start(d);

        if (d.ShowDialog() != System.Windows.Forms.DialogResult.OK)
        {
            throw new Exception("Something terrible happened");
        }

    }

    private void DoSomething(object modal)
    {
        Dialog d = (Dialog)modal;     

        // Block the thread!
        m_ResetEvent.WaitOne();

        for (int i = 0; i < 1000; i++)
        {
            d.SetWaitLabelText(i.ToString());
            Thread.Sleep(1000);
        }
    }
}

And here is the modal form

public partial class Dialog : Form
{
    public Form Owner { get; set; }

    public ManualResetEvent ResetEvent { get; set; }

    public Dialog()
    {
        InitializeComponent();
    }

    public void SetWaitLabelText(string text)
    {
        if (label1.InvokeRequired)
        {
            Invoke(new Action<string>(SetWaitLabelText), text);
        }
        else
        {
            label1.Text = text;
        }
    }

    private void Dialog_Load(object sender, EventArgs e)
    {
        // Set the event, thus unblocking the other thread
        ResetEvent.Set();
    }
}


I think you should rewrite the code to let thread.Start() isn't called before modal.ShowDialog().

As a workaround, you can try this:

public void SetWaitLabelText(string text) {
    Invoke(new Action<string>(SetWaitLabelText2), text);
}

void SetWaitLabelText2(string text) {
    lblWaitMessage.Text = text;
}

The first method always uses Invoke, regardless the value of InvokeRequired. The second method actually does the thing. This pattern is usable when you always call the function from another thread.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜