Click here to Skip to main content
15,889,345 members
Home / Discussions / C#
   

C#

 
GeneralRe: showing a panel on a dialog Pin
Kurt _B9-Nov-05 12:10
Kurt _B9-Nov-05 12:10 
GeneralRe: showing a panel on a dialog Pin
NikoTanghe9-Nov-05 21:11
NikoTanghe9-Nov-05 21:11 
QuestionIterating thru the Outlook mail folders - C# Pin
vjvivaj9-Nov-05 3:21
vjvivaj9-Nov-05 3:21 
AnswerRe: Iterating thru the Outlook mail folders - C# Pin
Judah Gabriel Himango9-Nov-05 5:24
sponsorJudah Gabriel Himango9-Nov-05 5:24 
GeneralRe: Iterating thru the Outlook mail folders - C# Pin
vjvivaj9-Nov-05 18:15
vjvivaj9-Nov-05 18:15 
Questionhelp with Deserialize a string problem in c# Pin
drey19-Nov-05 3:20
drey19-Nov-05 3:20 
AnswerRe: help with Deserialize a string problem in c# Pin
Jon Rista9-Nov-05 10:39
Jon Rista9-Nov-05 10:39 
NewsC# lock statement Pin
Den2Fly9-Nov-05 3:00
Den2Fly9-Nov-05 3:00 
Hi buddies,
I have discovered something so interesting to myself! Wana share this top secret discovery! here:

We all know that 'lock(lockObject) {...}' causes no more than one thread can enter the block of code (Critical section). I always thought about the argument 'lockObject' that was usually 'this' and its usage while it seems the lock functionality doesn't care about what that argument is.
Recently I concluded the argument is so so important and it has a major usage and that is it lets us create critical sections that are not just one single code block, and different code blocks in different methods even in different classes may construct ONE critical section that no more than one thread may exist in the whole section.
I mean we can group various code blocks into unique critical section by assigning a single object reference as their lock(lockObject) argument. And also one great result is that we can create different critical sections in one class by assigning different lock arguments instead of 'this'; and these sections are independent.

So all you already knew this?! ok but atleast please agree that it is not well documented in MSDN or perhaps it is, but I have never seen it explicitly in the sections discussing lock statement.

Thanks for your note,
- Mohammad

My test console application code:

using System;
using System.Threading;

namespace TestConsole
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
private static readonly object _lockObject1 = new object();
private static readonly object _lockObject2 = new object();

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Thread t1 = new Thread(new ThreadStart(Method1));
Thread t2 = new Thread(new ThreadStart(Method2));
Thread t3 = new Thread(new ThreadStart(Method3));

t1.Start();
t2.Start();
t3.Start();

Console.ReadLine();
}

static public void Method1()
{
lock(_lockObject1)
{
// critical section 1
for(int i = 0; i < 20; i++)
{
Console.WriteLine("Thread:1 " + i);
Thread.Sleep(100);
}
}
}
static public void Method2()
{
lock(_lockObject1)
{
// again critical section 1
for(int i = 0; i < 20; i++)
{
Console.WriteLine("Thread:2 " + i);
Thread.Sleep(100);
}
}
}
static public void Method3()
{
lock(_lockObject2)
{
// critical section 2
for(int i = 0; i < 20; i++)
{
Console.WriteLine("Thread:3 " + i);
Thread.Sleep(100);
}
}
}

}
}
---
"Art happens when you least expect it."

-- modified at 19:53 Wednesday 9th November, 2005
AnswerRe: C# lock statement Pin
Judah Gabriel Himango9-Nov-05 5:20
sponsorJudah Gabriel Himango9-Nov-05 5:20 
GeneralRe: C# lock statement Pin
Jon Rista9-Nov-05 10:54
Jon Rista9-Nov-05 10:54 
NewsRe: C# lock statement Pin
Den2Fly9-Nov-05 13:45
Den2Fly9-Nov-05 13:45 
QuestionFTP Pin
v.k.s9-Nov-05 2:48
v.k.s9-Nov-05 2:48 
AnswerRe: FTP Pin
lmoelleb9-Nov-05 20:57
lmoelleb9-Nov-05 20:57 
QuestionC# control licence question Pin
dojdoj9-Nov-05 2:47
dojdoj9-Nov-05 2:47 
Questionhow to manipulate exe files ? Pin
hdv2129-Nov-05 2:09
hdv2129-Nov-05 2:09 
AnswerRe: how to manipulate exe files ? Pin
User 66589-Nov-05 3:48
User 66589-Nov-05 3:48 
QuestionC# and IUnknown (COM) Pin
MLS9-Nov-05 1:22
MLS9-Nov-05 1:22 
QuestionRecursive function to return the max key of nodes of a singly connected list? Pin
ektoras9-Nov-05 0:35
ektoras9-Nov-05 0:35 
QuestionRecursive function to display the number of nodes of a singly connected list? Pin
ektoras9-Nov-05 0:31
ektoras9-Nov-05 0:31 
AnswerRe: Recursive function to display the number of nodes of a singly connected list? Pin
J4amieC9-Nov-05 2:16
J4amieC9-Nov-05 2:16 
GeneralRe: Recursive function to display the number of nodes of a singly connected list? Pin
ektoras9-Nov-05 3:36
ektoras9-Nov-05 3:36 
GeneralRe: Recursive function to display the number of nodes of a singly connected list? Pin
Wjousts9-Nov-05 4:18
Wjousts9-Nov-05 4:18 
GeneralRe: Recursive function to display the number of nodes of a singly connected list? Pin
ektoras9-Nov-05 7:20
ektoras9-Nov-05 7:20 
GeneralRe: Recursive function to display the number of nodes of a singly connected list? Pin
Wjousts9-Nov-05 7:35
Wjousts9-Nov-05 7:35 
GeneralRe: Recursive function to display the number of nodes of a singly connected list? Pin
J4amieC9-Nov-05 4:45
J4amieC9-Nov-05 4:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.