C# Windows 窗体在多线程时不响应

发布于 2024-10-09 22:38:24 字数 603 浏览 4 评论 0原文

我有一个只有两个功能的 Windows 窗体: 第一个是加载数据库中的一些行, 第二个功能是使用给定数量的线程处理这些行,因为处理速度在我的项目中非常重要,这就是为什么我不能考虑不使用多线程的选项。

问题依赖于我想添加停止执行进程的选项,这意味着如果发生任何问题,则停止所有当前线程,以便用户指定另一个线程数并再次重新启动执行,但我的 Windows 窗体似乎冻结并执行当所有线程正常完成工作后,进入 Stopping 方法。

更详细地讲,在处理按钮单击事件时,我正在启动所有线程并且 之后,我

for (int i = 0; i < threads.Length; i++)
{
      threads[i].Join();
}

在按钮单击事件中循环所有线程以停止我

for(int i=0;i<threads.Length;i++)
{
     if(threads[i].IsAlive)
     threads[i].Abort();
}

注意到,如果我评论连接部分,则界面能够响应停止事件单击。

但我不想这样做,因为我想区分所有线程完成执行的时刻,这就是为什么我加入 pa

I have a windows form with just two functionalities :
the first one is loading some rows in a database and
the second functionality is processing those rows using a given number of threads because processing speed is very important in my project that's why i can not consider the option of not using multithreading.

The problems relies that i want to add the option of stopping execution of the process, meaning stopping all of current threads if any problem occurs, so that the user specify another nr of threads and restart execution again, but my windows form seems freezing and execution enters stopping method after all threads have normally finished their work.

In more detail in processing button click event I am initiating all threads and
after that I am looping all threads

for (int i = 0; i < threads.Length; i++)
{
      threads[i].Join();
}

while in button click event for stop I

for(int i=0;i<threads.Length;i++)
{
     if(threads[i].IsAlive)
     threads[i].Abort();
}

i noticed that if i comment the joining part the interface is able to respond to stop event clicking.

But i don't want to do that because I want to distinguish the moment when all the threads have finished executing thats why i put joining pa

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

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

发布评论

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

评论(2

翻了热茶 2024-10-16 22:38:24

我假设您在主线程上执行连接循环。这意味着消息循环将停止,直到最后一个线程完成,从而破坏多线程。它会阻止所有事件的处理,包括按钮单击。

另外,您不应该使用 Thread.Abort()

您将不得不考虑一个更好的计划,

  • 线程代码应该检查停止标志
  • 。如果您绝对需要该连接模式,请从另一个线程执行此操作。也许来自后台工作者。

I assume you do the join-loop on the main thread. That means the message-loop is halted until the last thread finishes, defeating the multi-threading. And it blocks the processing of all events, including button-clicks.

Aside, you shouldn't use Thread.Abort()

You will have to think of a better plan

  • the thread-code should check for a stop flag
  • If you absolutely need that join-patten, do it from another thread. Maybe from a backgroundworker.
甚是思念 2024-10-16 22:38:24

调用 Join 会强制您的主线程等待所有其他线程并抵消您的多线程处理,这正是您不想要的。调用 Abort 会放弃该线程并阻止您从中获取任何结果。不要那样做。

可以设置一个带有回调的线程来返回结果,并轮询以查看是否应该取消该线程,但最简单的方法是使用 BackgroundWorker,这是一个专门为执行您的操作而设计的 .Net 类。正在努力做。 Code Project 有一个很好的教程

Calling Join forces your main thread to wait for all other threads and counteracts your multithreading, exactly what you don't want. Calling Abort abandons the thread and prevents you from getting any results from it. Don't do that.

It's possible to set up a thread with callbacks to return results, and poll to see if the thread should be canceled, but the easiest thing is to use BackgroundWorker, a .Net class designed specifically to do what you are trying to do. There is a good tutorial at Code Project.

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