Generating Threads at Runtime.

Rakesh Rao

Member
Joined
Oct 1, 2005
Messages
18
Programming Experience
Beginner
Hay all!
Am trying to generate some thread at runtime; with a new name each time, in my vb applications. As all the threads will access a same public sub, i also want to use some control on the threads.. like if a thread is processing the other must wait.

Is somebody can help me out, please do it fast.
Thanks in Advance.
 
You can check a thread's ThreadState property to see if it is running. If you need to pause a thread, use the Suspend method. To restart it, use the Resume method.
 
SyncLock

Are you looking something like this?...

Dim lockObject As New Object

Private Sub CreateNewThread()
Dim t As New Threading.Thread(AddressOf ThreadFunction)
t.Name = "ThreadName:" & (
New Random).Next
t.Start()
End Sub

Public Sub ThreadFunction()
SyncLock lockObject
Dim hc As String = Threading.Thread.CurrentThread.Name
For i As Integer = 1 To 100
Debug.WriteLine(hc & " " & i.ToString)
Next
End SyncLock
End Sub

If u remove the SyncLock block threads can execute the logic without waiting for other threads to finish. But with SyncLock threads has to wait till other threads finish the job
 
Am just not kidding .. i know these basic properties. So please don't waste time over that. Send something Realistic if u can.
Any way, thanks for your efforts.
 
You hav sent a quite good stuff. But please mention some description with that. As am not clear as i hav to call that function every time or it will run as a constructor or what?
Please clarify the working with some description. I hav used synclock already that is clear. But i want to generate around 60 parellel threads at the same time without any hang up in my applications. All these will access a same function. (like ThreadFunction u hav given).
So how i'll be able to
1. Generate a required random no of functions(i.e. sometime 50 , sometime
60) with that? And
2. Can i provide each thread with a different name and parameter(i.e eachtime a new string passed to the function.)?
Thanks a lot for your help and efforts . Please clarify these above too.
 
In the first place, you havn't mentioned what actually u required,, as in the second reply u posted, and if u think we're just wasting our time by helping others u better not to post qs and answers urself,.. I replied according to what u asked,.. how the hell i know u know basic properties or not... ur question should be descriptive to find better answers...

so i think should not waste my time on answering ur questions

but by lookin at the given sample why can't u think how to create multiple threads at runtime (b'cos u said u know basic properties...) and why can't u understand how to store and retrieve thread specific data...(b;cos u know basic properties)
 
Hey Vinjaya,
I think there is some misunderstanding in between. Cause wat u replied .. is somewat corresponding to the post i replied to another member ..named(mjb3030) whose topic did'nt made a sense for me. But the topic u sent and the example was quite helpful. and i also appreciated ur help. May be the post has been sent to u by mistake. So plz do not mind it, and Make a further response to my question. I need a futhter description of the code sent by u.
Am sorry for wat has happened.
 
Well to generate number of threads what u have to do is call the CreateNewThread function in a outside loop with all given data u need

and also apart from the thread name u can store data (thread specific TLB)
using t.SetData() and you can retrived the data stored in the thread using t.getData() so inside the Thread function u can call Threading.Thread.CurrentThread.GetData()
 
Thanks for this reply.
I hav read about threads upto a normal level. But i don't know how to store data and how to retrieve the data from a thread. Will you please clarify that with some simple example.

Actually wat am doing is.. am making an application for stocks. (viz. NASDAQ stock exchange rates, sale, purchase of shares etc.)
So, i hav to rapidly fire the threads .. and these threads make a web requests. The responses are stored in text files and then datasets are made. Finally the results are reflected to application.
But in this long process ... i don't know why some unpredictable errors occur? May be of using threads. cause sometimes it say that the thread is running . Sometimes it says it can not be resumed etc. So will you please mention me an example that can tell me a clear way to control and use the threads so that no conflics occur. and the cause of generating the threads at runtime is that i want to run a seperate thread to get the rates thru web for each symbol like microsoft, google etc.
Please reply as fast and as proper you can.
Thanks for responding.
Thanks.
 
Quick reply is Check MSDN for Thread.GetData and Thread.SetData where you can save and retrieve thread specific object (so u can save anything u want)
 
Back
Top