相关疑难解决方法(0)

使用Application.DoEvents()

可以Application.DoEvents()在C#中使用吗?

这个函数是否能够让GUI跟上应用程序的其余部分,就像VB6 DoEvents一样?

c# doevents winforms

263
推荐指数
6
解决办法
17万
查看次数

为什么Thread.Join()挂起,即使线程中的方法已经返回?

我有一个WPF应用程序,它使用一些库代码进行身份验证,需要在单线程单元线程中运行.我的方法是生成一个单独的线程来获取身份验证对象,阻塞直到线程返回然后继续执行.但是,在某些情况下,我的应用程序挂起在Thread.Join()上,即使线程方法已返回.

    public static ClaimsAuthenticationResult GetClientContextAndCookieCollection(string siteUrl, out CookieCollection cookieResult)
    {
        ClaimsAuthenticationResult authResult = new ClaimsAuthenticationResult();

        // Authentication module needs to run in single-thread apartment state because it uses
        // COM library calls where this is required
        Thread authenticationThread = new Thread(new ThreadStart(threadMethod));
        authenticationThread.SetApartmentState(ApartmentState.STA);
        authenticationThread.Start();

        // Block until thread completion
        authenticationThread.Join(); // Application hangs here

        return authResult;
    }

    private static void threadMethod() {
        // In proper application: set result. But for debugging, return immediately
        return;
    }
Run Code Online (Sandbox Code Playgroud)

我是mulththreading和WPF的新手,所以我可能会做一些愚蠢的事情.有谁看到这里发生了什么?为了记录,如果我没有将线程设置为STA,我不会遇到问题,但这是一个要求.

[编辑:似乎只有当我通过WPF视图中的验证绑定调用指定的方法时才会出现错误,特别是在TextBox上.当我在视图的构造函数中调用相同的代码时,代码按预期运行.这将是一个可行的解决方法,但知道这里实际发生了什么会很有趣.

[编辑:此处的代码已经简化了一些调试 …

c# wpf multithreading

5
推荐指数
1
解决办法
7839
查看次数

C#.NET:"多线程公寓"和"多线程"

我正在学习多线程概念(通常是针对C#.NET).阅读不同的文章,仍然无法完全理解几个基本概念.

我发布了这个问题."Hans Passant"解释得很好,但我无法理解其中的一部分.所以我开始谷歌搜索.

我读了这个没有答案的问题.

多线程和MTA是一样的吗?

假设我编写了一个WinForm应用程序,它是STA(如上面提到的Main()方法),我仍然可以在我的应用程序中创建多个线程.我可以肯定地说我的应用程序是"多线程的".这是否也意味着我的应用程序是MTA?

在谈论STA/MTA时,大多数文章(像这样)谈论COM/DCOM/Automation/ActiveX.这是否意味着DotNet与STA/MTA无关?

.net c# multithreading

5
推荐指数
1
解决办法
1058
查看次数

标签 统计

c# ×3

multithreading ×2

.net ×1

doevents ×1

winforms ×1

wpf ×1