|
|
Sorry,
Did not get enough time to reply you....
I'll re-post the code here for you tomorrow
Rakesh
|
|
|
|
|
I am new to multihreading. I understood the theory on multithreading from your explanations in the code. Thanks a lot for it. After starting the counter by pressing start button, when I check the checkbox to stop the counter while executing your code in visual studio 2008, I get an exception saying "format exception was unhandled" at "m_MainWindow.Invoke(m_NotifyMainWindow, m_Args)". Please help me out in overcoming this issue.
Thanks,
Rajath
|
|
|
|
|
Hi,
I think, I'll leave it for you. You are trying to learn multithreading, so try to solve yourself That must be a good start for you...
For clue, the error is in the callback method (The method that is getting called from the thread). Put a try catch inside that method, and put a break in the catch statement and try to resolve it
Let me know how do you go with this...
Best of luck !!
|
|
|
|
|
Sorry I found the solution myself. We need to specify the counter in the textbox where the counter has to be stopped and then the checkbox has to be checked. If we do not specify the counter in the textbox and check the checkbox, then we get the exception that I previously posted. But still this issue can be handled by the owner of this form by displaying the appropriate message in the messagebox. Yes I will do it and let you know.
Thanks a lot for the code and thanks for this forum.
Rajath
|
|
|
|
|
Public Sub StartThread()
While True
m_Counter = m_Counter + 1
'we need to create this array such a way, that it should contains the no of elements, that we need
'to pass as the arguments.
'Here we will pass two arguments ThreadIndex and Counter, so we took the array with 2 elements.
'Now we need to place the variable to the appropriate position of this array.
'Like : Our First Argument is ThreadIndex, so we will put ThreadIndex into the first element and
'm_counter into the second element.
'Basically you have to put the variables into the array with the same sequence that is there in the
'argument list
ReDim m_Args(1)
m_Args(0) = m_ThreadIndex
m_Args(1) = m_Counter
Try
'Call the notificaiton method on the main window by the delegate
m_MainWindow.Invoke(m_NotifyMainWindow, m_Args)
Catch ex As Exception
Dim result As DialogResult = MessageBox.Show("Enter a value in textbox", "Value Required", MessageBoxButtons.OK)
If result = DialogResult.OK Then
GoTo jump
End If
End Try
'wait for some time before continuing loop
jump: Thread.Sleep(1000)
End While
End Sub
Is this fine?
I have added the try catch statement.
|
|
|
|
|
Hi rajath,
You have used the Try-Catch inside the threaded method. I asked you to put inside the callback method, that means that method on the form, (which are being called from the thread).
But this will work for you too. And as you know you have already got the problem that you did not specify a numeric value for the tread stop input box. Actually as this was a demo project, I did not do all the validation. The project was just to demonstrate
And yes, it is better to use try catch inside your winform methods, avoid using try-catch inside threaded methods.
Rakesh
|
|
|
|
|
I have to do migration from VB6 to VB.net where i need to convert one portion of the code to vb.net using multithreading.That portion of the code in VB 6 is calling a function from the dll(which is having lot of parameters)I need to convert the code to vb.net by passing 10 threads.When i googled i came to know that it is difficult to pass parameters for the procedure.
My question is that is it very difficutl to pass parameters while multithreading?In my migration where i convert one portion of the code (in which a function is referrenced from dll)will i be able to implement the passing of parameters do i need to think of some other solutions.Can we pass the parameters using delegates inside a class where the thread is passed?
|
|
|
|
|
Hi Krithika,
Its not impossible to pass parameters to a threaded function. If you look at the soruce code of this article, you will find that I do pass a parameter (ThreadIndex) to the threaded function. But in that case it is suggested that you create a call and put the function inside that class and make some public variables in that class. Now the idea is...
1. create an object of that class
2. set the public variables (these are your parameters)
3. then invoke of the method of the object.
So when the mthod inside that object will run in a seperate thread it can uses the variables of the objects that you have already set.
Hope this will help you....
Rakesh
|
|
|
|
|
Hi Rakesh,
Thank u so much .I think the reply which u gave will help me and i will implement it and see
|
|
|
|
|
I heard that we can use a background worker class in net2.0 and pass parameters .But am not familiar with this background worker much .Will it be easy to do it or in the way u suggested will be an easy method to implement.As am a beginner i dont want to catch up with complex solutions can u suggest me with the logic of it.
|
|
|
|
|
Hi krithika,
Sorry for replyaing a bit late...
Yes BackgroundWorker is also a good readymade class in .NET. But it has some limitation. For example, if you have a method that need to finish a job at the background and at the end of the job it need to notify the main program (by calling a callback function), in that scenario its good and you can use it. But if you need to notify the main application from within the threaded method frequently, then BackgroundWorker will not work for you. For example, you have a method, that needs to run at the background and whenever something occurs it needs to notify the main thread and continue running.
Its possible to notify the main thread about the progress of work inside the main thread, but its a not custom. Its only a numeric value. But we really need to notify the mainthread sometimes with some data. In that chase using Thread class is suggested. Now depending on your project / goal, that you want to achieve, you will have to decide which one you need to use - BackgroundWorker or Thread
Here is the details of BackgroundWorker on MSDN
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx[^]
|
|
|
|
|
Dear Rakesh. I need some more information in this multithreading. I need your help to make necessary changes in my work. How I can contact you directly? Please send me a mail to realva.hariram@yahoo.com or please make a call to 09847633644
Thanks and Regards,
HARIRAM
|
|
|
|
|
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
|
|
|
|
|