473,396 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Using [STAThread] but still get MTA

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

Sep 4 '06 #1
3 2315

"leiz" <le********@gmail.comwrote in message
news:11**********************@74g2000cwt.googlegro ups.com...
| 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.


Sep 4 '06 #2
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
Willy Denoyette [MVP] wrote:
"leiz" <le********@gmail.comwrote in message
news:11**********************@74g2000cwt.googlegro ups.com...
| 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.
Sep 5 '06 #3
I already figure it out. Just Ignore this. :)

leiz wrote:
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
Willy Denoyette [MVP] wrote:
"leiz" <le********@gmail.comwrote in message
news:11**********************@74g2000cwt.googlegro ups.com...
| 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.
Sep 5 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: sonu | last post by:
Hi I have developed a smart client application. When i try to execute it. It displays the first form which is the login screen. On giving the corrent login id and password, the main form opens....
1
by: Alasdair | last post by:
Friends, I'm an old C programmer who upgraded to C++ but was never comfortable with it. I've recently moved to C# and love it but I obviously am missing some of the subtleties. I thought the...
3
by: Justine | last post by:
hi all, i just want to know the significance of in the C# application. Why & for What reason is this used. Thanz in Advance, Justine
1
by: Alberto | last post by:
What's the meaning of the STAThread attribute? Thanks
7
by: Adrian | last post by:
I hit on this problem converting a VB.NET insurance application to C#. Age next birthday calculated from date of birth is often needed in insurance premium calculations. Originally done using...
6
by: LEBRUN Thomas | last post by:
Hello :) I would like to use Outlook 2003 in my application so for that, i've added the referece to Outlook 11.0 Object Model to my project. Then, I try this simple code : using System;...
1
by: Sean | last post by:
Im fairly new to C# and am having a few problems getting events to fire when using COM interop with C#, can someone take a quick look at the following example and give me a pointer as to why the...
22
by: Martijn Mulder | last post by:
What is the penalty if I do not put just above the Main() method?
1
by: Sefner | last post by:
Hey everyone. I have a program that watches a .txt log file from a chat client or a game and based on each line, triggers events. One thing I am trying to do is send the contents of a text file to...
12
by: rafalK | last post by:
Hi All, I have a big problem with STAThread attribute. I'm using XNA framework connected with WinForms. XNA is working in non STAThread. I have a problem with displaying CommonDialog forms e.g....
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.