473,881 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(AddressO f LaunchCommThrea d)
NewThread.Alloc ateNamedDataSlo t("Offset")
NewThread.IsBac kground = True
NewThread.Name = SIMclass.SIM(1) .strCtrlDesigna tor
NewThread.SetDa ta(NewThread.Ge tNamedDataSlot( "Offset"), CType(1,
Object))
NewThread.Start ()
The beginning of the LaunchCommThrea d function:

'get thread data and controller # to use
Dim Ctrlr As Int16 'used to ID current (from data
slot)
Ctrlr =
Thread.CurrentT hread.GetData(T hread.CurrentTh read.GetNamedDa taSlot("Offset" ))
'declare per-thread variables for TCP communication
Dim address As IPAddress =
IPAddress.Parse (SIMclass.SIM(C trlr).strCtrlIP )

The results from the main thread:
?newThread.GetD ata(newThread.G etNamedDataSlot ("Offset"))
Nothing
?newthread.Name
"M1C1"

The results from the child:
?thread.Current Thread.Name
"M1C1"
?thread.Current Thread.GetData( thread.CurrentT hread.GetNamedD ataSlot("Offset "))
Nothing

Strangely, this syntax works from the main thread (does anyone know why?):
?newThread.GetD ata(Thread.Curr entThread.GetNa medDataSlot("Of fset"))
1 {Integer}
[Integer]: 1 {Integer}

Q:
1) Am I misunderstandin g 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 2530
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(AddressO f LaunchCommThrea d)
NewThread.Alloc ateNamedDataSlo t("Offset")
NewThread.IsBac kground = True
NewThread.Name = SIMclass.SIM(1) .strCtrlDesigna tor
NewThread.SetDa ta(NewThread.Ge tNamedDataSlot( "Offset"), CType(1,
Object))
NewThread.Start ()
The beginning of the LaunchCommThrea d function:

'get thread data and controller # to use
Dim Ctrlr As Int16 'used to ID current (from data
slot)
Ctrlr =
Thread.CurrentT hread.GetData(T hread.CurrentTh read.GetNamedDa taSlot("Offset" ))
'declare per-thread variables for TCP communication
Dim address As IPAddress =
IPAddress.Parse (SIMclass.SIM(C trlr).strCtrlIP )

The results from the main thread:
?newThread.GetD ata(newThread.G etNamedDataSlot ("Offset"))
Nothing
?newthread.Name
"M1C1"

The results from the child:
?thread.Current Thread.Name
"M1C1"
?thread.Current Thread.GetData( thread.CurrentT hread.GetNamedD ataSlot("Offset "))
Nothing

Strangely, this syntax works from the main thread (does anyone know why?):
?newThread.GetD ata(Thread.Curr entThread.GetNa medDataSlot("Of fset"))
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.Whatever NamedDataSlot

The thread they apply to is always the *current* thread - as the docs
for AllocateNamedDa taSlot 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.Alloc ateNamedDataSlo t("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 misunderstandin g 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(C trlr).strCtrlIP )

' etc
End Sub
End Class

Then to launch, we just

Dim cl As New CommLauncher
cl.Ctrlr = 1
Dim t As New Thread(AddressO f cl.LaunchComm)
t.IsBackground = True
t.Name = SIMclass.SIM(1) .strCtrlDesigna tor
t.Start()
For more about threading, I recommend Jon Skeet's C# threading pages at
<http://www.yoda.arachs ys.com/csharp/threads/or Joseph Albahari's C#
threading pages at <http://www.albahari.co m/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*******@googl email.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
9605
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, while an instance of class C should be able to check all methods #defined in C, B and A. #------------------------------------------------
16
25431
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 the properties: mode1, mode2, and mode3. This seems simple but I can't quite figure it out... Any ideas anyone?
2
11795
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 named SplashScreen, and a C# class library named BackgroundProcess.
3
2950
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 with the AddressOf a class that runs a long data intensive process that can take between 1-15 minutes and results in the creation of a uniquely named file. Meanwhile, I want my ASP.Net application to sit and wait until the file appears at a...
6
2718
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 store thread-specific data." what is "thread-specific data"? Best regards wmt email:iam1708@hotmail.com
2
1527
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 are getting sporadic problems where the data that was just set on the thread cannot be retrieved (it's null). It's almost like the IIS thread that we attach the data to at the beginning of the call is lost and a new thread created before the...
6
9770
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 sure. First, my assumption is that one thread is attempting to get a single key, while other threads are getting, setting, and deleting different keys. Obviously you could fail anyway if they were modifying the same key. The avenue of attack...
1
1393
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 shown below : <Card> <IPAddress>192.168.0.22</IPAddress> <Slot>2</Slot> <Card_Type><!]></Card_Type> <User_Label><!]></User_Label> <Card_Description>HD-SDI Up-Converter</Card_Description> <SW-Version>1211</SW-Version> ...
29
9171
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: --Thread 1-- 1. Reads TCPIP Buffer 2. Adds Buffer to Queue (q.size() to check queue isn't full, and q.push_back(...)) 3. Signals Reading Thread Event & Goes Back to Wait for More Messages on TCPIP
0
9928
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9776
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10718
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10816
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9555
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5781
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4597
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4196
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3225
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.