473,320 Members | 1,857 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

simple thread question

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

Apr 9 '06 #1
9 1096
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.

Apr 9 '06 #2
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?

Apr 9 '06 #3
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
"Tim" <Ci************@gmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.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?


Apr 9 '06 #4
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

Apr 10 '06 #5
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
Apr 10 '06 #6
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
Apr 10 '06 #7
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:
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


Apr 10 '06 #8
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.

Apr 10 '06 #9
Add this inside your do loop.
Threading.Thread.Sleep(100)

Apr 11 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: adeger | last post by:
Having trouble with my first forays into threads. Basically, the threads don't seem to be working in parallel (or you might say are blocking). I've boiled my problems to the following short code...
38
by: jrlen balane | last post by:
basically what the code does is transmit data to a hardware and then receive data that the hardware will transmit. import serial import string import time from struct import * ser =...
1
by: Jeff Yang | last post by:
These days,I have read the artile "Safe, Simple Multithreading in Windows Forms, Part 1" on MSDN,and I also runed the example of it....
3
by: Peter | last post by:
Hello Thanks for reviewing my question. I would like to know how can I programmatically select a node Thanks in Advanc Peter
3
by: Elliot Rodriguez | last post by:
Hi: I am writing a WinForm app that contains a DataGrid control and a StatusBar control. My goal is to update the status bar using events from a separate class, as well as some other simple...
1
by: Brian Henry | last post by:
Hello, I was tring to learn socket's (being i never used them before) and have a simple question. I want to create a listner that will get any data recieved and print it out. I've been able to...
73
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an...
3
by: Ryan Pedersen | last post by:
I have a small program that executes a single very long running background thread today. The scope of all variables in the method that is running in this thread today are all local to the...
176
by: nw | last post by:
Hi, I previously asked for suggestions on teaching testing in C++. Based on some of the replies I received I decided that best way to proceed would be to teach the students how they might write...
17
by: Chris M. Thomasson | last post by:
I use the following technique in all of my C++ projects; here is the example code with error checking omitted for brevity: _________________________________________________________________ /*...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.