Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么在使用多线程时,我的IIS应用程序池在工作线程引发和异常后关闭?_C#_Multithreading_Exception_Iis - Fatal编程技术网

C# 为什么在使用多线程时,我的IIS应用程序池在工作线程引发和异常后关闭?

C# 为什么在使用多线程时,我的IIS应用程序池在工作线程引发和异常后关闭?,c#,multithreading,exception,iis,C#,Multithreading,Exception,Iis,我正在尝试创建一些电子邮件发送过程。如果没有多线程方法,它将按预期工作。但发送所有电子邮件需要更多的时间 在我决定使用多线程之后,我还为多线程进程做了一些异常处理 这是我的方法 public enum EmailRequestProcesStatus { Pending = 1, Processing = 2, Complete = 3 } public async Task<int> InvokeSendingEmailAsync(string server

我正在尝试创建一些电子邮件发送过程。如果没有多线程方法,它将按预期工作。但发送所有电子邮件需要更多的时间

在我决定使用多线程之后,我还为多线程进程做了一些异常处理

这是我的方法

public enum EmailRequestProcesStatus
{
    Pending = 1,
    Processing = 2,
    Complete = 3
}
public async Task<int> InvokeSendingEmailAsync(string serverFolderPath)

{
            // update pending requests
            await UpdateEmailRequestToProcessAsync(EmailRequestProcesStatus.Processing);

            var invokedDateTime = DateTime.Now;
            // first get the CustomerEmail list
            var emailList= await GetPendingEmailList();

            IList<CustomerRule> customerRules = new List<CustomerRule>();
            int ruleLength = 0;
            List<Thread> threadList = new List<Thread>();
            // email count list
            List<int> sendEmailCountList = new List<int>();

            foreach (var customer in emailList)
            {
                ruleLength++;
                customerRules.Add(customer);
                if (ruleLength % 20 == 0)
                {
                    var custList = customerRules;
                    Thread t = new Thread(() => ProcessEmailSend(custList, serverFolderPath, invokedDateTime, sendEmailCountList));
                    threadList.Add(t);
                    t.Start();
                    customerRules = new List<CustomerRule>();
                }
            }

            if (customerRules.Count > 0)
            {
                Thread t = new Thread(() => ProcessEmailSend(customerRules, serverFolderPath, invokedDateTime, sendEmailCountList));
                threadList.Add(t);
                t.Start();
            }

            // join the all threads
            foreach (var thread in threadList)
            {
                thread.Join();
            }

            return sendEmailCountList.Count;
}

private void ProcessEmailSend(IList<CustomerRule> customerList, string serverFolderPath, DateTime invokedDateTime, List<int> sendEmailCountList)
{
   foreach(var cust in customerList)
   {
       var customerDetailList = get customerEmailDetails(cust);

       // access some file from file server by giving details
       var result = getPdf(customerDetailList.files);

       // do the rest of work using 
   }
}

private filesDto getPdf(filesDto files)
{
   try
   {
      //here I call the file server 
   }
   catch(Exception e)
   {
       UpdateEmailServiceWhenException();
       throw;
   }
}
抛出后,它将被全局异常处理程序捕获。 如果一个工作线程从文件服务器获取了一个未找到文件的异常,那么它将作为异常插入到表中并再次抛出

从此,所有进程都将终止,IIS应用程序池也将关闭。表中也可能有不明确的多条记录


我该怎么分类呢

最有可能的情况是,在该工作线程中引发多个未经处理的异常会导致快速失败保护功能。 简单地说,在一个可配置的时间跨度内,有足够多的未处理异常(默认值为5分钟内5次)将关闭应用程序池,导致503服务不可用响应。 请检查应用程序池中的高级设置。 要解决这个问题,我们需要处理异常或防止抛出异常。 请参阅下面的讨论。

应用程序池回收、断电等各种情况都可能导致进程突然退出,甚至在受控情况下,asp.net也不知道您的线程。最好将电子邮件排在数据库、消息队列等的某个位置,让非asp net的东西从该队列中提取电子邮件请求并可靠地发送电子邮件。显然是故意的。@Damien_不信者感谢你的评论。使用队列可以获得性能增益,就像拥有多个线程一样。如果没有多线程,我不会发生这个错误。你可能会发现这很有趣:谢谢你的回答。是的,我捕获了异常,再次使用throw关键字的全部目的是停止我创建的工作线程并加入主线程。有没有别的办法呢。如果是这样,我不想在捕获异常后再次抛出它。