简体   繁体   English

C#从后台线程更新Windows窗体中的UI

[英]C# Updating UI in Windows Forms from background thread

http://www.codeproject.com/KB/threads/winformsthreading.aspx http://www.codeproject.com/KB/threads/winformsthreading.aspx

I'm attempting to use the above, and while it works in some sense (it doesn't lock up my application), it isn't updating the labels on the UI unfortunately. 我试图使用上面的内容,虽然它在某种意义上起作用(它没有锁定我的应用程序),但它不会更新UI上的标签。 Am I going wrong somewhere? 我在某个地方出错了吗? I have two labels that are on the MainForm, expiredPoliciesLabel and missingPoliciesLabel. 我在MainForm,expiredPoliciesLabel和missingPoliciesLabel上有两个标签。 To update them, I have to set expiredPoliciesNum and missingPoliciesNum by doing a series of database queries, as you can see. 要更新它们,我必须通过执行一系列数据库查询来设置expiredPoliciesNum和missingPoliciesNum,如您所见。 I need the labels to update automatically every minute or so. 我需要标签每分钟左右自动更新。 (I know right now I have it set at 1 second, it's only to see if the code is working) (我现在知道我将它设置为1秒,只是看代码是否正常工作)

Here is my code. 这是我的代码。

public delegate void updatePolicyLabelsDelegate();

public partial class MainForm: Form
{
    SQLiteQuery sqliteQuery = new SQLiteQuery(Properties.Settings.Default.DatabasePath);
    int expiredPoliciesNum = 0;
    int missingPoliciesNum = 0;
    Thread minimizeThread;

    public MainForm()
    {
        this.Resize += new EventHandler(MainForm_Resize);
        this.IsMdiContainer = true;
        InitializeComponent();
        this.ShowInTaskbar = false;

        keyValidation();

        Thread bottomLabelsThread = new Thread(new ThreadStart(updateLabels));
        bottomLabelsThread.IsBackground = true;
    }

    public void updateLabels()
    {
        while (true)
        {
            Invoke(new updatePolicyLabelsDelegate(updatePolicyLabels));
            Thread.Sleep(1000);
        }
    }

    private void updatePolicyLabels()
    {
        DataTable dt = sqliteQuery.selectFromDatabase("*", "WHERE GLOPolicy != '1'");
        missingPoliciesNum = dt.Rows.Count;

        dt = sqliteQuery.selectFromDatabase("*", "WHERE ALPolicy != '1'");
        missingPoliciesNum = missingPoliciesNum + dt.Rows.Count;

        dt = sqliteQuery.selectFromDatabase("*", "WHERE WCPolicy != '1'");
        missingPoliciesNum = missingPoliciesNum + dt.Rows.Count;

        dt = sqliteQuery.selectFromDatabase("*", "WHERE ULPolicy != '1'");
        missingPoliciesNum = missingPoliciesNum + dt.Rows.Count;

        String now = DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day;

        dt = sqliteQuery.selectFromDatabase("*", "WHERE GLOPolicy = '1' AND GLOExpiration < '" + now + "'");
        expiredPoliciesNum = dt.Rows.Count;

        dt = sqliteQuery.selectFromDatabase("*", "WHERE ALPolicy = '1' AND ALExpiration < '" + now + "'");
        expiredPoliciesNum = expiredPoliciesNum + dt.Rows.Count;

        dt = sqliteQuery.selectFromDatabase("*", "WHERE WCPolicy = '1' AND WCExpiration < '" + now + "'");
        expiredPoliciesNum = expiredPoliciesNum + dt.Rows.Count;

        dt = sqliteQuery.selectFromDatabase("*", "WHERE ULPolicy = '1' AND ULExpiration < '" + now + "'");
        expiredPoliciesNum = expiredPoliciesNum + dt.Rows.Count;


        expiredPoliciesLabel.Text = expiredPoliciesNum + " Expired Policies       ";
        missingPoliciesLabel.Text = missingPoliciesNum + " Missing Policies       ";
    }

Thanks for any help, I'm new to threading, and could use some expertise, and believe it or not, I've searched everywhere trying to figure out where I'm going wrong to no avail. 感谢任何帮助,我是线程新手,可以使用一些专业知识,不管你信不信,我到处搜索,试图找出我出错的地方无济于事。

You created the thread object, but you don't start it, as your code seems to show: 您创建了线程对象,但是您没有启动它,因为您的代码似乎显示:

bottomLabelsThread.Start();

It is not started automatically. 它不是自动启动的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM