473,769 Members | 4,584 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1116
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 ThreadToCreateC SV as new thread
ThreadToCreateC SV.start()
In you OK handler, use the following statement

ThreadToCreateC SV.Join
Mike Ober
"Tim" <Ci************ @gmail.com> wrote in message
news:11******** **************@ i39g2000cwa.goo glegroups.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.Threa d.Sleep(100)

Apr 11 '06 #10

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

Similar topics

14
2179
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 block and ensuing output. Seems like the output should be all interleaved and of course it's not. Running Python 2.2 from ActiveState on Windows XP (also doesn't work on Windows 2000). Thanks in advance! adeger
38
3541
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 = serial.Serial()
1
1947
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. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms06112002.asp The question is In function void ShowProgress(string pi, int totalDigits, int digitsSoFar); if I use Invoke(showProgress, new object { pi, totalDigits, digitsSoFar});the UI performs better(it responses to my...
3
2166
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
2384
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 things. The method I am writing queries a large dataset. As part of my feedback to the user, I am updating the status bar when the connection is made and the dataset is actually retrieved. The dataset retrieval method I have placed on a separate...
1
1550
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 get it to recieve only one line of data, but the next one i send to it wont be printed like the 1st one. I had a listner running in a thread, does anyone have a simple listner code example that would show how to have a tcplistner thread running...
73
4626
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 endless loop in a line with: if a==b: print 'OK' I mean, it would be of much help to me on my way to understanding Python to know how such prefix code leading to an endless loop can look like and if it is eventually not possible to write such...
3
1298
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 method. The method doesn't do anything outside of its local scope. My question... can I start up a 2nd (3rd, and 4th and so on) thread running the same method? Is that safe?
176
8458
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 their own unit test framework, and then in a lab session see if I can get them to write their own. To give them an example I've created the following UTF class (with a simple test program following). I would welcome and suggestions on how anybody...
17
5818
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: _________________________________________________________________ /* Simple Thread Object ______________________________________________________________*/ #include <pthread.h> extern "C" void* thread_entry(void*);
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8870
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7406
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.