|
Actually, it's recommended you never use lock(this) [that is, locking the class instance] or lock(typeof(TestConsole)) [that is, locking the class type itself). It is highly recommended that if you do use locks, then you should lock private variables in your class, rather than class instance or the class type. The reason is that by using private variables to do locking, you have knowledge of and can control everyone that holds locks on your object. This would not necessarily be true for locking a class instance or the class itself.
Also, as an FYI, the C# lock statement is just syntax candy:
lock(_lockObj)
{
Console.WriteLine("hello!");
}
will get compiled down to:
System.Threading.Monitor.Enter(_lockObj);
try
{
Console.WriteLine("hello");
}
finally
{
System.Threading.Monitor.Exit(_lockObj);
}
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Morality Apart from God
Judah Himango
-- modified at 11:20 Wednesday 9th November, 2005
|
|
|
|
|
I agree with Judah, locking 'this' or the class itself is very bad practice, and leads to a lot of problems. Standard practice is to use a semaphore, or a synchronization root object, as a lock target. Some simple examples of good locking can be represented by a synchronized collection, or the singleton object:
A singleton:
public class MySingleton
{
#region Singleton Pattern
private static volatile MySingleton m_instance;
private static object m_syncRoot = new object();
public static MySingleton
{
get
{
if (m_instance == null)
{
lock (m_syncRoot)
{
if (m_instance == null)
{
m_instance = new MySingleton();
}
}
}
return m_instance;
}
}
#endregion
}
Synchronizing a collection:
public class MyCollection: CollectionBase
{
private class MySyncCollection: MyCollection
{
internal MySyncCollection(MyCollection col)
{
m_col = col;
m_root = col.SyncRoot;
}
private MyCollection m_col;
private object m_root;
public override object SyncRoot
{
get
{
return m_root;
}
}
public int Add(object item)
{
int index;
lock (m_root)
{
index = m_col.Add(item);
}
return index;
}
}
private object m_syncRoot = new object();
public override object SyncRoot
{
get
{
m_syncRoot;
}
}
public int Add(object item)
{
return List.Add(item);
}
public static MyCollection Synchronized(MyCollection col)
{
if (col == null)
throw new ArgumentNullException("The specified collection is null. Can not synchronize a non-existent collection.");
return new MyCollection.MySyncCollection(col);
}
}
|
|
|
|
|
Thank you for your replies friends,
While I am completely agree with your notes, I should say the concept I talked in my post was not a question about using or not using 'this' as the lock object.
That was something interesting that I didn't know up to know and that is grouped locks (with a single lock object) togather that construct composite single critical sections, and creating INDEPENDENT critical sections by assigning different lock objects.
I am excited about this because I didn't knew this before and non of my friends, thought so about lock usage. I posted here to see if anyone else see this interesting and usefull.
---
"Art happens when you least expect it."
|
|
|
|
|
how to download a file from a server using FTP through ASP.NET/C#
|
|
|
|
|
|
Hi,
I have found a C# control that I would like to use my in my commercial application. The control is distributed as a single C# source file which I have included in my project, i.e., my application will not link to an assembly or dll. It is licensed under LGPL or CPL (Common Public License), whichever suits the user (me) best. I would simply like to know if it is ok to do so and what I have to do to comply with the license I choose (e.g. include license terms)?
If there is somebody out there who can give me a quick answer, you can save me a few days trying to understand what those license files you never care to read actually means.
/Johan
|
|
|
|
|
hi . i want to manipulate exe files that when click on exe file before running ,it check some condition
and if it is true the file is run otherwise , the exe file close
thanks.
hdv212@yahoo.com
|
|
|
|
|
Study the PE-file format. www.wotsit.org has some documentation.
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Hello
I'm trying to develop an ActiveX container in C#.
The first thing I would need to do is to create an ActiveX control programatically (CoCreate) and to save it via it's property pages. How can I obtain a pointer to control's IUnknown interface?
Maria Luisa
|
|
|
|
|
Could someone give me an algorithm of a recursive function to return the max key of nodes of a singly connected list?
|
|
|
|
|
Could someone give me an algorithm of a recursive function to display-NOT TO RETURN-the number of nodes of a singly connected list?
|
|
|
|
|
I am probably misunderstanding your question, but any implementation of a List (ArrayList, LinkedList etc) should expose a Count property, and so your question of how to display this is down to where you want to display it.
on the console:
Console.WriteLine(myList.Count);
in a messageBox
MessageBox.Show(miList.Count.ToString());
sorry if ive misunderstood
|
|
|
|
|
This is not a C# question but an algorithm question for my homework at university.
|
|
|
|
|
Perhaps you should do you own homework? You learn a lot more that way. This isn't a difficult question.
|
|
|
|
|
It is absolutely sure that I can not do that, although I have tried very much. Any idea?
|
|
|
|
|
What have you tried? Show people that you are thinking about this and not just too lazy to do your homework and you'll find people are much more responsive.
You need to write a recursive function, something like this (you fill in the blanks):
<code>
public int CountNodes(Node n)
{
if(ExitCondition)
{
return ?;
case?
}
else
{
return ? + CountNodes(?);
}
}
</code>
|
|
|
|
|
while I Agree with wjousts assertion that a) this is not a particularly difficult question and b) you should do your own homework - I will provide some assistance.
You must know what a linked list is, if not go back and refer to your course notes. Recursion you probably also know about - essentially calling a function from itself (or put another way "to understand recursion, you must first understand recursion /geekhumour)
So, to help write this algorithm, I find it easy to start off in pseudo-code
while {not at end of list)
Incrememnt counter
loop
return counter
Now, I know you super-highlight-stressed that you want it DISPLAYED and not RETURNED!!11 (your homework said display it right?) however in order to dispaly something...it must first be returned from somewhere.
Turning the above into a "recursive function" is the bit you've got to do. If this is not enough to get you going on your homework, then seriously consider whether you should be studying a subject you cannot do!
Good luck.
|
|
|
|
|
My English is not good.
But I mean something like that:
int count(POINTER head)<br />
{<br />
return head!=NULL ? count(head->next)+1 : 0 ;<br />
}
But not to return the value. I WANT TO DISPLAY THE VALUE.(and this is difficult)
|
|
|
|
|
Where do you want to display it? In a window? on a command line?
|
|
|
|
|
|
Console.WriteLine(myList.Count(myList.FirstNode))
seriously, you couldnt work this out? Swap to a different course while you still can.
|
|
|
|
|
It is a Data Structure work J4amieC. Anyway thank you.
|
|
|
|
|
Hi folks,
I have project writing rich text document on metafile using GDI
Now I'm trying to make it using GDI+
Problem: I can't draw it precisely!!
Test sample: create metafile 10x10 inces, draw strips 10, 9, 8 inches long:
<br />
RectangleF frame = new RectangleF(0f, 0f, 10f, 10f);<br />
Metafile mf;<br />
Graphics g1;<br />
<br />
using(Graphics gr = CreateGraphics())<br />
{<br />
IntPtr dc = gr.GetHdc();<br />
mf = new Metafile(@"c:\inch.emf", dc, frame, MetafileFrameUnit.Inch, EmfType.EmfOnly);<br />
gr.ReleaseHdc(dc);<br />
}<br />
g1 = Graphics.FromImage(mf);<br />
g1.PageUnit = GraphicsUnit.Inch;<br />
g1.DrawLine(Pens.Green, 0F, 1F, 10F, 1F);<br />
<br />
g1.DrawLine(Pens.Green, 0F, 3F, 9F, 3F);<br />
g1.DrawLine(Pens.Green, 0F, 5F, 8F, 5F);<br />
g1.DrawLine(Pens.GreenYellow, 1f, 0f, 1f, 10f);<br />
g1.DrawLine(Pens.GreenYellow, 3f, 0f, 3f, 9f);<br />
g1.DrawLine(Pens.GreenYellow, 5f, 0f, 5f, 8f); <br />
<br />
g1.Dispose();<br />
mf.Dispose();<br />
Strips are shorter than needed!!
Using other units (Milimeter, Pixel, Point, Document) produces different results - all invalid
Question: how to create precise EMF - with definite size
WBR Tim
|
|
|
|
|
Hi folks,
I have a data grid.And I want to drag a cell's context into datagrid's caption text by dragging it. How can I do this...
Please help
-- modified at 9:29 Wednesday 9th November, 2005
|
|
|
|
|
Hello, I am presented with the following problem and have tried desperately (over 12 hours) to solve it:
I have a string say "011010101" and I want to parse this string based on a table of values:
i = 0
m = 011
k = 10
e = 101
I want to construct an algorithm that will go through the string above and parse it assigning each value to their corresponding table value. Looking at it, I know that the string would become "011" , "0" , "10" , "101" which for this example would be "mike" in that order. Since "i" is the first thing I check, my code tries to form "0", "11010101", and nothing else will get extracted. I don't need code, but maybe some concepts I could build off of? Any help would be greatly appreciated.
I love to program!
|
|
|
|
|