我正在开发一个使用 plain 实现的项目threads。应用程序中的大多数预期异常都会得到处理,但是,在某些情况下,其中一个线程会引发意外异常,并且应用程序会崩溃(应用程序是基于两者的,I/O因此Client-Server实际上不可能处理所有异常)。
为了解决这个问题,我尝试定义一个全局变量UnhandledExceptionHandler,以便应用程序显示友好的消息而不是崩溃。这是我尝试过的:
public partial class App : Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
// The rest of the startup logic goes here
}
void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Utilities.DisplayUnhandledException((Exception)e.ExceptionObject);
}
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用。从未被CurrentDomain_UnhandledException调用过。不幸的是,我无法更改应用程序的结构,这意味着我无法使用任务并行库。我不明白为什么这不起作用。还有其他方法可以处理线程中抛出的异常吗?任何帮助表示赞赏。
我有一个 Windows 服务,我需要在一天中的特定时间运行。假设时间 id 11:00 PM。目前我有代码每天运行此服务,但如何添加时间变量到此我无法得到那。这是我的 c# 代码..
protected override void OnStart(string[] args)
{
timer = new Timer();
timer.Interval = 1000 * 60 * 60 * 24;//set interval of one day
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
start_timer();
}
static void timer_Elapsed(object sender, ElapsedEventArgs e)
{
// Add your code here
readDataFromAd();
}
private static void start_timer()
{
timer.Start();
}
Run Code Online (Sandbox Code Playgroud)
请帮我定义时间和间隔。时间应该是晚上 11:00,计时器应该每天执行方法。