从其他线程更新GUI [英] Updating GUI from other thread

查看:123
本文介绍了从其他线程更新GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从另一个线程更新列表框,什么是实现这一目标的最简单的方法是什么?

我试图调用文本框,但没有奏效。

 私人无效的DoWork()
{
    TcpClient的客户端=新的TcpClient();
    client.Connect(192.168.1.3,10);
    StreamWriter的作家=新的StreamWriter(client.GetStream());
    StreamReader的读者=新的StreamReader(client.GetStream());

    JObject O =新JObject();
    o.Add(COMANDO,1);
    o.Add(目录,@C:\用户\克莱恩\桌面\ Acionamentos);
    writer.Write(o.ToString());
    writer.Flush();
    JArray阵列= JArray.Parse(reader.ReadToEnd());
    的for(int i = 0; I< array.Count;我++)
    {
        listBox1.Items.Add(数组[我]); //更新此线程GUI
    }
}

私人无效的button1_Click(对象发件人,EventArgs的)
{
    线程t =新主题(DoWork的);
    t.Start();
}
 

解决方案

我会用一个的BackgroundWorker ,你可以做你的异步的东西,在的DoWork ,使用报告进度 bgworker.ReportProgress(),你会得到回调 ProgressChanged (我称之为ReportProgress为处理每一个元素),那么你就可以更新您的GUI控件。

该工作人员还可以火 RunWorkerCompleted 何时结束。

  YourType arrayitem;

私人无效backgroundWorker1_DoWork(对象发件人,DoWorkEventArgs E)
{
    TcpClient的客户端=新的TcpClient();
    client.Connect(192.168.1.3,10);
    StreamWriter的作家=新的StreamWriter(client.GetStream());
    StreamReader的读者=新的StreamReader(client.GetStream());

    JObject O =新JObject();
    o.Add(COMANDO,1);
    o.Add(目录,@C:\用户\克莱恩\桌面\ Acionamentos);
    writer.Write(o.ToString());
    writer.Flush();
    JArray阵列= JArray.Parse(reader.ReadToEnd());

    INT百分比;

    的for(int i = 0; I< array.Count;我++)
    {
        arrayitem =数组[我]
        百分比=(第(i + 1)* 100)/array.Count;
        backgroundWorker1.ReportProgress(百分比);
    }
}

私人无效backgroundWorker1_ProgressChanged(对象发件人,ProgressChangedEventArgs E)
{
    listBox1.Items.Add(arrayitem);
    progressBar1.Value = e.ProgressPercentage; //如果你想添加进度
}

私人无效backgroundWorker1_RunWorkerCompleted(对象发件人,RunWorkerCompletedEventArgs E)
{
    label1.Text =完成;
}
 

请记住你的BackgroundWorker必须在 WorkerReportProgress 属性真。

I'm trying to update a listbox from another thread, what is the simplest way to achieve this?

I tried Invoking the textbox, but it didn't work.

private void dowork()
{
    TcpClient client = new TcpClient();
    client.Connect("192.168.1.3", 10);
    StreamWriter writer = new StreamWriter(client.GetStream());
    StreamReader reader = new StreamReader(client.GetStream());

    JObject o = new JObject();
    o.Add("comando", 1);
    o.Add("dir", @"C:\Users\klein\Desktop\Acionamentos");
    writer.Write(o.ToString());
    writer.Flush();
    JArray array = JArray.Parse(reader.ReadToEnd());
    for (int i = 0; i < array.Count; i++)
    {
        listBox1.Items.Add(array[i]);  //update GUI from this thread
    }
}

private void button1_Click(object sender, EventArgs e)
{
    Thread t = new Thread(dowork);
    t.Start();
}

解决方案

I would use a BackgroundWorker, you can do your async stuff in DoWork, report your progress using bgworker.ReportProgress() and you will get the callback in ProgressChanged (I would call ReportProgress for every element processed), then you will be able to update your GUI controls.

The worker can also fire RunWorkerCompleted when it ends.

YourType arrayitem;

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    TcpClient client = new TcpClient();
    client.Connect("192.168.1.3", 10);
    StreamWriter writer = new StreamWriter(client.GetStream());
    StreamReader reader = new StreamReader(client.GetStream());

    JObject o = new JObject();
    o.Add("comando", 1);
    o.Add("dir", @"C:\Users\klein\Desktop\Acionamentos");
    writer.Write(o.ToString());
    writer.Flush();
    JArray array = JArray.Parse(reader.ReadToEnd());

    int percentage;

    for (int i = 0; i < array.Count; i++)
    {
        arrayitem = array[i];
        percentage = ((i + 1)*100)/array.Count;
        backgroundWorker1.ReportProgress(percentage);
    }
}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    listBox1.Items.Add(arrayitem);
    progressBar1.Value = e.ProgressPercentage; // in case you'd want to add a progressbar
}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    label1.Text = "Done";
}

Keep in mind your backgroundworker must have true in the WorkerReportProgress property.

这篇关于从其他线程更新GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆