捕获 Environment.Exit() 上的异常

发布于 2024-07-21 08:09:21 字数 232 浏览 9 评论 0原文

我的 Main 函数结束后需要返回退出代码 1。 但是我有另一个线程永远不会结束(while(true))。 所以我设法调用Environment.Exit(1)。 但是在处理 com 对象时我遇到了一些异常...

由于多种原因,我无法更改其他线程代码。 你们主张做什么?

我可以捕获来自 com 对象处理的异常吗? 我还有其他返回退出代码的选择吗?

I need to return an exit code of 1 after my Main functions ends. However I have an other thread that never ends (while(true)). So I managed to call Environment.Exit(1). But I got some exception when disposing com objects...

For several reasons I can't change the other thread code. What do you guys advocate to do?

May I catch the exception coming from the com object disposing? Do I have an other option for returning an exit code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼泪也成诗 2024-07-28 08:09:21

我建议您:附加到 ProcessExit 事件以进行最后一次清理,或者...在线程上执行 Thread.Abort 以允许其在退出之前完成。

AppDomain.CurrentDomain.ProcessExit +=
delegate(object sender, EventArgs e)
{
    Console.WriteLine("Process Exit");
};
Thread t1 = new Thread(new ThreadStart(delegate
{
try
{
    while (true)
    {
        Console.WriteLine("test 1");
        Thread.Sleep(500);
    }
}
finally
{
    Console.WriteLine("Terminating t1");
}
}));

Thread t2 = new Thread(new ThreadStart(delegate
{
try
{
    while (true)
    {
        Console.WriteLine("test 2");
        Thread.Sleep(500);
    }
}
finally
{
    Console.WriteLine("Terminating t2");
}
}));

t1.Start();
t2.Start();
Thread.Sleep(2000);
t2.Abort();
t2.Join();
Environment.Exit(1);

I suggest you to: attach to ProcessExit event to do any last chance cleanup or... do a Thread.Abort on the thread to allow it to finish before exit.

AppDomain.CurrentDomain.ProcessExit +=
delegate(object sender, EventArgs e)
{
    Console.WriteLine("Process Exit");
};
Thread t1 = new Thread(new ThreadStart(delegate
{
try
{
    while (true)
    {
        Console.WriteLine("test 1");
        Thread.Sleep(500);
    }
}
finally
{
    Console.WriteLine("Terminating t1");
}
}));

Thread t2 = new Thread(new ThreadStart(delegate
{
try
{
    while (true)
    {
        Console.WriteLine("test 2");
        Thread.Sleep(500);
    }
}
finally
{
    Console.WriteLine("Terminating t2");
}
}));

t1.Start();
t2.Start();
Thread.Sleep(2000);
t2.Abort();
t2.Join();
Environment.Exit(1);
风铃鹿 2024-07-28 08:09:21

尝试使用:

Environment.ExitCode = 1;

但是; 如果另一个线程不是后台线程,您将需要它退出才能结束进程(在进程退出之前退出代码毫无意义)。

Try using:

Environment.ExitCode = 1;

However; if the other thread is not a background thread, you will need it to exit in order to end the process (the exit code is meaningless until the process exits).

于我来说 2024-07-28 08:09:21

您可以通过编写此代码来终止进程
Dim myProcess As System.Diagnostics.Process = System.Diagnostics.Process.GetCurrentProcess() myProcess.Kill()


环境.退出(1)
是终止所有进程的最佳方法

http://www.zhakkas.com/affiliates /idevaffiliate.php?id=542

you can kill proccess by writting this code
Dim myProcess As System.Diagnostics.Process = System.Diagnostics.Process.GetCurrentProcess() myProcess.Kill()

or
Environment.exit(1)
is best method for kill all processes

http://www.zhakkas.com/affiliates/idevaffiliate.php?id=542

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文