Home All Groups Group Topic Archive Search About

simple thread question

Author
9 Apr 2006 2:51 PM
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

Author
9 Apr 2006 4:05 PM
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.
Author
9 Apr 2006 8:57 PM
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?
Author
9 Apr 2006 10:04 PM
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


Show quoteHide quote
"Tim" <Citizen10Be***@gmail.com> wrote in message
news:1144616252.709666.316330@i39g2000cwa.googlegroups.com...
> 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?
>
Author
10 Apr 2006 8:16 AM
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
Author
10 Apr 2006 3:01 PM
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

Tim wrote:
Show quoteHide quote
> 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
Author
10 Apr 2006 11:46 AM
Andrew Morton
Tim wrote:
> 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
Author
10 Apr 2006 12:34 PM
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
Author
10 Apr 2006 4:16 PM
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.
Author
11 Apr 2006 2:56 PM
mmitchell@capstonetechnology.com
Add this inside your do loop.
Threading.Thread.Sleep(100)