程序无响应,直到功能完成 [英] Program unresponsive until function is done

查看:121
本文介绍了程序无响应,直到功能完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Visual Studio,C ++,Form应用程序

(我是Visual C ++新手)



I有一个应用程序,它有一个按钮,其功能非常复杂,大约需要一分钟才能完成。

另外:只要该函数运行,程序就会变得无法响应。当this-> Refresh()时窗口被更新。被调用,但是当我点击某个窗口时,它甚至不再更新。该功能继续运行,直到完成其工作,然后一切恢复正常。



我希望能够点击停止按钮。

p>

我该怎么做?

解决方案

这是因为主线程一个Windows程序运行消息泵(这就像绘制屏幕一样),只要你的函数运行,就可以阻止这种情况的发生。你需要做的是启动一个执行长时间运行任务的线程,当任务完成后,你可以回发到主线程来更新UI。我多年来一直没有进行Windows编程,但这是我记得的一个非常普遍的模式。

Visual Studio, C++, Form application

(I'm new to Visual C++)

I have an application which has a button who's function is very complex and it takes about a minute to finish.

Also: As long as the function runs the program becomes unresponsive. The window is updated when this->Refresh(); is called, but when I click somewhere into the window, then it's not even updated anymore. The function continues to run until it is done with its job and then everything is back to normal.

I want to be able to click a "Stop" button.

How do I do that?

解决方案

It's because the main thread of a windows program runs the message pump (which does things like painting the screen) and as long as your function runs you're blocking that from happening. What you need to do is launch a thread that does your long running task, and when the task completes you can post back to the main thread to update the UI. I haven't done windows programming in years but this was a very common pattern from what I recall.

这篇关于程序无响应,直到功能完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆