c# - 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()。任何帮助,将不胜感激。
解决方案
推荐阅读
- reactjs - 接受多个函数签名的正确方法
- reporting-services - Reporting Service Pagination Problem with IE
- python - 在 Jupyter Notebook 中使用 Nominatim 时出现配置错误
- omnet++ - 静脉中[*]和[*0]的区别?
- python - using decimal.get_context().prec gives error
- c# - 从字符串中获取正则表达式
- css - 如何在 Primng 组件上放置 img 或 svg insted 图标?
- javascript - webpack 无法解析子组件中的材质 ui 按钮
- video - 视频上的文字水印,不损失音频和视频质量
- python - 使用 Python 从 MongoDB 文件中检索最近 n 天