Using [STAThread] but still get MTA

  • Thread starter Thread starter leiz
  • Start date Start date
L

leiz

Hi all,

I am using C# and .net 2.0 to automate Word 2003. I have a few event
handlers to process word events such as
ApplicationEvents4.DocumentChange, etc. The threads that call the
handlers are MTA and are belonged to my C# program. Since my program
doesn't create any threads explicitly, I would like to know where the
MTA threads coming from and why they are MTA rather than STA.

Thanks a lot
 
| Hi all,
|
| I am using C# and .net 2.0 to automate Word 2003. I have a few event
| handlers to process word events such as
| ApplicationEvents4.DocumentChange, etc. The threads that call the
| handlers are MTA and are belonged to my C# program. Since my program
| doesn't create any threads explicitly, I would like to know where the
| MTA threads coming from and why they are MTA rather than STA.
|
| Thanks a lot
|

The handlers run on threadpool threads, these are MTA by default (cannot be
changed). When using eventhandlers to handle COM events, you should be
prepared to be called on an abitrary thread, that is, take care to marshal
calls to the UI (STA) thread when touching the UI for instance.

Willy.
 
Thanks for that.

Now I got another problem when I tried to synchronize the threads.
Everytime a NewDocument event handler is called, I want to add the new
docment name into my tab control. However, when TabControl.Add is
called, it sends message to UI and waits for returning. I overrided the
WndProc and use lock(anObject) both in WndProc and the event handler.
It didnt work. Any ideas?

Thanks
 
I already figure it out. Just Ignore this. :)
Thanks for that.

Now I got another problem when I tried to synchronize the threads.
Everytime a NewDocument event handler is called, I want to add the new
docment name into my tab control. However, when TabControl.Add is
called, it sends message to UI and waits for returning. I overrided the
WndProc and use lock(anObject) both in WndProc and the event handler.
It didnt work. Any ideas?

Thanks
 
Back
Top