出现的Thread.join返回false错误 [英] Thread.Join appears to return false incorrectly

查看:132
本文介绍了出现的Thread.join返回false错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 的Thread.join(INT millisecondsTimeout) 终止号的AppDomain 取值。

通常情况下,我得到一个错误信息,说明该AppDomain中并没有在5秒钟内结束。虽然通过调试我看到 AppDomain.Unload()来电5秒内轻松终止步进,但的Thread.join 返回false。

Frequently, I get an error message stating that the AppDomain did not terminate within 5 seconds. Whilst stepping through the debugger I see that the AppDomain.Unload() call terminates easily within 5 seconds, but Thread.Join returns false.

我在哪里去了?

var thread = new Thread(
    () =>
    {
        try
        {
            AppDomain.Unload(someAppDomain);
        }
        catch (ArgumentNullException)
        {

        }
        catch (CannotUnloadAppDomainException exception)
        {
            // Some error message
        }
    });

thread.Start();
const int numSecondsWait = 5;

if (!thread.Join(1000 * numSecondsWait))
{
    // Some error message about it not exiting in 5 seconds
}

修改1

值得添加了每个在的AppDomain 就做。每个的AppDomain 至少有一个的 定时 。代码大致如下所示(请注意,我崩溃的类加载到一个在这里为便于阅读)。

Worth adding what each of the AppDomains do. Each AppDomain has at least one Timer. The code roughly looks as follows, (keep in mind I've collapsed loads of classes into one here for readability).

static void Main(string[] args)
{
    _exceptionThrown = new EventWaitHandle(false, EventResetMode.AutoReset);
    _timer = new Timer(TickAction, null, 0, interval);
    try
    {
        _exceptionThrown.WaitOne();
    }
    finally
    {
        _timer.Dispose(_timerWaitHandle);
        WaitHandle.WaitAll(_timerWaitHandle);
    }
}

在影响我知道,主线程将抛出一个 ThreadAbortException ,跳进finally语句,并确保定时队列在退出之前完全耗尽。

In effect I know that the "Main" thread will throw a ThreadAbortException, jump into the finally statement and ensure the Timer queue is fully drained before exiting.

当他们打勾方法里面所有的定时第虽然日志。这样我就可以接近肯定的是,没有任何计时器队列, _timer.Dispose(_timerWaitHandle)立即返回。

All of the Timers though log when they are inside the tick method. So I can be near certain that there is nothing on the timer queue, and the _timer.Dispose(_timerWaitHandle) returns immediately.

不管它是否确实与否,的至少的一个三的AppDomain 的I是卸载 ING不会在5秒内完成。

Regardless of whether it does or not, at least one of the three AppDomains I am Unloading will not complete it within 5 seconds.

推荐答案

如果你想确保始终AppDomain的5秒钟内卸载,你可以尝试来衡量它。
例如使用这样的事情:

If you want to be sure that the appdomains always unload within 5 seconds, you can try to measure it. For example using something like this:

var stopwatch = System.Diagnostics.Stopwatch.StartNew();
AppDomain.Unload(someAppDomain);
long elapsedMillis = stopwatch.ElapsedMilliseconds;
System.Diagnostics.Trace.Writeline("Unload duration: " + elapsedMillis + " ms");



Visual Studio中(或Sysinternals公司的DebugView的工具)的输出窗口将显示它

The Output window of Visual Studio (or the DebugView tool from sysinternals) should show it

这篇关于出现的Thread.join返回false错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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