simple thread question

T

Tim

if I start a thread

mythread.start

and then I click a button that has a do loop with no doevents in the
loop,

do
loop until myval=true

the first thread never finishes as the do loop hogs resources.

question: how can I make mythread continue despite the do loop problem?
and can it be done without using doevents?

thanks
 
C

Cerebrus

Hi,

What does myThread do ? And what's the problem with using DoEvents ?

In my view, one of the purposes of having a multi-threaded application
is to prevent long processes from blocking out the main or UI thread,
and keep the application responsive.

So, why not move your do...loop to another thread, instead of running
it on the main UI thread ?

Regards,

Cerebrus.
 
T

Tim

it's kind of like this...

I have a routine building a csv file. I'm doing this ahead of the user
needing it. A bit of forward planning. I start creating the csv file
when the user pops up a window. they then spend some time entering
data, and click OK.

now if the thread that was creating the csv file has finished, the OK
button works and does its stuff. if the OK button is pressed and the
thread building the csv file is still running, I need to wait until it
is does. So at the moment I just loop until it is. If I put a doevents
in the loop the user can keep clicking everything on the form, and
things get messy. Without the doevents, the background thread never
completes.

any ideas?
 
M

Michael D. Ober

When you start the thread that is creating the csv file, save the thread
variable. Then use it's join method

dim ThreadToCreateCSV as new thread
ThreadToCreateCSV.start()


In you OK handler, use the following statement

ThreadToCreateCSV.Join


Mike Ober
 
T

Tim

thread.join, excellent thank you.
I'll go and read up about it.

If you want to expand a little more while I am away reading that would
be very helpful.

Cheers
 
A

Andrew Morton

Tim said:
I have a routine building a csv file. I'm doing this ahead of the user
needing it. A bit of forward planning. I start creating the csv file
when the user pops up a window. they then spend some time entering
data, and click OK.

now if the thread that was creating the csv file has finished, the OK
button works and does its stuff. if the OK button is pressed and the
thread building the csv file is still running, I need to wait until it
is does. So at the moment I just loop until it is. If I put a doevents
in the loop the user can keep clicking everything on the form, and
things get messy. Without the doevents, the background thread never
completes.

Does it really take that long to create the csv file? If you're building it
using strings then using a StringBuilder may be a much better solution. If
you're writing the file one line at a time then consider building it in a
StringBuilder and writing that to a file in one go.

Andrew
 
C

Cor Ligthert [MVP]

Andrew,

Does it really take that long to create the csv file? If you're building
it using strings then using a StringBuilder may be a much better solution.
If you're writing the file one line at a time then consider building it in
a StringBuilder and writing that to a file in one go.
Almost impossible that the user is able to click on a button during that
time. Or the OP should get the data from something as internet on a 19.2
line and build the CSV direct without getting first the data..

Using a thread for this will consume probably the most time.

Just my thought,

Cor
 
B

Brian Gideon

Tim,

Thread.Join will block the thread from which is called. In your case
it would be the UI thread. That will effectively stop the message loop
and prevent the user from interacting with the form.

A better approach is to have the thread raise an event notifying your
form that its work is complete. Your form would subscribe to this
event before starting the thread. Just remember to use Control.Invoke
to marshal the execution of code onto the UI thread before accessing
any controls on your form.

Brian
 
T

Tim

thank you guys for all your input.

the csv building is actually very quick. the content for the csv file
however comes from a webservice, and might well be several thousand
rows of data (this is the exception. typically only a few rows will be
required).

I tried the .join thing and it worked. I'm also happy that is locks the
form while it waits (that is exactly the functionality I was after)

Brian wrote this
"A better approach is to have the thread raise an event notifying your
form that its work is complete. Your form would subscribe to this
event before starting the thread. Just remember to use Control.Invoke
to marshal the execution of code onto the UI thread before accessing
any controls on your form."

I don't understand it, but it sounds like something I should know about
so I'll investigate further.
 

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

Top