简体   繁体   English

WinForms标签前色更新

[英]WinForms label forecolor update

I've a problem: I try to update font color in a label from another thread, but it fails and I've no idea why. 我有一个问题:我尝试从另一个线程更新标签中的字体颜色,但是失败了,我也不知道为什么。

    private Color defaultBgColor = Color.Red;
    private Color passOkBgColor = Color.LightGreen;

    private Color defaultForeColor = Color.FromArgb(255, 255, 255, 255);
    private Color passOkForeColor = Color.FromArgb(255, 0, 0, 255);

There's my function: 有我的功能:

private void Func() {
        try {
            if ( pipeReader != null) {
                string msg_strg = String.Empty;
                while(true) {
                    if ( !npsc.IsConnected ) {
                        npsc.Connect();
                        Thread.Sleep(500);
                        continue;
                    }

                    while ( (msg_strg = pipeReader.ReadLine()) != null ) {                      ;
                        statusLabel.BeginInvoke( (MethodInvoker)(() => {
                                                                    if ( statusesDict.ContainsKey(msg_strg) )
                                                                        statusLabel.Text = statusesDict[msg_strg];
                                                                    else
                                                                        statusLabel.Text = "!UNKNOWN STATUS!";

                                                                    if ( msg_strg != "CARDREADER_USER_EXISTS" ) {
                                                                        this.BackColor = defaultBgColor;
                                                                        statusLabel.ForeColor = defaultForeColor;
                                                                    } else {
                                                                        this.BackColor = passOkBgColor;
                                                                        statusLabel.ForeColor = passOkForeColor;
                                                                    }
                                                                 }) );
                        statusLabel.Refresh();
                        Thread.Sleep(300);
                    }
                }
            }
        } catch ( Exception ex ) {
            Log.Instance.Error("Exception: "+ex.Message);
        }
    }

How should I resolve this problem? 我应该如何解决这个问题?

There are at least two issues with your code. 您的代码至少有两个问题。

First, statusLabel.Refresh call should be inside the BeginInvoke block. 首先, statusLabel.Refresh调用应位于BeginInvoke块内。

Second, the BeginInvoke block is using captured variable msg_strg which might have been modified at the time the block executes. 其次, BeginInvoke块使用捕获的变量msg_strg ,该变量在该块执行时可能已被修改。

The following will fix the above issues, try and see if your problem (whatever it is) is solved: 以下内容将解决上述问题,尝试查看您的问题(无论如何)是否已解决:

private void Func() {
    try {
        if (pipeReader != null) {
            Action<string> updateStatus = message =>
            {
                if (statusesDict.ContainsKey(message))
                    statusLabel.Text = statusesDict[message];
                else
                    statusLabel.Text = "!UNKNOWN STATUS!";

                if (message != "CARDREADER_USER_EXISTS") {
                    this.BackColor = defaultBgColor;
                    statusLabel.ForeColor = defaultForeColor;
                } else {
                    this.BackColor = passOkBgColor;
                    statusLabel.ForeColor = passOkForeColor;
                }

                statusLabel.Refresh();
            };


            while(true) {
                if (!npsc.IsConnected) {
                    npsc.Connect();
                    Thread.Sleep(500);
                    continue;
                }

                string msg_str;
                while ((msg_strg = pipeReader.ReadLine()) != null) {
                    statusLabel.BeginInvoke(updateStatus, msg_str);
                    Thread.Sleep(300);
                }
            }
        }
    } catch (Exception ex) {
        Log.Instance.Error("Exception: " + ex.Message);
    }
}

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

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