Click here to Skip to main content
15,893,266 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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.
Posted

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.
 
Share this answer
 
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
 
Share this answer
 
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
C#
[DllImport("user32.dll")]
public static extern int GetAsyncKeyState(int vKey);
public const int VK_ESCAPE = 0x1b;


Then, before you start your process call
C#
GetAsyncKeyState(VK_ESCAPE);


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.
 
Share this answer
 
To do this, you need for your operations to occur in another thread, and to show a modeless dialog box with a button which will kill that thread. If you do it all on the same thread, the operation will kill your UI from being painted, let alone responding.

Everyone else seems to assume you're using C#, but you've tagged it as C++. Am I missing something ?
 
Share this answer
 
v2
To Christian:

Everyone else? I was the only one that had responded when you said that and if you read my response, I said,

"I don't know what this looks like in C++, but in C# it's"

So, no, everyone else didn't think it was C#. Since you're "everyone else" was really just mean, then in fact, everyone else thought it was C++.
 
Share this answer
 
I think the best way is to create a Form like MessageBox and use

FormMSGBox lala = new FormMSGBox();
lala.show(this);

:cool:
 
Share this answer
 
A lot of people already suggested using threads and I completely agree. However, this is not an easy task if you never worked with thread before. I suggest you read this excellent article [^] (it's quite long but it is certainly time well spent and you'll learn a lot from it). It also contains an example which is extremly similar to your (except that it is for image processing and not doing operation on files, but the concept is the same).
 
Share this answer
 
To William:

Your answer seemed to contain a lot of C# specific info, but then someone else posted and was talking about how to do it in WPF, so no, I commented because TWO people seemed to be giving info that was specific to managed code, making me wonder if the question was mis tagged and if you both knew from previous questions that this user is really using C#.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900