简体   繁体   English

C#:如何从线程更新Winform?

[英]C#: how to update winform from thread?

AC# thread (Read()) causes System.NotSupportedException when it tries to update a winform based on received content. 当AC#线程(Read())尝试根据收到的内容更新Winform时,导致System.NotSupportedException。 The full error message is 完整的错误消息是

Read() System.NotSupportedException: An error message cannot be displayed because an optional resource assembly containing it cannot be found at Microsoft.AGL.Common.MISC.HandelAr() at System.Windows.Forms.ProgressBar._SetInfo() at System.Windows.Forms.ProgressBar.set_Value() at ...ProcessStatus() at ...Read() Read()System.NotSupportedException:无法显示错误消息,因为在System.Windows.Forms.ProgressBar._SetInfo()的Microsoft.AGL.Common.MISC.HandelAr()处找不到包含该消息的可选资源程序集。 Windows.Forms.ProgressBar.set_Value()位于... ProcessStatus()位于... Read()

The Build/Target Environment is: Microsoft.NET\\SDK\\CompactFramework\\v2.0\\WindowsCE . 构建/目标环境为: Microsoft.NET\\SDK\\CompactFramework\\v2.0\\WindowsCE Is the problem writing to the ProgressBar from a Thread? 问题是否从线程写入ProgressBar? If so, what is the correct C#/winforms method to update a ProgressBar from a Thread? 如果是这样,从线程更新ProgressBar的正确C#/ winforms方法是什么? In this application the Read() Thread is continuous: it is started when the application starts and runs forever. 在此应用程序中,Read()线程是连续的:它在应用程序启动时启动并永远运行。

void ProcessStatus(byte[] status)
{
    Status.Speed = status[5];
    var Speed = Status.Speed/GEAR_RATIO;
    Status.Speed = (int) Speed;
    progressBarSpeed.Value = Status.Speed;
    ...

You'll need to use Invoke to make changes to controls created in the Gui Thread. 您需要使用Invoke更改在Gui线程中创建的控件。

To make life easier, take a look at some of the extension methods provided here 为了使生活更轻松,请看一下此处提供的一些扩展方法

您应该致电Control.BeginInvoke

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

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