 |
|
 |
Do you work on tapi? if yes kindly tell me how to make a dialer in which u connect landline to ur modem,no telephone is used, just pc and headphones (vb.net programming)
|
|
|
|
 |
|
 |
Great Rakesh Mehta.
Thanks!
Sandro
|
|
|
|
 |
|
 |
1)m_NotifyMainWindow = AddressOf MainWindow.ReceiveThreadMessage
2) m_Args(0) = m_ThreadIndex
3) m_Args(1) = m_Counter
'Call the notificaiton method on the main window by the delegate
4) m_MainWindow.Invoke(m_NotifyMainWindow, m_Args)
5)Public Sub ReceiveThreadMessage(ByVal ThreadIndex As Integer, ByVal Counter As Integer)
the value sent in line number 4 is the address of m_NotifyMainWindow and the m_Args is a array of object, however the parameter receiving method in the main window/form is both Integer.
Select Case ThreadIndex
Case 1
Me.lbl_ThreadCounter1.Text = Counter.ToString
If Me.chk_Thread1.Checked Then
'If the appropriate check box is checked we will look into the stop counter
If Counter = Convert.ToInt32(Me.txt_ThreadStopCounter1.Text) Then
'if it matched with counter variable, passed from the tread, we will stop that thread.
CType(m_ThreadList(ThreadIndex - 1), Thread).Abort()
'Remove the object from the thread list
m_ThreadList(ThreadIndex - 1) = Nothing
End If
End If.
QUESTIONS:
1) Now when the values are passed(invoked) to the main window, m_NotifyMainWindow (address) and m_Args (array of object) both became integer (line number 5)?
m_Args(0) = m_ThreadIndex
m_Args(1) = m_Counter
2) please explain that the value of 'Counter' is the array index 1 (m_Counter)?
3) Please explain that the value m_NotifyMainWindow became integer too as Thread index where in fact it's a address based from line number 1?
please reply: archie_4_christ@yahoo.com.ph
|
|
|
|
 |
|
 |
butiful example after long search, thanks
|
|
|
|
 |
|
 |
It's work and very useful. thank you for your article.
|
|
|
|
 |
|
 |
Suffer from cut and paste syndrome
|
|
|
|
 |
|
 |
who doesn't
|
|
|
|
 |
|
 |
Verry nicely explained. good keet it up and thanks
|
|
|
|
 |
|
|
 |
|
 |
thanks... i was looking for sumthng just like this..helped me very much in understanding the threads as ur code was well commented.
|
|
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
 |
Hi there,
In simple word delegate is a pointer to a method. While using THREAD you can not simply call a function on a different thread (for example in a form) from within the thread. In this situation we need to create a delegate which will point to the function we need to call, and from within the THREAD you invoke the delegate and then it will call the function pointed by it. Thus you can avoid CROSS THREAD operation in your code. One thing here, remember that you can call a function of one thread from another, but that function call should not change any component within the method called from the thread. That means, if you are calling a function on a form from within the thread and if the function in the form tries to update a text in a textbox, or a label or any other control, then you will receive a CROSS THREAD operation error. At the same time if the function on the FORM does not do any visual element update, then
For example you have
1. A form (which is ultimately a class)
2. A class which contains a big function to do some continuous job
Now to run a thread, what you do is you create an object of the class and then you create a thread and you mention a function inside the class as a starting point of your thread.
Now if you need to call a function on the form from within the function inside the threaded class, you can not call it directly like form1.myFunction() as this may cause an invalid CROSS THREAD operation.
What you need to do is you need to create a delegate that will point to myFunction() on the form and then you need to call myFunction() using the delegate (function pointer)
to create a delegate you simply have to declare the delegate function with the same signature of the function you want to point.
For example.. if myFunction() looks like this
private sub myFunction(byval v1 as int32, byval str as string)
the the delegate delcaration should be
private delegate sub DelMyFunction(byval v1 as int32, byval str as string)
Note: The name of the delegate method could be anything.
now you need to create an object to the above delegate and point the object to the function on the main form
dim x as new DelMyFunction(addressof Form1.myFunction)
now x becomes the pointer to the function myFunction and you can use x to call the function.
Form1.invoke(x, arguments() as object)
our myFunction() takes two argument, but if you see the above line, then there is no way to pass multiple argument. There is only an array of Object. You have to put all your arguments in this Object array. Like
redim arg(1) as object
arg(0) = 5 (our first argument in function is an int32)
arg(1) = "myname" (second argument is a string)
and you pass the argment as
Form1.invoke(x, arg)
This will call your function myFunction on your form and will pass the arguments sequentially as you put it in the object array.
Hope this helps you to understand how to work with delegate
You can refer MSDN for more code snippest http://msdn.microsoft.com/en-us/library/system.delegate.aspx[^]
|
|
|
|
 |
|
|
 |
|
 |
Hi,
I am not sure If I understood the problem correctly. What I have understood that.. while you are creating object of the class (where you have kept the threaded method), each time in the FOR loop the customer id is getting changed.. That means.. all the threads are seeing the same CustomerID.
Is this your problem?
Let me know
Rakesh
|
|
|
|
 |
|
|
 |
|
 |
Hi,
I did not understand what you are trying to do exactly. Can you paste here a part of your code? so that I can understand what you are trying to do. Synclock is used to LOCK an object from updating by different code segment at the same time. for example if you are running multiple thread and each thread work on the same shared object and you want a thread to wait when another thread is working on that object, then in that case you need to use synclock. Let me know your problem exactly, I'll try my best
Cheers
Rakesh
|
|
|
|
 |
|
|
 |
|
 |
OOps.. thats huge code.. not readable Just post the logic. It's not possible for me to understand your code.. Just post the logic, what you are trying to do.
|
|
|
|
 |
|
 |
Thank you for this example. I was having a very hard time implementing threads in a class structure and your code gave me the breakthrough I needed. Keep it up!
|
|
|
|
 |
|
 |
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.
|
|
|
|
 |
|
 |
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
|
|
|
|
 |
|
 |
'===============================================
'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
|
|
|
|
 |
|
 |
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
|
|
|
|
 |