Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Updating NI Control Values in a Thread

Hello.  I have an application that uses a hardward timer from an NI board to give me a 10 Hz loop.  When the 10 Hz clock signal rises a callback is fired and this callback calls my 10 hz loop as a thread.  On every tenth iteration of the loop it calls a 1 Hz thread where the meat of the program runs.  I've had issues with timing before so I made some indicators to show me how many milliseconds certain operations take, and to keep track of the 10hz loop count and the period of my threads.  The time required to complete tasks in each of the two threaded loops can not exceed the maximum allowable 100ms/1000ms or bad things happen.

 

I have this same program running in many cells, some of which are intel i7s with Win7 64bit and others are dual core XP machines.  In one particular case (with a Windows 7 machine) the code which updates the values from these indicators would screw up and crash the program even though it was in a Try/Catch statement.  It would throw an unhandled exception somehow.  In other cells I never have an issue with this exact same code.  I commented out every call to update values from that thread and I got no more errors. 

 

What would be the best way to update these values when I'm using a thread?  Please see the picture below for a screen shot of the indicators.

Programming Data Acquisition and Control in Measurement Studio and Labwindows/CVI
0 Kudos
Message 1 of 4
(5,401 Views)

When you say "It would throw an unhandled exception somehow", is that the expected behavior when the thread exceeds the time allotted? Also, you may try using simple numeric indicators which take less time to redraw (though this should be handled in a separate thread to begin with).

 

Regards,

 

Brandon V.

Applications Engineer

National Instruments 

0 Kudos
Message 2 of 4
(5,371 Views)

The code for one of more of the now-commented out writes to indicator values would throw an unhandled NI exception even though they were in a try catch block.  An example is below:

 

            'tnkA.Value = Now.Second * 1000 + Now.Millisecond - SectionTimer

 

The writes do not take too long, and the error does not involve timing.  It involves threading and writing values to those controls.  The error occured on several occasions and ceased when I commented out those lines.

 

I wish I had the error text but I was not writing that error to file because I don't want 10 Hz file writes if a problem occurs and won't go away.  Are there any rules about setting a Control.value property from a thread?

Programming Data Acquisition and Control in Measurement Studio and Labwindows/CVI
0 Kudos
Message 3 of 4
(5,365 Views)

.NET will not let you update any controls from a thread other than the one that created it.  The simplest solution is to check InvokeRequired.  InvokeRequired basically checks the thread ID to make sure it matches the thread the control was created on.  There are lots of examples on the internet.  This is one of them.

 

source http://lostechies.com/gabrielschenker/2009/01/23/synchronizing-calls-to-the-ui-in-a-multi-threaded-a...

 

public void OnChangeStatus(StatusEventArgs e)
{
    if (this.InvokeRequired)
        this.BeginInvoke(new Action<StatusEventArgs>(OnChangeStatus), new[] {e});
    else
        lblStatus.Test = e.StatusMessage;
}

 

0 Kudos
Message 4 of 4
(5,357 Views)