线程和MTA STA [英] Threading And MTA STA

查看:105
本文介绍了线程和MTA STA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿读者,



从我的主线程中,我创建了另一个线程并设置了它的ApartmentState。

当我打破新线程的构造函数并且线程状态是STA时。



经过几个步骤后,我经常失去控制并跳到一个内部触发的计时器。

公寓状态将改为MTA导致Clipboard.GetText();什么都不返回。



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);





那么为什么哦为什么会变成MTA?



感谢阅读!



So why oh why does it turn into to MTA?

Thanks for reading!

推荐答案

我很惊讶你的代码没有用,所以我做了以下测试:



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

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





您的问题没有转载。



同时,您尝试更改公寓状态完全相同的方式;并且你的代码看起来是正确的。



然而,问题可能在你的代码中的其他地方没有向我们显示。在什么时候你的线程的公寓状态被改回MTA?代码在哪里表示?

在这种情况下,需要显示问题的完整代码。你可以提交这样的东西吗?



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?


在SAKryukov建议使应用程序变小,我想如果我只是删除线程,我做了但产生了相同的效果。



因此,在增加计时器滴答时间之后,我注意到它只是在MTA中运行的已过去事件。所以经过一些谷歌搜索后我发现实际上有3个计时器类。



System.Timers.Timer

System.Threading。计时器

System.Windows.Forms.Timer



我发现只有第三次在STA运行。

另一件事学到了;)
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 ;)


这篇关于线程和MTA STA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆