Forms

  • Thread starter Thread starter Burns
  • Start date Start date
B

Burns

Hi,

I asked this question before, but didn't get the answer I
was looking for, so I'm hoping if I re-phrase it it'll
make more sense.

I'm trying to write a MultiReplace program that will
replace a word or phrase in multiple text files. I have
written most of the code to do this, but I want to
display a progress bar to the user as it goes through the
files.

I've designed a simple form with a progress bar and a
cancel button that I wish to display as the replace code
is running.

At the moment I have a main dialog window which allows
the user to specify what they want to find and replace.
When they click the "Replace" button in this dialog, it
creates an instance of my second dialog (the one with the
progress bar) and calls the .ShowDialog method on it.
Now I need to know how to run my "replace" code once the
second dialog has displayed, since the .ShowDialog method
won't return until the second dialog has closed. I know
I could use the .Show method, but I want the dialog to be
modal.

I hope this makes sense.

Cheers,
Burns
 
Hi,

You should use a worker thread to do the find/replace operation while the
main thread is running the progress bar dialog, you also needs some form of
sync between them, basically you must have two events, if the user press
cancel you should inform the worker thread of it, and the worker thread
should inform the dialog when it finish.

Also you said that you use ShowDialog therefore either you create the thread
from the Progress bar dialog itself ( I would do this ) or you have to
create and start it before you call ShowDialog.

regarding of how to do the progress bar, well it's very simple, you just
use a timer and on each interrupt you increment the ProgressBar.Value if it
become the MaxValue you equal it to the MinValue.

Warning:
The code below is just to give you an idea, I'm not working now on my
machine and cannot be sure of the exact signature of the methods/properties
or events, so do not copy it verbatin and rather get the general idea.

I would use two flags ( bool variables ) to indicate both a cancel operation
or a Finish event , you may use events too, but IMHO the flags are easiers.
In the timer handler you will check for the finish flag, if it's then you
are done and can close the form, otherwise you just increment the value, I
used a variable "finito" to this purpose, you declare it as:

bool finito = false;

later I will show you how you set it.

public TimerEvent( object sender, )
{
if ( finito )
{
timer.enabled = false;
this.Close();
return;
}

// check the current value and increment it
progressBar.Value = progressBar.Value + 1;

}


The Cancel handler is extremely simple, just a line:

{
cancelSearch = true;
}

the variable cancelSearch is declared in the same way that "finito"

now, the worker thread is the most interesting part of all, it keeps
searching on the files and at the same time checks for the cancelSearch
flag:

protected void DoSearch()
{

while ( ( (currentfile = GetNextFile() )!= null ) && (cancelSearch ==
false)
{
//do the search
}
//inform the dialog we finished the search, either by cancel or by end
of the file list
finito = true;
}


As you can see , it;s very symple too, just iterate and at the end set the
flag to true. it will be picked up in the next tick event and the dialog
will close itself.




Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
I asked this question before, but didn't get the answer I
was looking for, so I'm hoping if I re-phrase it it'll
make more sense.

A similar answer to this was answered last week IIRC.
 
Back
Top