vb.net backgroundworker

Associate
Joined
8 Mar 2007
Posts
2,176
Location
between here and there
hey all,

i'm trying to get my backgroudworkers, but i need some help.

I've read everything that google can offer but i'm still unsure.

Can anyone offer little jem's of knowledge or know any good websites??

cheers guys
 
well, i have a program (see other thread titled "make vb.net wait 30 seconds")that is growing in size and it would appear that i'm going to need a background worker to do the work and output to a listbox.

I'd like to do it myself and really just want to get a understanding of what and how it works before trying to code it in to my app.

cheers
 
blastman, did you read what I put on the other '30 seconds' thread? Have you tried running the code? It does what you need, unless I've misunderstood you. You don't need a background worker - you effectively already have one - in the process you spawn.

If you want to mess around with multithreading for your own interest, then that's a different matter.
 
topbanana,

I did see your post, and thanks. But no sadly i couldn't get it go. :rolleyes:
I suppose i should have posted back there but a workmate pointed me in this direction and since i have loads of commands to run on each form for each "go" button i thought this would prove better.

Having said that, I really don't care as long as it works and i understand why!!

your code..

Code:
Dim SOut As System.IO.StreamReader = myProcess.StandardOutput

Dim exited As Boolean = False

While Not exited
exited = myProcess.HasExited

While (Not SOut.EndOfStream)
Formstatus.statuslist.Add(SOut.ReadLine())
End While

Application.DoEvents()

Thread.Sleep(1000)
End While

...baffled me as i was unable to supplement in my commands that i need to run.

If i wanted to run the "net use \\" + ipaddress.text + " /user:mine spottydog" command and have it show "commands completd" in my listbox before then running "copy c:\" + file.text + " \\" + ip.text + "\c$\temp.txt" and dislpaying the "1file(s) copied"

How would i go about that??

cheers for your help by the way! :D
 
cheers Conrad, that helped. My understanding is getting better.

I am however still interested in what topbanana has to say on the subject
 
Test the theory for yourself. Create a text file and fill it with a load of junk and use the stream reader to out put the content of the text file using reader.ReadLine() and see if it hangs the main thread. I am gonna say it will. Calling application.doevents() within the loop will help, calling it outside the loop will do nothing until the reader has finished and has exited the loop.

TrUz
 
Creating multi-threaded programs is very tricky and difficult (speaking from experience!).

A backgroundworker is basically another name for a thread.

In VB by default your program will have one thread (called the UI thread).

Now, if you are going to be running a function or section of code that takes a long time to process - this will cause the User Interface (UI) to appear to freeze. To prevent this you create a new thread for the section of code to execute in. This thread will appear to run simultaneously to the UI thread (although it doesn't in reality). To summarise this use is to ensure interface response and this use for threading is quite simple to code.

Another use for threads is splitting a task up. A real life example could be an inbox of order forms in an office(pool of data/text file/database). Processing each order form takes time(a function). One person(thread) could have the job of processing these, or it could be split between many people(threads) - each taking the top form and processing it themselves. Things can then become very tricky if people need to communitcate and exchange information(messgage passing/shared memory). This use for threading is very difficult to code.

In VB threading follows a stick rule set. Here's an example of one basic concept - a newly created thread is not allowed to access any user interface components directly (the thread must invoke the controls). This rule exists to stop the UI thread and other threading colliding with each other.

Multitheading is a huge subject so have a look on google and get the basic concepts and ideas. The articles on MSDN for multithreading are basically as simple as it gets!
 
Back
Top Bottom