473,385 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Thread not getting value in named slot

I am launching a new thread from my application's main process (using VB.net
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("Offs et"), 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("Off set"))
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.GetNamedDa taSlot("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
Aug 17 '06 #1
1 2493
Fred B wrote:
I am launching a new thread from my application's main process (using VB.net
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("Offs et"), 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("Off set"))
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.GetNamedDa taSlot("Offset"))
1 {Integer}
[Integer]: 1 {Integer}
Unfortunately you are being misled by one of the loosenesses of VB
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:
1) Am I misunderstanding the purpose of named data slots?
A bit.
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
current thread's slot but that isn't where I am directing it?

The easiest way to pass information to new threads is to create a helper
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
la*******@googlemail.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
Aug 17 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Fernando Rodriguez | last post by:
Hi, I need to traverse the methods defined in a class and its superclasses. This is the code I'm using: # An instance of class B should be able to check all the methods defined in B #and A,...
16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
2
by: BG | last post by:
We're having trouble writing the code to update a UI control (label.Text) from a secondary thread. We're using C# with Windows Forms. We have a main form named MainForm, a splash screen form...
3
by: Stephen Miller | last post by:
I have an ASP.Net application that sends a NetworkStream to a .Net Service, which has a TcpListener listening on a port for the ASP.Net client. When it receives a request it creates a new thread...
6
by: iam1708 via DotNetMonster.com | last post by:
I was looking at the docs for Thread and can't understand the different between "unnamed data slot "and "data slot".and docs in Thread.GetData() say "Threads use a local store memory mechanism to...
2
by: Mike W | last post by:
I am using thread.GetNamedDataSlot and thread.SetData to attach data to a thread inside of a class library which in turn is used by an asp.net 2.0 application. For the most part it works but we...
6
by: Adam Olsen | last post by:
It seems to be a commonly held belief that basic dict operations (get, set, del) are atomic. However, since I know searching the hash table is a multistep process, I thought I'd check it out for...
1
by: IanHollamby | last post by:
I am new to XML/XSL formatting so have a fundamental question/problem. I have two formats of XML data that I require to format, the first XML file (which is under my control) uses child elements as...
29
by: NvrBst | last post by:
I've read a bit online seeing that two writes are not safe, which I understand, but would 1 thread push()'ing and 1 thread pop()'ing be thread-safe? Basically my situation is the follows: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.