473,385 Members | 1,518 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.

Passing references of ThreadStatic variables

Hi,

I am using .NET 2.0.

Suppose I mark a static variable with the ThreadStatic attribute in a class.
From some method in the class, if I pass that variable to an object of
another class as an argument in a method, would it be maintained as a
reference in the other object ? Subsequently, if the second object spawns a
thread which also accesses this variable, would the two threads be working on
the same instance of the variable ? Or does the second thread get a copy of
the thread static variable ?
Feb 28 '06 #1
1 4467
Thank you. That answers my question.

"Vadym Stetsyak" wrote:
Hello, Hemanth!

If you pass that veriable as method parameter then second object will use the value of static var initialized in the first object.

Also if second object will spawn new thread and access the variable directly SomeType.var then it will have another instance. OTOH if you store the reference passed as method parameter, then both thread will share the same variable.

Here is the sample

public class SomeType
{
[ThreadStatic]
public static SomeAnotherType var;

}

public class SomeType2
{
SomeAnotherType ineternalVar;;
void Method(SomeAnotherType var)
{
internalVar = var;

Thread thread = new Thread(new ParametrizedThreadStart(ThreadMethod));
}

void ThreadMethod(object obj)
{
//this.internalVar here will be the same for both threads
//however
bool isEqual = this.internalVar == SomeType.var; //will return false as we access static variable directly...

}
}

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot

Mar 1 '06 #2

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

Similar topics

0
by: lawrence | last post by:
Those of you with backgrounds with the C language will laugh at my mistake, but those of you, like myself, who deal mostly with PHP should be warned about passing variables as references -...
4
by: Amr Mostafa | last post by:
Hello :) I'm trying to write a script that deals with a web service. I'm using NuSoap class. my question is : Can I pass some variables By Reference to the web service and get the result back...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
6
by: Scott Zabolotzky | last post by:
I'm trying to pass a custom object back and forth between forms. This custom object is pulled into the app using an external reference to an assembly DLL that was given to me by a co-worker. A...
6
by: ged | last post by:
Hi, i am a oo (c#) programmer, and have not used javascript for a while and i cant work out how javascript manages its references. Object References work for simple stuff, but once i have an...
61
by: academic | last post by:
When I declare a reference variable I initialize it to Nothing. Now I'm wondering if that best for String variables - is "" better? With Nothing I assume no memory is set aside nor GC'ed But...
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
2
by: alexey_r | last post by:
I need to set the value of a ThreadStatic field before starting the thread. According to MSDN, setting the value normally won't work, because this is done only once. Neither will a static...
2
by: CJ | last post by:
I came across the ThreadStatic attribute today, but I'm a bit confused by it. I understand that it makes a variable static to a particular thread, so each thread will have it's own value for it...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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...

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.