Using [STAThread] but still get MTA

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
 
W

Willy Denoyette [MVP]

| 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.
 
L

leiz

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
 
L

leiz

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
 

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