Yes I follow everything said and Weltchys point about thread static.
But I still go back to what the documentation says:-
"If the procedure is not Shared, its local variables are instance variables, including the Static variables."
So, what my interpretation of this was is that it simply produced a member variable which was hidden from outside the method. It would be otherwise treated the same as any other instance variable. This is precisely what I wanted.
Clearly the documentation simply lies.
- Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
- Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!
Reply to: .Net Statics
Collapse
You are not logged in or you do not have permission to access this page. This could be due to one of several reasons:
- You are not logged in. If you are already registered, fill in the form below to log in, or follow the "Sign Up" link to register a new account.
- You may not have sufficient privileges to access this page. Are you trying to edit someone else's post, access administrative features or some other privileged system?
- If you are trying to post, the administrator may have disabled your account, or it may be awaiting activation.
Logging in...
Previously on ".Net Statics"
Collapse
-
Originally posted by DimPrawnBecause you don't understand threading and the nature of Static data. Like most VB6 to VB.NET people.
Think outside the box...
"In which case as Weltchy suggested you need to store the state within the currently executing thread. In Java (sorry not a .NET developer) you have a ThreadLocal object (assuming it's the same idea as a ThreadStatic object) where you can store information that is specific to the current thread."
See above.
Leave a comment:
-
Originally posted by Cowboy BobIn which case as Weltchy suggested you need to store the state within the currently executing thread. In Java (sorry not a .NET developer) you have a ThreadLocal object (assuming it's the same idea as a ThreadStatic object) where you can store information that is specific to the current thread. If the thread re-enters the method it will be able to pick up it's own stored state, and any other thread would have it's own individual state.
Leave a comment:
-
Originally posted by ASBFair comment, but there are of course differing views.
In this particular case the method has to retain state. Now I can retain it as a member in the class for example. However the issue with that is scope.
Since it is only valid within the context of the method then I would prefer to reduce visibility to that method. If it's a member variable it's all too easy for somebody to come along and misuse it at a later date. Hence the thought of the static. Something I know you cannot do in C# (or couldn't).
Leave a comment:
-
Originally posted by WeltchyLook at the ThreadStatic attribute as well, just in case you really want to hear that nice ka-plonking sound
I'm not using the attribute, or is VB being "helpful" and marking it for me. ??
Leave a comment:
-
Look at the ThreadStatic attribute as well, just in case you really want to hear that nice ka-plonking sound
Leave a comment:
-
Yes but...
That is referring to static members within shared (static) methods. Not instance methods. Here I have an instance method.
The MS documentation specifically describes them as instance variables in this case:
"If the procedure is not Shared, its local variables are instance variables, including the Static variables."
So it *should* have been alright. If I can get round to it then I'll reproduce it and look at the IL (or more likely just avoid them like the plague).
So my threading problem (and yes the exception is raised by Monitor.Enter) was not because of any lack of understanding on my part. Just by MS lieing in the docs.
As for C# I have a plan. But it doesn't seems to be getting any closer.
Leave a comment:
-
Taking the piss is much more fun than helping people.
Anyway. Static in VB.NET is some syntactic sugar to generate a hidden Shared (static in C#) class level variable in the IL to simulate a local static variable. Sounds like a typical VB.NET kludge to me and trap for the unwary.
This Blog entry describes how the static local variable is intitialised and the impact of multiple threads.
http://weblogs.asp.net/psteele/pages/7717.aspx
Now since you have:
Protected Overrides Function DeviceHasBeenEnabled() As Boolean
Static blnFirstTime As Boolean = False
You are trying to combine an Instance method with a static field and I am guessing here (I do C#) that the behind the scenes kludge is designed to work with:
Shared Function DeviceHasBeenEnabled() As Boolean
Static blnFirstTime As Boolean = False
Lesson: Learn C#. Understand IL. Understand threading and initialisation.
HTH
Leave a comment:
-
Originally posted by Cowboy BobWhy the hell would you want to? Statics are for constants (which should be declared final) or (though personally I think it's bad practice) for use as member variables in static classes (ie singletons). That is it. Anything else is a threading disaster waiting to happen...
In this particular case the method has to retain state. Now I can retain it as a member in the class for example. However the issue with that is scope.
Since it is only valid within the context of the method then I would prefer to reduce visibility to that method. If it's a member variable it's all too easy for somebody to come along and misuse it at a later date. Hence the thought of the static. Something I know you cannot do in C# (or couldn't).
What I was curious of was what is the specific threading issue that causes the allocation to fail, because clearly there is something I misunderstood. Perhaps DP will enlighten me rather than just taking the piss.
Leave a comment:
-
Originally posted by DimPrawnBecause you don't understand threading and the nature of Static data. Like most VB6 to VB.NET people.
Would you care to enlighten me O great one.
Leave a comment:
-
Originally posted by ASBObviously I can do it a different way with no problems but why the hell can't I allocate a static in this circumstance.
Leave a comment:
-
Because you don't understand threading and the nature of Static data. Like most VB6 to VB.NET people.
Leave a comment:
-
.Net Statics
Protected Overrides Function DeviceHasBeenEnabled() As Boolean
Static blnFirstTime As Boolean = False
The above code throws a null reference exception.
I am guessing it's because the ultimate caller is in a SyncLock ??
Obviously I can do it a different way with no problems but why the hell can't I allocate a static in this circumstance.Tags: None
- Home
- News & Features
- First Timers
- IR35 / S660 / BN66
- Employee Benefit Trusts
- Agency Workers Regulations
- MSC Legislation
- Limited Companies
- Dividends
- Umbrella Company
- VAT / Flat Rate VAT
- Job News & Guides
- Money News & Guides
- Guide to Contracts
- Successful Contracting
- Contracting Overseas
- Contractor Calculators
- MVL
- Contractor Expenses
Advertisers
Contractor Services
CUK News
- Streamline Your Retirement with iSIPP: A Solution for Contractor Pensions Sep 1 09:13
- Making the most of pension lump sums: overview for contractors Sep 1 08:36
- Umbrella company tribunal cases are opening up; are your wages subject to unlawful deductions, too? Aug 31 08:38
- Contractors, relabelling 'labour' as 'services' to appear 'fully contracted out' won't dupe IR35 inspectors Aug 31 08:30
- How often does HMRC check tax returns? Aug 30 08:27
- Work-life balance as an IT contractor: 5 top tips from a tech recruiter Aug 30 08:20
- Autumn Statement 2023 tipped to prioritise mental health, in a boost for UK workplaces Aug 29 08:33
- Final reminder for contractors to respond to the umbrella consultation (closing today) Aug 29 08:09
- Top 5 most in demand cyber security contract roles Aug 25 08:38
- Changes to the right to request flexible working are incoming, but how will contractors be affected? Aug 24 08:25
Leave a comment: