首页 > 解决方案 > c#线程停止而不抛出错误

问题描述

我在 C# 中有一个长时间运行的线程,它正在停止而不会引发错误。我添加了额外的尝试/捕获来尝试缩小范围,但仍然没有抛出错误。线程是这样设置的:

loggingThread = new Thread(Log)
{
     IsBackground = true
};
loggingThread.Start();

然后 Log 方法设置如下:

private void Log()
{
        log.Error("Logging started");
        stopLogging = new ManualResetEvent(false);
        try
        {
            while (!stopLogging.WaitOne(250))
            {
                try
                {
                    List<PublishValues> valuesToPublish = new List<PublishValues>();
                    List<RecentData> recentData;
                    using (var session = sessionFactory.OpenStatelessSession())
                    {
                        recentData = session.Query<RecentData>().ToList();
                        using (var tx = session.BeginTransaction(IsolationLevel.ReadUncommitted))
                        {
                            foreach (var dataVal in recentData)
                            {
                                Historical lastHistory;
                                if (!LastHistories.ContainsKey(dataVal.Id))
                                {
                                    lastHistory = null;
                                }
                                else
                                {
                                    lastHistory = LastHistories[dataVal.Id];
                                }
                                if (dataValValueNeedsLogged(lastHistory, dataVal.SampleInterval))
                                {
                                    Historical hist;
                                    if (lastHistory != null && dataVal.Value == lastHistory.UnformattedValue)
                                    {
                                        var now = clock.Now;
                                        now.AddMilliseconds(-now.Millisecond);
                                        hist = lastHistory;
                                        hist.LastSampleUtcTime = now;
                                        hist.CheckValue = hist.CalcNt();
                                        session.Update(hist);
                                    }
                                    else
                                    {
                                        var now = clock.Now;
                                        now.AddMilliseconds(-now.Millisecond);
                                        hist = new Historical()
                                        {
                                            dataValId = (short)dataVal.Id,
                                            FirstSampleUtcTime = now,
                                            LastSampleUtcTime = now,
                                            StatusRaw = dataVal.Status,
                                            UnformattedValue = dataVal.Value,
                                            CountSamples = 1
                                        };
                                        session.Insert(hist);
                                        UpdateMrData(hist, session);
                                    }
                                    LastHistories[dataVal.Id] = hist;
                                    valuesToPublish.Add(Convert(hist, dataVal, DateTime.UtcNow));
                                }
                            }
                            tx.Commit();
                            if (valuesToPublish.Count > 0) eventPublisher.Publish<PublishValuesMessage>
                                    (new PublishValuesMessage(dataValValueChanges.ToArray()));
                        }
                    }
                }
                catch (Exception ex)
                {
                    log.Error(ex.ToString());
                }
            }
        }
        catch (Exception ex)
        {
            log.Error(ex.ToString());
        }

}

没有从循环内部或外部记录错误。永远不会设置手动重置事件。它在应用程序中的任何地方都没有调用 thread.abort()。任何帮助,将不胜感激。

标签: c#asp.netmultithreadingservicethreadpool

解决方案


推荐阅读