NEWBIE - Pause loop process with button click

  • Thread starter Thread starter Dave
  • Start date Start date
D

Dave

Greetings,

I have a button on a form that, when clicked, loops through all the words in
a .txt file and displays them on the form one at a time. Some of the text
files are large, and I cannot click any other buttons in my form or anything
until the process concludes.

I want to have a "pause" button that stops that process, but I wouldn't be
able to click it until the loop has finished.

It seems like I have an error in my programming methodology. Can anybody
give me a method I could follow to make this happen? Thanks,

-Dave
 
The dirty way is to add an application.doevents() in your loop. The correct
way to add a thread to handle the processing of the text file.

Chris
 
You my want to look at threading. Define your sub that does the work, ie
getText(), and start the thread when the user clicks the button.

private sub getText()
' does stuff
end sub

private sub button_click(byval sender as syste.object, _
byval e as syste.eventargs) handles button.click

dim workThread as new Thread(AddressOf getText)
workerThread.Start()
end sub

Now you can do other things while the file loads. If you want to cancel the
operation you can add in some additional code for suspending and resuming
the thread, or even cancelling it. Be careful to clean up any unfinished
operations left by the terminated thread though. Also you need to be careful
about allowing more than one thread to start at a time which could have
undesirable effects.

HTH
Dave
 
Chris,

Thank you for your reply.

Can you please point me in the right direction on how to "add a thread to
handle the processing of the text file" like you mentioned in your reply? I
appreciate it.
 
Do you want a "pause" or a "cancel"? Pausing
might be a little trickier because the question
"for how long?" comes up.

Off the top of my head, you might create a custom search class
that includes a "cancel" method that you could call.

The main thing is that you want to kick the search off
in it's own thread. This will give you access to all
of the awesome new threading features in .net

Then, when you call the "cancel" method of your search object,
it could toggle a private variable value. Your search loop
could check the value and bail if needed.
 
Dave said:
I have a button on a form that, when clicked, loops through all the words
in a .txt file and displays them on the form one at a time. Some of the
text files are large, and I cannot click any other buttons in my form or
anything until the process concludes.

I want to have a "pause" button that stops that process, but I wouldn't be
able to click it until the loop has finished.

Multithreading in Windows Forms applications
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=multithreading&lang=en>
 
Chris,

Do events seems to be on this newsgroups dirty, however I don't see what is
the advantage of multithreading, where we should add synchronizing parts to
prevent that the user is not taking to early actions, which are dependent
from the complete reading of the text, before that complete text is read.

I think that we should take that what is at the right moment the right
solution. Threading is that in my opinion surely not forever.

Just my thought.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top