Cross thread thread error showing a form during a form. Example

advertisements

I have a tricky problem here that I have not been able to solve and neither have the people I asked on msdn forums.

I am using a third part product (signal lab from mitov.com) which is a set of .net components. I have created a windows form app that works fine if it is run. I now want to show this form from another form when this other form loads (or shows, or activates...).

I already have examples of this working with another form: Here is the typical code: I am loading a form (SecondForm) from the main program...

private void SecondForm_Load(object sender, EventArgs e)
{
     Form _macros = new Macros(this);  //this works perfectly fine
     _macros.show(this);

     //this is where I have no success
     Form _spectrum = new SpectrumScope;
     _spectrum.Show();

}

I get various errors from no form displayed and then an exception about not instantiating the _spectrum, to an error about cross threaded operation not being allowed, to a blue screen with a message about clocks not being synchronized between processors.

If I place a button on "Second Form" and add the show form code to the click event, it works fine. If I try to use form.activated, or form shown instead of form.load there is not difference. If I add a time to form Shown, which then does a button.performClick, there is no difference.

Obviously the form which contains many components (and many threads behind the scene) does not like the fact that it is not being opened by the user.

I'm fairly new to C# and I'm pretty sure this is threading related.

Thank you for any help.


Try the blow code in your form load method, that should let .Net manage the threading for you.

this.Invoke(new MethodInvoker(() => (new SpectrumScope).Show()));