jacknad
jacknad

Reputation: 13739

C#: how to update winform from thread?

A C# thread (Read()) causes System.NotSupportedException when it tries to update a winform based on received content. 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()

The Build/Target Environment is: Microsoft.NET\SDK\CompactFramework\v2.0\WindowsCE. Is the problem writing to the ProgressBar from a Thread? If so, what is the correct C#/winforms method to update a ProgressBar from a Thread? In this application the Read() Thread is continuous: it is started when the application starts and runs forever.

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

Upvotes: 0

Views: 562

Answers (2)

Brook
Brook

Reputation: 6009

You'll need to use Invoke to make changes to controls created in the Gui Thread.

To make life easier, take a look at some of the extension methods provided here

Upvotes: 4

SLaks
SLaks

Reputation: 887453

You should call Control.BeginInvoke

Upvotes: 2

Related Questions