|
|
|
Thanks for all your response. Sorry, I didn't make myself clear.
Actually I want to change the value of status in the lock block. I need to lock it in case some one else are using it.
so it should be something like that:
class MyClass<br />
{<br />
<br />
private object status = null;<br />
private object status0 = new object(0);<br />
private object status1 = new object(1);<br />
private object status2 = new object(2);<br />
<br />
myfunc()<br />
{<br />
...<br />
lock ( status ) {<br />
status = status1;<br />
}<br />
...<br />
}<br />
<br />
}
It has problem as I mentioned.
Maybe I should change it to:
status = new object(0);<br />
lock ( status )<br />
{<br />
status = status1;<br />
}
Another quick question:
From the help in .Net, it suggests I can use either this or typeof(class). But I don't understand how to use "typeof(class)"; I tried "lock ( this )", it worked. But I wonder how about if I have different variables in one class to protect. If I use "lock (this)", it'll bring up some unnecessary blocks. Right? So what should I do in this case?
Thank you very much.
|
|
|
|
|
yyf wrote:
1: status = new object(0);
2: lock ( status )
3: {
4: status = status1;
5: }
I've added line numbers for clarification
This function will not work as you expect it to. For example assume I have two instances of MyClass m1 and m2. Each are created in a seperate thread. m1 executes lines 1-4 and then there is a thread switch. The problem is now the lock is on the previous object status pointed to and there is virtually no way to retrieve that object. Now m2 comes along and executes line 1 and 2. It is locking a completely new object so even though the thread containing m1 is currently inside the lock, the thread containing m2 will proceed into the code block. Now event though status1 will be set correctly in your case, the code is misleading and incorrect.
The easier way to do this is through a property and a locking object.
<br />
public class MyClass<br />
{<br />
private static object Sync = new object();<br />
public static object _status = null;<br />
<br />
public static object status<br />
{<br />
get <br />
{ <br />
lock ( Sync )<br />
{ <br />
if ( _status == null ) <br />
{ <br />
_status = new object();<br />
}<br />
}<br />
return _status;<br />
}<br />
}<br />
<br />
}<br />
Or you can just use a static constructor.
Jared
jparsons@jparsons.org
www.prism.gatech.edu/~gte477n
|
|
|
|
|
Thanks Jared, your answer are great.
However, I think it's not exactly what I want to do.
Look, in my case, the status is used as a signal in one instance. In your example, the status is used for all the instances, so you make it "static".
I usually read the code like that:
lock (this)<br />
{<br />
.....<br />
}
I think this is something similar to my case. Also if you read the help from C#.Net about the lock keyword, it said:
lock(expression) statement_block
expression
Specifies the object that you want to lock on. expression must be a reference type.
Typically, expression will either be this, if you want to protect an instance variable, or typeof(class), if you want to protect a static variable (or if the critical section occurs in a static method in the given class).
But I don't understand why I can not lock the variable which I want to protect. If I use this, how about two not related variables which I want to protect? When I lock "this" for "status", then another not related variable, let say "flag", it can not be changed either. It doesn't make sense.
Thanks again.
|
|
|
|
|
Hello!
I'm kind of new to the world of C# and have a little query...Is there some way to manipulate/store/use 256 bit int/float/long so that finally a calculator for those 256 bit values can be made? I know theres the decimal datatype for 128 bits so is there a way for 256 bits too? I have got a vague idea about using a link list and that many byte nodes but are there any other means?
Thanks beforehand for time and help...
|
|
|
|
|
|
Thankyou I have seen/have this ...
|
|
|
|
|
Use a BitVector or BitArray. There really need to be better class of this.
leppie::AllocCPArticle("Zee blog"); Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.
|
|
|
|
|
|
Thanks...but could you help me out some little more and tell me howwww exactly to divide that 256 input into those 2 128 bit decimals? I mean the problem facing me is this: I take a string input (readline) and then have to convert that to some numerical form to perform those mathe operations...but to do that I have to have some container, a datatype big enough to hold that converted 256 bit value first right?...so how do I know how and from where to break that converted string input into 2 decimals actually during conversion? I do hope I haven't muddled this up...so anything possible?
|
|
|
|
|
Hello there ...
Can anyone help me finding a REAL File / Directory TreeView with CheckBoxes like
it can be found in (for example) the System Backup Tool of Windows 2000 or
maybe like the one in R-Studio ??
I tried to make this with the integrated TreeView and the CheckBoxes but they can't
get grey if only some subitems are checked.
I am looking for a complete component which is not included in the .NET but
somewhere in Win32 )
Can anybody help me ?
JH
|
|
|
|
|
Hello there,
i've got a WindowsForms DataGrid containing
some DataGridBoolColumns.
Can anybody tell me how to get the CheckBox
control hosted by DataGridBoolColumn?
Thanks a lot.
brian
|
|
|
|
|
i have a small app that sits in the Icon tray. how can i set the app's starting position to open at the bottom right, just above the clock?
thanks for your help.
.gonad.
|
|
|
|
|
|
this article is very nice...
Thanks for your help.
.gonad.
|
|
|
|
|
Does any one know a good source for downloading Icons i can use to spice up my GUI.
Thanks
Trevor
|
|
|
|
|
MS Office
open your favourite office product, right click on the toolbar, select customize, in the Commands tab select All Commands, drag the command with the icon you want onto a toolbar, now right click the button on the toolbar and select Copy Button Image, now go paste it into your favourite image editor.
[disclaimer] of course you should not be using these icons for commercial applications
|
|
|
|
|
|
It is rather a simple problem (it looks like that).
Once I set the first item visible in a comboBox (comboBoxName.SelectedIndex = 0)
I notice a difference between the comboBox behavior in Windows 2000 and Windows XP.
In Windows 2000 the selected item is visible in the comboBox (it works fine) but in Windows XP the selected item is visible in the comboBox and it is also HIGHLIGHTED (MARKED).
How to make it just visible (like in Windows 2000) and NOT HIGHLIGHTED?
Thanks for help in advance.
misiek
|
|
|
|
|
Hello!
I program with Configuration management application block (CMAB)
I need to use configuration file that has more than two levels:
<Section>
<SubSection>
<SubSection>
<Element>
</SubSection>
</SubSection>
</Section>
How can I do it using CMAB? ConfigurationSectionHandler?
|
|
|
|
|
Hello!
I program with Configuration management application block (CMAB)
I need to use configuration file that has more than two levels:
<Section>
<SubSection>
<SubSection>
<Element>
</SubSection>
</SubSection>
</Section>
How can I do it using CMAB? ConfigurationSectionHandler?
|
|
|
|
|
Hi, I have created my own control that inherited TextBox.
I override the OnValidating event to check the text against database. The problem is that this happens everytime I leave the control, even if I click on a button which is not the desired behaviour. I tried to set "causes validation " property on the button , but this does not have effect. The only thing that make a change is when I set "Causes validation=false" on my control, but then this event doesn't fire at all.
Help please,
MitDej
|
|
|
|
|
hi,
i want to know any good books for .net xml, webservices? i want to get a better understanding on XSD, xml validation, xml serialization etc...
i heard that professional asp.net by Wrox is quite good. any comments?
anyone can help?
regards,
jim
|
|
|
|
|
hi,
i want to know any method to implement popup menu in C#?
any help for starting?
thanks,
jim
|
|
|
|