Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Can't open a form with scatterGraph on it in a thread that is not the main thread

I've created a form with a scatterGraph on it. I'm trying to show the form in a thread that isn't the main thread.
I receive an exception "InvalidCastException" when my program performs ".ShowDialog()" method of the form.
The code looks like in the example below:
 
...
ThreadStart ts = new ThreadStart(tsm);
Thread t = new Thread(ts);
t.Start();
...
private void tsm()
{
    FormWithScatterGraphOnIt f = new FormWithScatterGraphOnIt();
    f.ShowDialog(); ***
}
...
 
The *** line throws the exception.
I've built my project in VS2005 C#.


Message Edited by 20 on 03-09-2008 09:16 AM
0 Kudos
Message 1 of 7
(4,171 Views)
Hi 20,

So far I haven't been able to reproduce the behavior you are seeing.  The fastest way to get this problem solved would be to post a simple solution that demonstrated the problem. My simple code snippet that I used was the following:

private void button1_Click(object sender, EventArgs e)
{
    Thread myThread =  new Thread(new ThreadStart(threadFunction));
    myThread.Start();
}

private void threadFunction()
{
    Form2 myForm = new Form2();
    myForm.ShowDialog();
}


What version of Measurement Studio are you using?
Do you have SP1 of the .NET Framework 2.0 installed?

Best Regards,
Jonathan N.
National Instruments
0 Kudos
Message 2 of 7
(4,150 Views)
Hi Jonathan,
 
1. I'm using VS2005 and I have SP1.
 
2. If I set thread's ApartmentState property as STA I can go around the problem but that doesn't solve it.
 
3. Your code is exactly as my code is (the form has a graph on it).
 
Eddie
0 Kudos
Message 3 of 7
(4,145 Views)
Hi 20,

- What version of Measurement Studio are you using?
- I assume this behavior happens in a brand new project with just the code I mentioned in it?
- Have you enabled any additional exception hanlding under Debug >> Exceptions?
- What OS are you running on?

Posting your example project still might help as you never know, maybe if I run your code I will get the exception. In order to help solve the problem, its best if I can somehow reproduce the issue.

Best Regards,
Jonathan N.
National Instruments
0 Kudos
Message 4 of 7
(4,143 Views)
1. I'm using MS 2005
2. The project is a brand new project
3. I haven't added new exceptions
4. Windows 2000
 
The problem occurs only if I add an instrumentControlStrip to the graph.
0 Kudos
Message 5 of 7
(4,138 Views)
Hi 20,

So I am able to reproduce the exception you are seeing when using the InstrumentControlStripControl along with having a PropertyEditor control as an item in the strip control.  I then narrowed the issue down to the general PropertyEditor control so it has something to do with that.  At this point, I'm not sure yet what the problem is but thanks for reporting it.  For your reference, the bug ID is #96497.  As of now, the only workaround I can think of is to display your second form in the main UI thread. If we come up with another workaround, I'll let you know.

Sorry for the inconvenience.

Best Regards,
Jonathan N.
National Instruments
0 Kudos
Message 6 of 7
(4,130 Views)
Hi 20,

We figured out what the problem was and it turned out to not be a bug, but rather a lack of documentation for the PropertyEdtitor.  The overall issue is that under the hood, there are calls to COM objects and therefore you need to correctly set the Apartment State for the thread you created. The way you were using the STAThread attribute was incorrect as you were trying to apply this attribute to the method that the thread calls. See the STAThreadAttribute Class in the MSDN documentation for more information. Basicallly you just need to use the SetApartmentState method like so:

Thread myThread =  new Thread(new ThreadStart(threadFunction));
myThread.SetApartmentState(ApartmentState.STA);
myThread.Start();


I tested this out and it works fine. We are going to add some additional documentation that describes this use case in the future.

Best Regards,
Jonathan N.
National Instruments
0 Kudos
Message 7 of 7
(4,116 Views)