|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Thread not getting value in named slot2003), and I can't get the child to receive the parameter I'm attempting to send it in a named data slot. The code for launching the thread: Dim NewThread As New Thread(AddressOf LaunchCommThread) NewThread.AllocateNamedDataSlot("Offset") NewThread.IsBackground = True NewThread.Name = SIMclass.SIM(1).strCtrlDesignator NewThread.SetData(NewThread.GetNamedDataSlot("Offset"), CType(1, Object)) NewThread.Start() The beginning of the LaunchCommThread function: 'get thread data and controller # to use Dim Ctrlr As Int16 'used to ID current (from data slot) Ctrlr = Thread.CurrentThread.GetData(Thread.CurrentThread.GetNamedDataSlot("Offset")) 'declare per-thread variables for TCP communication Dim address As IPAddress = IPAddress.Parse(SIMclass.SIM(Ctrlr).strCtrlIP) The results from the main thread: ?newThread.GetData(newThread.GetNamedDataSlot("Offset")) Nothing ?newthread.Name "M1C1" The results from the child: ?thread.CurrentThread.Name "M1C1" ?thread.CurrentThread.GetData(thread.CurrentThread.GetNamedDataSlot("Offset")) Nothing Strangely, this syntax works from the main thread (does anyone know why?): ?newThread.GetData(Thread.CurrentThread.GetNamedDataSlot("Offset")) 1 {Integer} [Integer]: 1 {Integer} Q: 1) Am I misunderstanding the purpose of named data slots? 2) Am I misusing named data slots by using one to pass a parameter? 3) Why does this code not work? I am able to place the value into the current thread's slot but that isn't where I am directing it? TIA Fred B Fred B wrote:
Show quoteHide quote > I am launching a new thread from my application's main process (using VB.net Unfortunately you are being misled by one of the loosenesses of VB > 2003), and I can't get the child to receive the parameter I'm attempting to > send it in a named data slot. > > The code for launching the thread: > > Dim NewThread As New Thread(AddressOf LaunchCommThread) > NewThread.AllocateNamedDataSlot("Offset") > NewThread.IsBackground = True > NewThread.Name = SIMclass.SIM(1).strCtrlDesignator > NewThread.SetData(NewThread.GetNamedDataSlot("Offset"), CType(1, > Object)) > NewThread.Start() > > > The beginning of the LaunchCommThread function: > > 'get thread data and controller # to use > Dim Ctrlr As Int16 'used to ID current (from data > slot) > Ctrlr = > Thread.CurrentThread.GetData(Thread.CurrentThread.GetNamedDataSlot("Offset")) > > > 'declare per-thread variables for TCP communication > Dim address As IPAddress = > IPAddress.Parse(SIMclass.SIM(Ctrlr).strCtrlIP) > > > > The results from the main thread: > ?newThread.GetData(newThread.GetNamedDataSlot("Offset")) > Nothing > ?newthread.Name > "M1C1" > > The results from the child: > ?thread.CurrentThread.Name > "M1C1" > ?thread.CurrentThread.GetData(thread.CurrentThread.GetNamedDataSlot("Offset")) > Nothing > > Strangely, this syntax works from the main thread (does anyone know why?): > ?newThread.GetData(Thread.CurrentThread.GetNamedDataSlot("Offset")) > 1 {Integer} > [Integer]: 1 {Integer} syntax. The various *NamedDataSlot functions are actually *Shared* functions in Thread, and should be invoked with this syntax: Thread.WhateverNamedDataSlot The thread they apply to is always the *current* thread - as the docs for AllocateNamedDataSlot say, "No other thread (not even a child thread) can get that data." The fact that VB allows access to Shared members through an instance variable is (IMO) a mistake, and in VB2005 such code produces a warning. So when you say NewThread.AllocateNamedDataSlot("Offset") you are actually allocating a named data slot on the *current* thread. Bearing this in mind, everything that subsequently happens makes sense. > Q: A bit.> 1) Am I misunderstanding the purpose of named data slots? > 2) Am I misusing named data slots by using one to pass a parameter? Yes.> 3) Why does this code not work? I am able to place the value into the The easiest way to pass information to new threads is to create a helper > current thread's slot but that isn't where I am directing it? class: Public Class CommLauncher Public Ctrlr As Int16 'wrap in a property in a real app, posible readonly Public Sub LaunchComm() 'declare per-thread variables for TCP communication Dim address As IPAddress = _ IPAddress.Parse(SIMclass.SIM(Ctrlr).strCtrlIP) ' etc End Sub End Class Then to launch, we just Dim cl As New CommLauncher cl.Ctrlr = 1 Dim t As New Thread(AddressOf cl.LaunchComm) t.IsBackground = True t.Name = SIMclass.SIM(1).strCtrlDesignator t.Start() For more about threading, I recommend Jon Skeet's C# threading pages at <http://www.yoda.arachsys.com/csharp/threads/> or Joseph Albahari's C# threading pages at <http://www.albahari.com/threading/>. Most threading info tends to be in C#... One final note: I see that both the main thread and the worker thread are accessing SIMclass - if you aren't already, make sure that access to thread-shared resources is correctly synchronized. -- Larry Lard larryl***@googlemail.com The address is real, but unread - please reply to the group For VB and C# questions - tell us which version |
|||||||||||||||||||||||