MultiThreading

LankanDude

New member
Joined
May 23, 2006
Messages
2
Programming Experience
1-3
Please tell me how to do the below.
I need to pass a value from a sub to another sub.
But the second sub has to be called by the first sub using a thread.

Given below are the approaches that I took.
But none of them worked.

1)

Public Class MyClass
Public Sub MyThreadFunc( ByVal RequestXmlStr As String)
Dim BankReportThread As New Thread(AddressOf
MyFunc(RequestXmlStr ))
BankReportThread.Start()
End Sub

Public Sub MyFunc( ByVal RequestXml As String)
Dim X as String
X = RequestXml
End Sub

End Class


2)

Public Class MyClass
Public Sub MyThreadFunc( ByVal RequestXmlStr As String)
Dim BankReportThread As New Thread(AddressOf
MyFunc)
BankReportThread.AllocateNamedDataSlot("RequestXml")
BankReportThread.SetData(BankReportThread.GetNamedDataSlot("RequestXml"), RequestXmlStr)
BankReportThread.Start()
End Sub

Public Sub MyFunc( ByVal RequestXml As String)
Dim X as String
Dim returnValue As LocalDataStoreSlot
returnValue = Thread.GetNamedDataSlot("RequestXml")
X = CStr(Thread.GetData(returnValue))
End Sub

End Class
 
Named data slots are thread specific, so setting one in one thread does not make it accessible in another. .NET 2.0 has added the ParameterizedThreadStart delegate to make passing arguments to threads easier but in earlier versions the usual way is to define a class that has a property or field for each data value you want to pass to the new thread and a method for actually starting the thread.
VB.NET:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim ts As New ThreadStarter("Hello World")
        Dim t As New Threading.Thread(AddressOf ts.EntryPoint)

        t.Start()
    End Sub

End Class

Public Class ThreadStarter

    Private data As String

    Public Sub New(ByVal data As String)
        Me.data = data
    End Sub

    Public Sub EntryPoint()
        Debug.WriteLine(Me.data)
    End Sub

End Class
 
Thanks for the reply :D

1)
But what I'm trying to do is to create a named data slot in BankReportThread and when it's actually running trying to access the slot using the current thread (which is BankReportThread)
Is there any way to do this?


2)
Since both my methods are in the same class, how do u suggest to modify the answer given by you?
Could u plz explain it a bit more.
Do I simply need to create a global variable in the class and assign the value to it and retrive it?

3) If I use a global variable will there be any synchronization issues? will there be any dirty reads?

Once again thanks for the reply :)
 
Here's a quote from the help topic for the AllocateNamedDataSlot method:
Threads use a local store memory mechanism to store thread-specific data. The common language runtime allocates a multi-slot data store array to each process when it is created. The thread can allocate a data slot in the data store, store and retrieve a data value in the slot, and free the slot for reuse after the thread expires. Data slots are unique per thread. No other thread (not even a child thread) can get that data.
Slots allocated with this method must be freed with FreeNamedDataSlot.
but you read that already, right? As that says, named data slots are not accessible to any thread except the one that allocated it.

You can simply set a class-level variable in one thread and read it in another. It's really up to you how you synchronise access to that variable, depending on how it is used. You might declare a property to access it and use SyncLock blocks in the getter and setter.

If you want to call a method in the same class as that in which the worker thread was created then you can do so from the EntryPoint method in my code. You'd have to pass a reference to the class containing the method, or a reference to the method itself, to enable the ThreadStarter object to call the desired method:
VB.NET:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim ts As New ThreadStarter("Hello World", Me)
        Dim t As New Threading.Thread(AddressOf ts.EntryPoint)

        t.Start()
    End Sub

    Public Sub SomeSub(ByVal message As String)
        MessageBox.Show(message)
    End Sub

End Class

Public Class ThreadStarter

    Private data As String
    Private caller As Form1

    Public Sub New(ByVal data As String, ByVal caller As Form1)
        Me.data = data
        Me.caller = caller
    End Sub

    Public Sub EntryPoint()
        Me.caller.SomeSub(data)
    End Sub

End Class
Actually, it occurs to me that if you intend to use this ThreadStarter class only within the Form1 class then it should be nested within the Form1 class and declared Private.
 
Back
Top