开发者

Reading value from other thread.

I created a thread. How can I read values of variables from GUI thread?

I know how to change them. I don't know how to read them.

This is what I'm using for changing things on GUI thread:

    public void Log(string message)
    {
        MethodInvoker m = () => { Log_textBox.Text += message; };
        if (InvokeRequired)
        {
            BeginInvoke(m);
        }
        else
        {
            Invoke(m开发者_开发技巧);
        }
    }

I need to read some values from GUI thread:

    public void StartBot()
    {
        Klient.StartBot(selectedType, (int)nb_count.Value, nb_nonstop.Checked, (...)int.Parse(extra_set.SelectedItem.ToString()));
    }

    private void StartStopButton_Click(object sender, EventArgs e)
    {
            Thread questThread = new Thread(StartBot);
            Klient.RequestToStop = false;
            questThread.Start();
    }

I'm getting cross thread operation error on line with Klient.StartBot(...) argument list.

How to fix it?


You can return a value from an invoked delegate by creating a Func<ReturnType>.
Invoke will return the function's return value.

Alternatively, you can set a local variable inside the delegate and call it using Invoke (not BeginInvoke, since that won't wait for it to finish)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜