|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
simple thread questionif 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 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. 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? 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? > 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 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 Tim wrote:
> I have a routine building a csv file. I'm doing this ahead of the user Does it really take that long to create the csv file? If you're building it > 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. 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 Andrew,
> Does it really take that long to create the csv file? If you're building Almost impossible that the user is able to click on a button during that > 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. > 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 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. Add this inside your do loop.
Threading.Thread.Sleep(100)
appending colored / fonted <?> text to a richtextbox
StringBuilder termination char 32bit image quality in menus and tool bar strips VB2005 - Secure Access to SQL Server through Application Only Drawing points in degrees!!! Drawing a square (or circles) before each item of a treeview 'System.Security.SecurityException' with .NET 2005 Only After 2003 to 2005 Upgrade Wizard, Getting System.Security.SecurityException Looking for a License Package mousehover |
|||||||||||||||||||||||