PC Review


Reply
Thread Tools Rate Thread

ApplicationContext ThreadSafety

 
 
amdrit
Guest
Posts: n/a
 
      22nd Apr 2009
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();
}
}


 
Reply With Quote
 
 
 
 
amdrit
Guest
Posts: n/a
 
      22nd Apr 2009
I found my solution, all I needed was SynchronizationContext.


"amdrit" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>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();
> }
> }
>



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Thread problems with own ApplicationContext, eskeeske Microsoft Dot NET Framework Forms 1 18th Apr 2005 08:00 PM
ApplicationContext class question craig Microsoft Dot NET Framework Forms 0 25th Jan 2005 05:59 PM
ApplicationContext and SaveFileDialog =?Utf-8?B?c2lja2Zpc2g=?= Microsoft Dot NET Framework Forms 0 14th Jan 2005 07:17 PM
Localization of ApplicationContext Tamir Khason Microsoft C# .NET 2 9th Nov 2004 08:00 AM
ApplicationContext for multiple forms and SaveFileDialog Just Gooey Microsoft Dot NET Framework Forms 0 6th Aug 2004 10:05 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:39 PM.