C# .NET运行时错误-未处理的异常-使用一个线程中的两个线程时
尝试添加第二个线程(pp)时,EventViewer中出现错误。当我只运行一个线程(dev)时,它工作得很好 这是事件查看器中的错误消息:C# .NET运行时错误-未处理的异常-使用一个线程中的两个线程时,c#,.net,multithreading,C#,.net,Multithreading,尝试添加第二个线程(pp)时,EventViewer中出现错误。当我只运行一个线程(dev)时,它工作得很好 这是事件查看器中的错误消息: Application: Service.exe Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: System.ArgumentException at System.
Application: Service.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.ArgumentException
at System.Diagnostics.EventLogInternal.WriteEntry(System.String, System.Diagnostics.EventLogEntryType, Int32, Int16, Byte[])
at System.Diagnostics.EventLog.WriteEntry(System.String, System.Diagnostics.EventLogEntryType, Int32)
at Service.Service.startRID(System.String)
at Service.Service.<OnStart>b__4_1()
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Threading.ThreadHelper.ThreadStart()
下面是startRID的样子。我添加了一堆catch,并尝试将错误记录到事件查看器中,但它不记录错误
private void startRID(string farmName)
{
try
{
while (true)
{
if (String.IsNullOrEmpty(ConfigurationManager.ConnectionStrings[farmName].ConnectionString))
{
eventLog.WriteEntry(string.Format("Exception in startRID method - no connectionString string value for " + farmName), EventLogEntryType.Error,
(int)EventId.GeneralException);
return;
}
RedisInfoDaemon rid = new RedisInfoDaemon(farmName);
try
{
rid.Start();
}
catch (Exception ex)
{
eventLog.WriteEntry(string.Format("Exception in startRID method - ex " + ex.Message), EventLogEntryType.Error,
(int)EventId.GeneralException);
try
{
rid.Reset(false);
rid.Start();
}
catch (Exception err)
{
eventLog.WriteEntry(string.Format("Exception in startRID method inside- err " + ex.Message), EventLogEntryType.Error,
(int)EventId.GeneralException);
Console.Write(err.ToString());
rid.Reset(true);
Thread.Sleep(TimeSpan.FromMinutes(5));
rid.Start();
}
}
}
}
catch (Exception erer)
{
eventLog.WriteEntry(string.Format("Exception in startRID method - main - err " + erer.Message), EventLogEntryType.Error,
(int)EventId.GeneralException);
}
}
我们看不到startRID的功能,所以我们无法告诉您什么正在爆炸,但是:我们可以告诉您的是不要让异常进入调用堆栈的顶部,因为这会杀死您的应用程序。因此:可以添加一种方法,如:
static void SafelyDoSomething(string key) {
try { startRID(key); }
catch(Exception ex) {
// lots of logging here
Console.Error.WriteLine(ex); // add more than this!
}
}
并使用
新线程(()=>SafelyDoSomething(“Dev”)
/新线程(()=>SafelyDoSomething(“prepod”))代码>等等。这至少应该给你一个错误的线索。但归根结底:它在我们看不到的代码中。强调:这不是一个修复程序-它是一个诊断工具。我们看不到startRID的功能,所以我们无法告诉您什么正在爆炸,但是:我们可以告诉您的是不要让异常进入调用堆栈的顶部,因为这会杀死您的应用程序。因此:可以添加一种方法,如:
static void SafelyDoSomething(string key) {
try { startRID(key); }
catch(Exception ex) {
// lots of logging here
Console.Error.WriteLine(ex); // add more than this!
}
}
并使用新线程(()=>SafelyDoSomething(“Dev”)
/新线程(()=>SafelyDoSomething(“prepod”))代码>等等。这至少应该给你一个错误的线索。但归根结底:它在我们看不到的代码中。强调:这不是一个修复—它是一个诊断工具。EventLog
是。您必须重新编写逻辑。事件日志
是。您必须重新编写逻辑。startRID正在做什么?eventLog
可能不是线程安全的。不要跨线程使用它。startRID
在做什么?eventLog
可能不是线程安全的。不要跨线程使用它。我添加了startRID方法。事实上,我确实有一个关于这些的窍门。我来编辑原稿code@TDeoodfig根据您添加的内容,您需要解决的第一个问题似乎是eventLog.WriteEntry
失败:)可能是权限被拒绝或不存在;但是:不要让事件日志扼杀你的应用程序:尝试/捕获事件日志。WriteEntry
并记录在其他地方我添加了startRID方法。事实上,我确实有一个关于这些的窍门。我来编辑原稿code@TDeoodfig根据您添加的内容,您需要解决的第一个问题似乎是eventLog.WriteEntry
失败:)可能是权限被拒绝或不存在;但是:不要让事件日志扼杀你的应用程序:尝试/捕获周围的事件日志。WriteEntry
并将其记录到其他地方
static void SafelyDoSomething(string key) {
try { startRID(key); }
catch(Exception ex) {
// lots of logging here
Console.Error.WriteLine(ex); // add more than this!
}
}