MessageBox无需停止执行 [英] MessageBox without stopping the execution

查看:128
本文介绍了MessageBox无需停止执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在一个非常大的文件中进行一些操作.因此,这需要花费大量时间,并且我想允许用户取消循环.我以为解决方案与使用MessageBox有关,但是如果用户不按任何按钮,我就找不到让程序运行的方法.

请帮忙.

Javi.

Hi,

I''m doing some operations in a very big file. So it takes a lot of time and I want to allow the user to cancel the loop. I supposed the solution has to do with using a MessageBox, but I can''t find the way to let the programme running if the user don''t push any button.

Please, help.

Javi.

推荐答案

好,我要坚持桨了.

关于OP的问题,我建议仅向应用程序添加一个无模式对话框,如果用户单击其按钮,该对话框将中断该过程.这在C ++/MFC中是相当标准的.我认为不需要线程,除非应用程序在处理文件时还有其他工作要做.
OK I''m going to stick my oar in.

From the OP''s question I would suggest just adding a modeless dialog to the app which will interrupt the process if the user hits its button. This is fairly standard in C++/MFC. I see no need for threads unless the app has other work to do while processing the file.


您需要确保处理文件的循环在与UI不同的线程上运行.有几种方法可以实现此目的,请参阅我的文章底部的链接以获取示例.通常,只有UI操作(那些与UI直接相关的操作,按钮处理程序,控件初始化等)才应阻止UI线程.此外,UI线程 only 仅可以更新UI(否则将引发异常).所以:

1.在其自己的线程上启动处理"循环.
2.禁用启动"按钮或启动进程的任何操作(例如,UI线程,作为单击按钮的一部分).
3.如果您添加了取消按钮,它将保持启用状态&可点击的.

现在,以下任何一种事情都可能发生:
a)用户按下取消"按钮,在这种情况下,循环应停止,并且应重新启用开始"按钮.
b)循环完成处理. UI线程必须进行更新以向用户报告并重新启用开始"按钮.在Winforms中,这是通过控件上的Invoke 方法完成的(请参见 http://www.yoda. arachsys.com/csharp/threads/winforms.shtml [ ^ ].在WPF上,您需要调用Dispatcher.Invoke.请参见 http://msdn.microsoft.com/en-us/magazine/cc163328.aspx [ ^ ]图4进行指导
You need to make sure the loop processing the file runs on a different thread to the UI. There are several ways to achieve this see the links at the bottom of my post for examples. In general, only UI operations (those directly related to the UI, button hanlders, control initialisation etc) should block the UI Thread. Additionally, only the UI thread can update the UI (otherwise an exception is thrown). So:

1. Start the Processing loop on its own thread.
2. Disable the "Start" button or whatever starts the process (UI thread, as part of button on-click for example).
3. If you have added a cancel button, it will remain enabled & clickable.

Now one of the things can happen, either:
a) The user presses the cancel button, in which case the loop should cease, and the start button should be re-enabled.
b) The loop completes processing. The UI thread must do the update to report back to the user and re-enable the Start button. In winforms this is Done via the Invoke method on the control ( See http://www.yoda.arachsys.com/csharp/threads/winforms.shtml[^] for an example. On WPF you need to call Dispatcher.Invoke. See http://msdn.microsoft.com/en-us/magazine/cc163328.aspx[^] figure 4 for guidance


MessageBox不是解决方案.

有一个非常简单的方法可以做到这一点.通常,Esc键是按下该键以退出进程的键.不过,我不确定C ++到底是什么样子.自从我使用C ++以来已经有一段时间了.

在C#中...以
开头
A MessageBox is not the solution.

There''s a pretty simple way to do this. Generally Esc is the key one would press to quit a process. Though, I''m not sure what exactly it looks like in C++. Been a while since I''ve used C++.

In C#...start with
[DllImport("user32.dll")]
public static extern int GetAsyncKeyState(int vKey);
public const int VK_ESCAPE = 0x1b;



然后,在开始流程调用之前



Then, before you start your process call

GetAsyncKeyState(VK_ESCAPE);



这将清除以前对转义键的所有点击.

然后,定期在您的代码中再次运行该GetAsyncKeyState行,并查看其是否返回true.如果是这样,则用户已按Esc键,则应退出.

另外,我会在表单的某处添加状态栏,以通知用户如果要取消,则应按Esc键.



That will clear any previous hits to the escape key.

Then, periodically in your code, run that GetAsyncKeyState line again and see if it returns true. If it does, the user has hit Esc and you should exit.

Also, I would add a status bar somewhere on your form to inform the user that if they want to cancel, they should hit Esc.


这篇关于MessageBox无需停止执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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