NI TestStand

cancel
Showing results for 
Search instead for 
Did you mean: 

Calling windows form object from test stand causes form to freeze.

Hi,

I am working on a teststand project that uses .Net dlls. In teststand I am calling a .Net dll which creates a new windows.forms object to perform the test. In the windows form, I only have a picture box that shows the screen of a remote measurement device. The form works fine when called from a windows form application. However, when i use test stand, the form freezes until the test finishes. We need the windows form to be visible in order to inform the operator about the test progress. Any help would be very appreciated.

  

0 Kudos
Message 1 of 4
(4,072 Views)

On the Windows operating system, a window is not functional (does not paint or accept user input) unless the thread which created it is processing Window messages. There are several ways to accomplish this, but it really depends on what you are trying to do. Do you want this form to be like a modal dialog, such that your step's code module does not return until the dialog is dismissed? If so then you should be using the ShowDialog() method on the form rather than the Show() method. If you want your Window to be modeless (in otherwords stay visible even after your step's code module returns), then I recommend you create a new thread inside of your .NET module and have that thread create the form and process window messages for it (i.e. the new thread can do ShowDialog() thus allowing the original thread to return back to teststand). You will then need to call Invoke on the form from the TestStand thread in order to call something in the form owner's thread.

 

IMPORTANT NOTE: The thread which is processing messages for the Window must be the same thread that creates it. This is what's called thread affinity. A Window has a special association with the thread which creates it.

 

Hope this helps,

-Doug

 

Message Edited by dug9000 on 05-06-2010 10:19 AM
Message Edited by dug9000 on 05-06-2010 10:20 AM
0 Kudos
Message 2 of 4
(4,062 Views)

Hi Doug,

Thanks for your reply. First of all the first method, ShowDialog() method, is not suitable for my application. I am creating a new thread inside the .Net dll that i call from test stand. In this thread I also create a form object and use the Show() method. In the form there exists a picturebox that shows the screen of a remote measurement device. In the thread I also send remote control commands to control the measurement device, and I want to observe the results of the remote control commands and test results on screen. In order to update the picturebox I have used the Invoke method as below.

serverScreen.SetPictureBoxImage(image); 

and the delegate is: 

        public delegate void PictureDelegate(Image image);  
        public void SetPictureBoxImage(Image image) {        
           if (pictureBox1.InvokeRequired)     
               pictureBox1.Invoke(new PictureDelegate(SetPictureBoxImage), image);    
           else {  pictureBox1.Image = image; 
               Refresh();   } 

        }

 

However, this doesnt work as well. Do i have something wrong, or do you have any other suggestions? 
Message Edited by Idin on 05-07-2010 09:17 AM
0 Kudos
Message 3 of 4
(4,037 Views)

Hi Idin,

 

A couple of issues:

 

1) I'd recommend doing ShowDialog() from the new thread rather than just show, as otherwise you have no control over the lifetime of that thread and it could just be exiting right after the show is done or getting reused depending on how you created the thread. Also you have no guarantee that it will be processing messages. ShowDialog() runs a message loop until the dialog is dismissed. Don't worry about this dialog being modal. Modality is generally per thread so it should not interfere with UIs displayed by other threads. Making this change might fix your problem depending on what the problem is.

 

2) I'm not 100% sure, but Images might also have thread affinity and thus need to be created in the thread in which they are used. What error message or problem are you seeing exactly. Also please try the ShowDialog() change above in 1) as that is also a potential problem, but one that is much easier to fix.

 

Hope this helps,

-Doug

0 Kudos
Message 4 of 4
(4,013 Views)