卸载 appdomain 时出错.(来自 HRESULT 的异常:0x80131015),在 Windows 服务中 [英] Error while unloading appdomain. (Exception from HRESULT: 0x80131015), inside Windows Service

查看:33
本文介绍了卸载 appdomain 时出错.(来自 HRESULT 的异常:0x80131015),在 Windows 服务中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Windows 服务中收到此错误.这与我之前在我的问题此处

I receive this error in a windows service. This is the same service that I've previously discussed in my question here

修改代码以使用Parallel.ForEach(我自己的版本,因为这是一个3.5 Windows 服务).使用 Parallel 的原因归结为 Unload 每个域花费的时间太长,并且并行运行它们应该被证明更快(似乎即使每个 Unload 只有一个线程正在执行?!).

The code is revised to use Parallel.ForEach (my own version as this is a 3.5 windows service). The reason for the Parallel use is down to the fact that it simply took too long to Unload each domain and running them in parallel should prove to be faster (appears to be even though there is only one thread that's doing each Unload?!).

基于其他帖子,我只能猜测这在某种程度上归结为我使用 ThreadPool ThreadUnloadAppDomains.我只是不知道如何避免它?

Based on other posts, I can only guess that this is somehow down to the fact I am using a ThreadPool Thread to Unload the AppDomains. I just can't see how to avoid it?

public partial class SomeService : ServiceBase
{
    private Manager _appDomainManager;

    protected override void OnStop()
    {
        _appDomainManager.Dispose();
    }
}

public class Manager : IDisposable
{
    public void Dispose()
    {
        Log.Debug("Disposing");
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (_disposed) return;
        if (disposing)
        {
            // dispose managed resources
            Parallel.For(0, appdomains.Length, UnloadAppDomian);
        }

        _disposed = true;
    }
}

private UnloadAppDomain(int appDomainIndex);

public static class Parallel35
{
    public static void For(int start, int end, Action<int> action)
    {
        var waitHandles = new WaitHandle[end - start];
        for (int j = 0; j < waitHandles.Length; j++)
        {
            waitHandles[j] = new ManualResetEvent(false);
        }

        for (int i = start; i < end; i++)
        {
            int i1 = i - start;
            ThreadPool.QueueUserWorkItem(
                state =>
                {
                    try
                    {
                        action((int) state);
                    }
                    finally
                    {
                        ((ManualResetEvent) waitHandles[i1]).Set();
                    }
                }, i);
        }
        WaitHandle.WaitAll(waitHandles);
    }
}

推荐答案

我将此作为一个错误追溯到 AppDomain 之一退出时等待从未设置的 WaitHandle.

I tracked down this as a bug to one of the AppDomains on exit waiting for a WaitHandle that's never set.

如果一个线程没有中止,例如因为它正在执行非托管代码,或者因为它正在执行 finally 块,然后在一段时间内抛出CannotUnloadAppDomainException最初称为卸载的线程.

If a thread does not abort, for example because it is executing unmanaged code, or because it is executing a finally block, then after a period of time a CannotUnloadAppDomainException is thrown in the thread that originally called Unload.

AppDomain 现在卸载速度相对较快,我的服务也很快停止.

The AppDomain now unloads relatively quickly and my service stops quite quickly.

这篇关于卸载 appdomain 时出错.(来自 HRESULT 的异常:0x80131015),在 Windows 服务中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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