ApplicationContext ThreadSafety

A

amdrit

I am prototyping a TaskTray application and am having a problem displaying a
form from my workflow.

I have an ApplicationContext class that I use to manage my workflow and to
display my forms. The application connects to a socket server that then
requests my application to display a form. This form has a webbrowser
control on it.

When I attempt to display this form, I receive an error:

A first chance exception of type 'System.Threading.ThreadStateException'
occurred in System.Windows.Forms.dll
System.Threading.ThreadStateException: ActiveX control
'8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the
current thread is not in a single-threaded apartment.

I have legacy ActiveX conrtols that I also need to display on this form once
I have this corrected. Does anyone know how to work around this issue? Do
I have to invoke the form, I wouldn't even know how to test for
InvokeRequired or even implement a delegate to show this form? How do I get
back to the main thread to execute this code?

Note: When I attempt to display the same form from a menu item click event
(Accessed from the TaskTray Icon), it loads with no issue.

Here is my code to generate the issue:

[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MyApplicationContext());
}

public class MyApplicationContext : ApplicationContext
{
//Called from socket client
private void ProcessCommand(byte[] data)
{
Envelope stamp = DeserializeEnvelope(data);

switch (stamp.MessageType)
{
case MessageType.ShowForm:
ShowForm((FormData)stamp.Data);
break;
}
}
void ShowForm(FormData data)
{
frmToShow f = new frmToShow();
f.Data = data;
f.Show();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top