C# 表单控件不会使用多线程更新 [英] C# Form Controls Won't Update with Multithreading

查看:35
本文介绍了C# 表单控件不会使用多线程更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在玩多线程并阅读这里的一些问题,但在这里没有找到直接解决我的问题的答案.

I've been playing around with multithreading and reading up on some of the questions here, but I haven't found an answer that directly addresses my concerns here.

我有一个在单个线程上运行的应用程序,除了单独窗口中的进度条.根据我的研究,我需要为该表单创建一个新线程,该线程将在其属性更改时重绘该表单的控件.我已将问题简化为下面的一个简单示例:

I have an application that runs on a single thread, except for a progress bar in a separate window. Based on my research, I need to create a new thread for that form which will redraw the form's controls as it's properties change. I've reduced the problem to a simple example below:

这是主"程序:

class Program
{
    static MyForm form;

    static void Main(string[] args)
    {
        form = new MyForm();
        form.Show();

        doWork();

        form.Close();
    }

    //arbitrary example of processing that takes some period of time
    static void doWork()
    {
        while (form.Value < 100000) 
        {
            form.ChangeVal();
            Thread.Sleep(1);
        }
        return;
    }
}

...这是表格.我不包括 VS 自动生成的东西.

...And here's the Form. I'm not including the auto-generated stuff from VS.

public partial class MyForm : Form
{
    private int val;
    public int Value
    {
        get { return val; }
        set { val = value; }
    }

    public Thread GUIupdater;


    public MyForm()
    {
        InitializeComponent();
        this.Refresh();
    }

    private void MyForm_Load(object sender, EventArgs e)
    {
        GUIupdater = new Thread(new ThreadStart(GUIupdaterThread));
        GUIupdater.Start();

        this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(killThreadOnClose);
    }

    public void ChangeVal()
    {
        val++;
    }

    private void changeLabel(string s)
    {
        label.Text = s;
        label.Refresh();
    }
    private delegate void labelChanger(string s);
    private void GUIupdaterThread()
    {
        while (true)
        {
            Invoke(new labelChanger(changeLabel), new object[]{val.ToString()} );
            Thread.Sleep(100); //100 ms
        }
    }
    private void killThreadOnClose(object sender, FormClosingEventArgs e)
    {
        GUIupdater.Abort();
    }
}

所以,我的目的是让计算不断运行,窗口的图形更新得相当快.然而,当我运行程序时,调用函数只被调用一次,并且标签从未真正更新!

So, my intention here is to have the calculations running constantly, with the window's graphics updating reasonably quickly. When I run the program, however, the invoke function is only called once, and the label never actually updates!

感谢任何和所有反馈.如果您想在 IDE 中查看代码,您可以从 这里

Any and all feedback is appreciated. If you want to view the code in an IDE you can download my project from Here

  • 当我添加 Console.WriteLine 语句时,我发现 GUIupdaterThread(用于更新 GUI 的东西)循环总是在 Invoke 语句上中断",永远不会到达Thread.Sleep".我将其更改为BeginInvoke",这会导致循环正常运行,但这并没有改变 GUI 不更新的事实.

澄清:

关于我的实际"项目:

  • 程序"中的主线程模拟我的软件,这是一个实现接口的插件.我决定在该线程中而不是在窗口创建的线程中更改 val/value 是经过深思熟虑的.
  • 我只能使用 .NET 4.0 .任何更新的功能都帮不了我

推荐答案

我建议重写表单的 OnPaint 方法.然后在 ChangeVal 中,在你更新了你需要更新的任何变量/数据之后,调用 this.Invalidate 这应该触发表单重新绘制自身.

I would suggest overriding the form's OnPaint method. Then inside ChangeVal, after you have updated whatever variables/data you need to update, call this.Invalidate which should trigger the form to repaint itself.

或者,如果您只是更新单个标签,请在您的 ChangeVal 方法中调用 label.Refresh.表单应正确更新.这是一个对我有用的例子:

Or if you're just updating a single label, call label.Refresh in your ChangeVal method. The form should update correctly. Here's an example that worked for me:

此表单上只有一个标签.

This form has a single label on it.

public partial class ProgressForm : Form
{

    private int currentValue = 0;

    public ProgressForm()
    {
        InitializeComponent();
    }

    public void ChangeValue(int newValue)
    {
        currentValue = newValue;
        lblValue.Text = string.Format("Current value: {0}", currentValue);
        lblValue.Refresh();   //Call Refresh to make the label update itself
    }
}

static class Program
{
    private static ProgressForm progressForm = null;

    [STAThread]
    static void Main()
    {
        //Application.EnableVisualStyles();
        //Application.SetCompatibleTextRenderingDefault(false);
        //Application.Run(new Form1());

        progressForm = new ProgressForm();
        progressForm.Show();

        doWork();

        progressForm.Close();
    }


    //arbitrary example of processing that takes some period of time
    static void doWork()
    {
        int i = 0;
        while (i < 100000)
        {
            progressForm.ChangeValue(i);
            Thread.Sleep(1);
            i++;
        }
        return;
    }
}

这篇关于C# 表单控件不会使用多线程更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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