Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hey readers,

From my main thread, I create another thread and I set its ApartmentState.
When I break in the constructor of the new thread and the thread state is STA.

After a few steps I usually lose control and jump to a timer that fired inside it.
The apartmentstate will have changed to MTA which causes Clipboard.GetText(); to return nothing.

bwlog = new Thread(new ThreadStart(startlog));
bwlog.SetApartmentState(ApartmentState.STA);


So why oh why does it turn into to MTA?

Thanks for reading!
Posted
Updated 27-Dec-10 14:56pm
v2
Comments
Dr.Walt Fair, PE 27-Dec-10 19:59pm    
How are you trying to set the state? See http://msdn.microsoft.com/en-us/library/system.threading.thread.apartmentstate%28VS.80%29.aspx
Scalee 27-Dec-10 20:06pm    
it's in the code block in the post, the second line.
Dalek Dave 27-Dec-10 20:56pm    
Edited for Grammar and Readability.

I was surprised your code did not work, so I've done the following test:

C#
namespace SetSTA {
    using System;
    using System.Threading;

    class Program {

        [MTAThread]
        static void Main(string[] args) {
            Thread mainThread = Thread.CurrentThread;
            System.Console.WriteLine(
                "Main thread apartment state: {0}",
                mainThread.GetApartmentState());
            Thread staThread = new Thread( () => {
                Thread thisThread = Thread.CurrentThread;
                System.Console.WriteLine(
                    "Custom thread apartment state: {0}",
                    thisThread.GetApartmentState());
            });
            staThread.SetApartmentState(ApartmentState.STA);
            staThread.Start();
            staThread.Join();
            System.Console.ReadKey();
        } //Main
    
    } //class Program

} //namespace SetSTA


This code sample works as expected; this is output:

Main thread apartment state: MTA
Custom thread apartment state: STA


You problem is not reproduced.

At the same time, you try to change the apartment state in exact same way; and your code looks correct.

However, the problem can be somewhere else in your code which is not shown to us. At what moment of time your thread's apartment state is changed back to MTA? Where is the code to indicate that?
In this case, complete code which shows the problem is needed. Can you submit such thing?
 
Share this answer
 
v5
Comments
Kasson 27-Dec-10 23:13pm    
Good call. My 5 to you.
Shani Natav 28-Dec-10 3:55am    
My guess is that the timer event has referring to, is in a MTA thread.
Scalee 28-Dec-10 10:53am    
I'll post the source, however i expect to get grief for it since the nature of the class is a key logger. But you might be able to see that's all local stuff, i use it for when i forget things.

http://pastebin.com/95BSyBBY
edit: whoops didn't remove my password ;)
Sergey Alexandrovich Kryukov 28-Dec-10 13:58pm    
I understand you may be able to deliver all of the code. I would not like too much code, too. You could do much better: make the possible minimum application just to reproduce the problem, no more. If you fail to reproduce it on a really simple sample, this itself gives you a good food for thought. Finally, you will dissect all the path from the simple to the real-life and locate the point where this thing goes crasy.
Sergey Alexandrovich Kryukov 30-Dec-10 13:29pm    
I'm just curious why this is voted "3" and how come the solution is marked accepted.
Did you find the solution yourself? (See my comment to your own answer.)
On SAKryukov suggestion to make the application smaller, i thought what if i simply remove the thread, which i did but yielded the same effect.

So after increasing timer tick time, i noticed it's only the elapsed event that runs in MTA. So after a bit of googling i found out that there are actually 3 timer classes.

System.Timers.Timer
System.Threading.Timer
System.Windows.Forms.Timer

And i found out that only the 3rd runs in STA.
Another thing learned ;)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 30-Dec-10 13:29pm    
Yes, of course, there are 3 timers.
But it was not your question. We still don't know why the threads apartment mode turned back to MTA. Or it is not the fact anymore? Do you know the answer (please don't tell use it does not matter because you have a solution)? Why? If you know why not sharing with us?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900