长时间运行进程的线程选项

发布于 2024-11-04 17:22:30 字数 275 浏览 0 评论 0原文

哪种线程模型最适合这种情况:我有一个进程可能需要几分钟才能完成,一旦启动,我就无法控制运行的代码。该过程要么出错,要么返回结果。

我想允许用户取消该过程。我不需要进程报告其进度,只需要报告它已完成或出现错误。如果取消该过程,则无需进行清理。

我查看了各种线程选项,并注意到添加取消似乎我需要在运行代码中实现某种取消标志,这在我的情况下是不可能的。添加线程超时也是不切实际的。我考虑过在 AppDomain 中运行我的进程,但感觉这种方法可能是死胡同。

使用 C# 3.5 Winform。

Which threading model is best for this scenario: I have a process can take several minutes to complete and once it starts I have no control of the code that runs. The process will either error out or return results.

I would like to allow the user to cancel the process. I don't need the process to report its progress, only that it completed or there was an error. There's no clean up required if the process is canceled.

I looked at the various threading options and noticed to add cancellation it seems I need to implement some sort of cancellation flag in the running code which isn't possible in my case. It's also not practical to add a thread timeout. I thought about running my process in an AppDomain but get the feel that this approach may be a deadend.

Using C# 3.5 Winform.

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

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

发布评论

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

评论(5

洛阳烟雨空心柳 2024-11-11 17:22:30

实际上,单独的 AppDomain 或 Process 是取消它的唯一安全方法。 Thread.Abort() 非常不安全,不建议这样做。

Actually a separate AppDomain or Process is the only safe way to cancel it. Thread.Abort() is very unsafe, is it dis-advised.

扶醉桌前 2024-11-11 17:22:30

有Thread.Abort(),但不能保证杀死线程。它的作用是在线程中引发 ThreadAbortException,这不一定会杀死线程,具体取决于它正在做什么,就像 finally {} 块一样。

最好放入某种协作标志,让线程检查并优雅地清理并在看到时退出,但如果您没有选择,Thread.Abort() 就可以了。请注意,如果线程代码设置锁等,则它们不会被清除,除非线程具有处理 ThreadAbortException 并清除它们的逻辑。

There is Thread.Abort(), but it is not guaranteed to kill the thread. What it does is raise a ThreadAbortException in the thread, which won't necessarily kill the thread depending what it is doing, like a finally {} block.

It's always better to put in some kind of cooperative flag the thread inspects and gracefully cleans up and exits upon seeing, but if you don't have a choice, Thread.Abort() is about it. Beware that if the thread code is setting locks and such, they are not going to get cleaned up unless the thread has logic to handle the ThreadAbortException and clean them up.

寒江雪… 2024-11-11 17:22:30

在您的线程中声明一个标志(例如 bool Killme)并将其设置为 false。并查看 System.Threading 中的 Timer轮询该标志的值。如果为 true,则退出线程。该计时器需要由线程在初始化期间创建。

它将在您的线程进程中按照您的需要并由线程执行,这意味着线程知道他需要停止并且可以在离开之前执行一些操作(例如关闭打开的文件等)。

Declare a flag in your thread (e.g. bool killme) and set it to false. And have a look at a Timer in System.Threading to poll the the value of this flag. If true, exit the thread. This timer needs to be created by the thread during its initialisation.

It will be executed during your thread process as you want and by the thread, meaning the thread knows he needs to be stopped and can do some actions before leaving (like closing opened files, etc.).

笔芯 2024-11-11 17:22:30

如果您在运行的单独线程中运行进程,则可以调用 Thread。中止。不应该使用 Thread.Abort 的原因有很多,所以请仔细阅读警告。

If you are running your process in a separate thread that you ran up you could call Thread.Abort on it. There are a LOT of reasons why Thread.Abort should not be used though so read the warnings carefully.

小矜持 2024-11-11 17:22:30

快速但肮脏的方法是使用内核和平台调用。

 [System.Runtime.InteropServices.DllImport("kernel32.dll")]
  static extern IntPtr OpenThread(uint dwDesiredAccess, bool bInheritHandle, uint dwThreadId);

 [System.Runtime.InteropServices.DllImport("kernel32.dll")]
  static extern bool TerminateThread(IntPtr hThread, uint dwExitCode);

从OpenThread获取指针并在TerminateThread中使用它。您需要使用线程中的 threadID 作为 OpenThread 的最后一个参数。

请注意,这通常是一个非常糟糕的主意,尽管有时肮脏总比不存在好。

另外,我这样说是因为我假设您不能使用取消标志。如果可以的话,这始终是最好的方法。我的博客上有一个示例。

http://memmove.blogspot.com/2011/04/treating -thread-as-service.html

The quick and dirty way is to use the kernel and platform invoke.

 [System.Runtime.InteropServices.DllImport("kernel32.dll")]
  static extern IntPtr OpenThread(uint dwDesiredAccess, bool bInheritHandle, uint dwThreadId);

 [System.Runtime.InteropServices.DllImport("kernel32.dll")]
  static extern bool TerminateThread(IntPtr hThread, uint dwExitCode);

Obtain the pointer from OpenThread and use it in TerminateThread. You will need to use the threadID from your thread for the last argument to OpenThread.

Be warned, this is usually a very bad idea, though sometimes dirty is better than non-existence.

Also, I say this because I assume that you cannot use a cancelation flag. If you can, that is always the best method. I have a sample on my blog.

http://memmove.blogspot.com/2011/04/treating-thread-as-service.html

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