|
Excellent article but you have used GOTO
But i shall replace that with a function,
Thank you so much for sharing your experience with everyone
|
|
|
|
|
Hi Rakesh,
Luved it ....
I am a beginer to VB.net ...
Was trying to access a listview on a form after starting a thread .
Tried hard and couldnt find any help anywhere . First, I thought as in VB.net, it all about object, I am not able to access a object in one form from another form.
Tried searchign on the net, but for my dismay .. there was nothing on the Internet.
I nearly lost hopes when one of my friend told me thread dont allow to access a text or list view ..
I tried hard .. but no solution ... UNTIL I got ur code ..
It was superb, well explained ... and everyhting that a beginner needs..
Today I understand, why Indian economy has improved becoz of IT. Its becoz of people like YOU.
hats Off buddy ... thanks heaps ..
Please keep posting articles like these so that people like me can improve in .net.
Thanks again ..
rodney
|
|
|
|
|
Dear Mehta,
Based on your code, in clsThread > StartThread() Sub, the thread is active, because it continuously checking parameter from caller form (Form1) by using "While True".
I want to know is there any method to pass parameter into thread, which is the thread is passive. So, the thread does not need to check parameter continuously from caller form.
Many thanks for your helping.
Best regards,
Bondan Wisnuwardhana
|
|
|
|
|
Hi Bondan,
I really did not understand what are you trying to accomplish. Better if you can write in a bit details..
|
|
|
|
|
Dear Mehta,
Based on your code, in class clsThread.vb, Sub StartThread(). The thread is continuously checking argument from Form1 by using "While True". I call that the thread is active, because it continuously check whether any argument change in Form1.
I call passive thread, is thread that does not check argument from Form1. If there any change argument in Form1, the Form1 is automatically send that argument to clsThread.vb. So, clsThread just receive argument from Form1. clsThread does not check argument from Form1.
Many thanks for your helping.
Best regards,
Bondan Wisnuwardhana
|
|
|
|
|
Hi Bondan,
Now I understand what you are trying to do.. You are having a confusion over here.
Alaways keep one thing in your mind. i.e
A thread is a function that run continously. If we dont need a continuous loop (which can hang our program), then why do we need a thread?
Is not it? If there is no loop in the threaded function, then as soon as the execution of the function completes, the thread will die.
If I have understood properly, then what you are trying to do is.. If anything happnes on your form you want to notify someone to do some other task.
If this is the case, then you will have to use windows sendmessage, postmessage api to communicate between two processes. Notice here, its not thread, its two different process.
For example, you may have 2 projects, 1 is with GUI and another without GUI, can run as service or with a hidden form. The GUI less project sleeps, while it does not have to do anything. So its an IDLE process, and does not use CPU. Now when something happens on your main application you can send a message to your GUI less process to do something..
Instead of using process, you can also acheive the same using a hidden form..
For example, whatever you are trying to do within the threaded function, you write all the codes inside that hidden form.. and you can send a message to that hidden form, from your main form using that hidden windows handle (FORM.handle) and sendmessage or postmessage API
Hope this helps you a bit
|
|
|
|
|
Hi Mehta,
I need your help again Based on your example, I want to make class library. I need to convert form1 as VB class, but in clsThread there is public function "Invoke" that only associated with form. How to modify thread to satisfy my requirement?
Many thanks,
Bondan Wisnuwardhana
|
|
|
|
|
Dear Bondan,
If you have understood the technique of creating a thread and how to call a method (by a pointer to the method) from that thread function then you should understand that FORM is alaways not necessary for multithreading. My example was just to give an idea of threading. You can also pass a pointer to a method to your thread class. Is not it? and then can invoke that method from within your thread method. Try to learn about delegate, you need that. Throw away my code, use your own idea with the learnings and I am sure you can achieve your goal.
Best Regards
|
|
|
|
|
Dear Mehta,
I want to know how to pass parameter into running thread? Based on your example, here is the case :
1. Thread 1 is running.
2. Textbox is filled, manually by user, with a number.
3. Then the label counter start automatically from the number that filled from the textbox.
4. How to update the m_Counter whereas thread is running?
Many thank for helping.
Bondan Wisnuwardhana
|
|
|
|
|
Thread is nothing, but to run a function seperately, where you want the function to do a continuous job wihtout blocking your application.
Say you are running an infinite while loop or something inside your thread method.
While the program pointer is looping through your while loop offcourse you can not change anything inside that method..
If you really need to do that, you will have to plan yourself..
Something like creating a global variable (then the main application as well as the thread, both will refer to the same variable). Then if you change the variable value from the main application, then offcourse the thread will also read the updated value in its next loop or so.
If you are using my example class approach, then while you are creating objects of the thread class, then you must store the ref. of those objects in an arraylist or somewhere. So anytime you can change any property or public member of that object using its ref.
Hope this helps you
|
|
|
|
|
Many thanks for the clues
|
|
|
|
|
Dear,
I am newbie in multithreading programming and the code is very helpful . Now I will make library from that code. I want to make library from "clsThread" file, and I create new library project based on clsThread. But in "Private m_MainWindow As Form"(clsThread line 17), "Form" does not recognized by Visual Studio (I use Visual Studio 2008). Could anyone help me how to solve that problem?
Many thanks...
Bondan Wisnuwardhana
|
|
|
|
|
1. Add System.Windows.Forms as the reference to your projct
2. At the top of your class import system.windows.forms namespace
imports System.Windows.Forms
Thing should work for you
Cheers
Rakesh Mehta
Project Manager
Silicon Valley Infomedia Pvt. Ltd.
|
|
|
|
|
Dear,
Thank you for helping . But in "Public Sub New(ByVal ThreadIndex As Integer, ByRef MainWindow As frmMain)", the frmMain is not recognized. So "m_NotifyMainWindow = AddressOf MainWindow.ReceiveThreadMessage" could not identified too. I think it because of frmMain (as caller form) is not in the library. How to solve that problem?
Many thanks...
Bondan Wisnuwardhana
|
|
|
|
|
this line has been repeated in button4, button6 as well as button8's onClick events....
instead they should be
"If Not m_ThreadList(2) Is Nothing Then If CType(m_ThreadList(2), Thread).IsAlive Then"
&
"If Not m_ThreadList(3) Is Nothing Then If CType(m_ThreadList(3), Thread).IsAlive Then"
in button6 & button8 respectively.
|
|
|
|
|
This is the first time I'm seeing a GOTO outside of error handling since 1983.
|
|
|
|
|
I use mutithreading in my webservice and call it in my live website.but when i made more than one search it's giving error 'Service Unavailable'.sometimes i didn't get results.
ushagupta
|
|
|
|
|
That is because the server may not be responding or may have kept a check for spammers.
Sikunj
|
|
|
|
|
Dear all,
After studing the example and applying this technic successfully into my own project, I have the following question. How to start a main thread withs starts for instance 4 other threads? For example: in the explained example each button starts a single thread, each single thread does its job (counting in this case) and reports to the main window, which updates the textboxes with the new counting value. Now I like to create a case where only one button starts a main thread, which starts four counting threads. Each counting thread count independenly from the other threads and with a different delay. In other words counting thread one increments its counter every 500ms and counter thread two increments its counter every 1000ms and so on. The idea is that starting from a main thread is that a cancel button on the main form can abort the main thread, so that all other four counting threads are aborting with it (well thats what I hope ).
Does anyone have an idea/suggestions/tips to help me solve this issue? I have to say that I am not an expert in VB.net, however I am well motivated to become one.
Thanks in advance,
Raymond
|
|
|
|
|
I know it's a little late for the reply, but here is my suggestion.
Based on the example code, you will need to do the following:
<br />
Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load<br />
For i As Integer = 1 To 4<br />
Dim objThreadClass As New clsThread(i, Me)<br />
objThreadClass.WaitPeriod = i * 1000<br />
Dim objNewThread As New Thread(AddressOf objThreadClass.StartThread)<br />
objNewThread.IsBackground = True<br />
objNewThread.Start()<br />
m_ThreadList.Add(objNewThread)<br />
Next<br />
End Sub<br />
In clsThread you will need to add a public variable called WaitPeriod like this
<br />
Public WaitPeriod As Integer<br />
NOTE: Although the preferred method would be to use a property instead of a public variable.
Finally, change the Thread.Sleep() call in the StartThread() method of clsThread to the following:
<br />
Thread.Sleep(WaitPeriod)<br />
What this will do, is create 4 threads when your application loads, and start all 4 of them with a 1 second interval difference (1st thread will have a WaitPeriod of 1 second, 2nd thread will have a WaitPeriod of 2 seconds, etc)
To stop all the threads at the same time, you can add a button to the form and put this in the Click event
<br />
For i As Integer = 0 To m_ThreadList.Count - 1<br />
CType(m_ThreadList(i), Thread).Abort()<br />
Next<br />
I do however recommend that you do some error checking in this function, exactly as Rakesh did in all his other functions.
|
|
|
|
|
Hello, i am a regular visitor of code project and i am new in multithreading application hear it's a nice application but it was created in a newer version of .net and we are using framework 1.1.4322 so i am not able to opena an applicaion so would you help like can you send it the related version code of multithreading thanks in advance.
Regards, Rajesh Champaneria
|
|
|
|
|
I have code that uses 'events'. When data comes in over the internet, the event fires. And inside the event I want to write the data to a buffer.
But I also want to be able to read from that buffer in a different routine.
I'm worried that the 'events' will fire while I'm trying to read the buffer.
Is this the sort of problem that threading solves?
If so, how would the code you've supplied be adapted to protect a buffer?
Thanks.
Gidster
|
|
|
|
|
1. I have save detail method on server which take long time to dave data in database because it is inserting 10000+ records each time and my frond end application hangs for 2-3 minutes.
Through safe multi threading i want to achvive this task and I also want to know how many threads are running on server because this methods might be call from other user.
Can i also get % of complition of task by each thread?
please post your answer
|
|
|
|
|
Hi,
you can easily the code given in the sample to accomplish your task. Just you have modify the code to use with your project..
1. Simply make a Class with 2 methods.
a> sub new()
b> StartProcess() -> This methods will have the loop which will insert the rows in the database
2. Create a delegate (you will find in the sample code) in this class, which will point to the tread message receiving method of the form.
3. Within the loop, you can invoke the delegate on the form and can send the loop counter to the method in the form.
4. Create the object of this class from within the form. And dont forget to make a Method in the form to receive the message from the thread (for which you have created a DELEGATE on the class. In the sample code you will find the method, which is getting message from the thread.
5. Now create an object of the class and make a thread with the address of the StartProcess method of the thread and then start the thread
6. You front end form will start the thread, and the thread will start it work. Everytime it will go through the loop, it will post the message to you form.. and inside that method you can show the prgress of the thread.. when ur loop will be finished in the class.. pass a message to the form indicating thread has been finished.. and after receiving the message on the form.. simply kill the thread.
If you need anything more.. plz let me know
Rakesh Mehta
Project Manager
Silicon Valley Infomedia Pvt. Ltd.
|
|
|
|
|
I am facing some issues in multi threading. I am using multi threading concept for sending bulk emails. If you share me your skype id, I will make a call to you and you can check it directly.
Please help me in this regard.
HARIRAM
9847633644
|
|
|
|