多线程问题

多线程问题

问题描述:

我正在使用计时器重置用作警告框的标签.基本上,如果用户执行了某些操作(更具体地说,出了点问题,例如:他使用了程序无法识别的字词),则这可以尽早发现出现问题的原因,并将所发生的事情返回给他,以便他可以更改输入.

I'm using a timer to reset a lable I use as a warning box. Basically, if the user does something (more specifically, something goes wrong, ex : He uses a word not recognized by the program), this catches what went wrong early and returns to him what happened so he can change the input.

重置5秒后将标签清空,以防止他看到请不要使用汉字"之类的字样,并且可能仍认为旧错误仍然存​​在.这就是我阅读调用的内容(因为我听到begininvoke需要结束调用,所以我选择了invoke).

The reset blanks out the label after 5 seconds to prevent him from seeing something like "please do not use chinese characters" and maybe still thinking an old error is still up. This is what I got reading the invoke (since I hear begininvoke requires an endinvoke, I chose invoke).

private void lblWrn_TextChange(object sender, EventArgs e)
{
    Timee = new System.Timers.Timer(5000);
    Timee.Elapsed += new ElapsedEventHandler(timerClearWrn);
    Timee.Enabled = true;
}

string empty = "";
private void timerClearWrn(object sender, ElapsedEventArgs elapsed)
{
    lblWrn.Invoke(new Action<Label>(lblWrn), new object[] { lblWrn, "" });
}

我不太确定这是哪里出了问题,并且查找示例无法找出要更改的部分.有人可以向我解释错误或调用更多内容吗?

I am not too sure where I am going wrong with this, and looking up examples, cannot figure out which part to change. Can someone explain to me the error or invoke a bit more?

如果它是Windows Forms应用程序,则使用System.Windows.Forms.Timer,则不需要Invoke,因为计时器回调是在主窗体上执行的线.

If it's a Windows Forms application, use System.Windows.Forms.Timer, then you don't need Invoke, as the timer callback is executed on the main thread.

此外,不要在每次文本更改时都创建新的计时器.

Also, don't create a new timer on every text change.