 |
|
 |
Hello Rakesh,
Firstly, great Thank you for helping me understand the threading module.
Have textbox to enter a msg & on Button event click, String entered in the Textbox is added to n_msgstr.
I have declared a Public n_msgstr as String in the form module. I am trying to call the string from the class module `Public Sub StartThread` m_Args(1) = frmMain.n_msgstr
Have changed the counter as string in the Deletegate event.
But doesn't seem to work. Any solution.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi,
Can you please post your code and tell me what you are trying to do actually? I provided the demo project just to understand the concept of threading. If you understand the concept of multithreading then you can do anything.. let me know if you have any confusion or if you have problem understanding anything, also post your code so that I can rectify it..
Regards Rakesh
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
'=============================================== 'Form Code '=============================================== Imports System.Threading Public Class frmMain Public m_ThreadList As New ArrayList Public n_str_Msg As String
Public Sub Start_Thread_Action() If Not m_ThreadList(0) Is Nothing Then If CType(m_ThreadList(0), Thread).IsAlive Then MsgBox("You can not start this thread again. Becoz this thread is still alive !!", MsgBoxStyle.Critical) Else GoTo StartThread End If Else GoTo StartThread End If Exit Sub StartThread: Dim objThreadClass As New clsThread(1, Me) Dim objNewThread As New Thread(AddressOf objThreadClass.StartThread) objNewThread.IsBackground = True objNewThread.Start() m_ThreadList.Item(0) = objNewThread End Sub Public Sub End_Thread_Action() If Not m_ThreadList(0) Is Nothing Then If CType(m_ThreadList(0), Thread).IsAlive Then CType(m_ThreadList(0), Thread).Abort() m_ThreadList(0) = Nothing Me.TextBox2.Text = "" Else MsgBox("No thread is active in this section", MsgBoxStyle.Critical) End If Else MsgBox("No thread is active in this section", MsgBoxStyle.Critical) End If End Sub Public Sub ReceiveThreadMessage(ByVal ThreadIndex As Integer, ByVal str_Msg As String) Select Case ThreadIndex Case 1 Me.TextBox2.Text = str_Msg End Select End Sub
Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing End_Thread_Action() End Sub
Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load m_ThreadList.Add(Nothing) Start_Thread_Action() End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click n_str_Msg = Me.TextBox1.Text & vbCrLf & n_str_Msg 'Msg_String = Me.TextBox1.Text TextBox3.Text = n_str_Msg Me.TextBox1.Text = "" End Sub End Class
'=============================================== ' Class Code '=============================================== Imports System.Threading
Public Class clsThread Private m_ThreadIndex As Integer Private m_Counter As Integer = 0 Private m_str_Msg As String 'Public n_str_Msg As String Private m_Args(1) As Object Private m_MainWindow As Form
Private Delegate Sub NotifyMainWindow(ByVal ThreadIndex As Integer, ByVal str_Msg As String) Private m_NotifyMainWindow As NotifyMainWindow
Public Sub New(ByVal ThreadIndex As Integer, ByRef MainWindow As frmMain) m_ThreadIndex = ThreadIndex m_MainWindow = MainWindow m_NotifyMainWindow = AddressOf MainWindow.ReceiveThreadMessage End Sub
Public Sub StartThread() While True 'm_Counter = m_Counter + 1 'Debug.Print(frmMain.n_str_Msg) If Len(frmMain.n_str_Msg) = 0 Then m_str_Msg = "No Message" Else m_str_Msg = frmMain.n_str_Msg & vbCrLf & m_str_Msg End If
ReDim m_Args(1)
m_Args(0) = m_ThreadIndex 'm_Args(1) = m_Counter m_Args(1) = m_str_Msg m_MainWindow.Invoke(m_NotifyMainWindow, m_Args) Thread.Sleep(5000) End While End Sub End Class
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Does this line print the value of the variable properly?
Debug.Print(frmMain.n_str_Msg)
If not try using
MainWindow.n_str_Msg
Because in this class we have already passed the ref. of the main from while construing the object and we have hold the ref in MainWindow variable.
let me know
Rakesh
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
 |
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
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
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 !!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
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
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
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.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
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
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
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?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
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
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi Rakesh, Thank u so much .I think the reply which u gave will help me and i will implement it and see
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
 |
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.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
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[^]
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
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
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
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
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
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
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi Bondan,
I really did not understand what are you trying to accomplish. Better if you can write in a bit details..
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
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
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
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
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |