Click here to Skip to main content
15,920,383 members
Home / Discussions / C#
   

C#

 
QuestionRe: Block a method execution Pin
Michel Godfroid19-Apr-10 20:23
Michel Godfroid19-Apr-10 20:23 
GeneralRe: Block a method execution Pin
Luc Pattyn20-Apr-10 1:11
sitebuilderLuc Pattyn20-Apr-10 1:11 
GeneralRe: Block a method execution Pin
Hum Dum20-Apr-10 1:45
Hum Dum20-Apr-10 1:45 
GeneralRe: Block a method execution Pin
Luc Pattyn20-Apr-10 1:56
sitebuilderLuc Pattyn20-Apr-10 1:56 
AnswerRe: Block a method execution Pin
Calla19-Apr-10 20:08
Calla19-Apr-10 20:08 
GeneralRe: Block a method execution Pin
Hum Dum19-Apr-10 20:18
Hum Dum19-Apr-10 20:18 
GeneralRe: Block a method execution Pin
Calla19-Apr-10 20:39
Calla19-Apr-10 20:39 
GeneralRe: Block a method execution Pin
Michel Godfroid19-Apr-10 21:12
Michel Godfroid19-Apr-10 21:12 
To clarify further:
The class which contains your lock (and method), should be instantiated only once, preferably before starting your threads. You should then pass this class object to your threads, so that they all look at the same object.
Do NOT instantiate the class from within the threads, as this will create separate objects (and separate locks)

class LockObject
{
    private Object MyLock = new Object();

    void WriteMessage(string msg)
    {
         lock(MyLock)
         {
            ...
         }
    }
      
    public void Dowrite()
    {
        while(true)
        {
              WriteMessage("Whatever");
        }
    }
}
</code>



<code>class Test
{
    static void Main()
    {
        Thread[] threads = new Thread[10];
        LockObj = new LockObject();
        for (int i = 0; i < 10; i++)
        {
            Thread t = new Thread(new ThreadStart(LockObj.DoWrite));
            threads[i] = t;
        }
        for (int i = 0; i < 10; i++)
        {
            threads[i].Start();
        }
    }
}

GeneralRe: Block a method execution Pin
Calla19-Apr-10 21:18
Calla19-Apr-10 21:18 
JokeRe: Block a method execution Pin
Michel Godfroid19-Apr-10 21:22
Michel Godfroid19-Apr-10 21:22 
GeneralRe: Block a method execution Pin
Mycroft Holmes19-Apr-10 22:47
professionalMycroft Holmes19-Apr-10 22:47 
GeneralRe: Block a method execution Pin
Michel Godfroid19-Apr-10 23:14
Michel Godfroid19-Apr-10 23:14 
GeneralRe: Block a method execution Pin
Hum Dum19-Apr-10 21:28
Hum Dum19-Apr-10 21:28 
GeneralRe: Block a method execution Pin
Hum Dum20-Apr-10 1:53
Hum Dum20-Apr-10 1:53 
GeneralRe: Block a method execution Pin
Michel Godfroid20-Apr-10 4:35
Michel Godfroid20-Apr-10 4:35 
Questionhow to display current time. Pin
sudhir behera19-Apr-10 17:59
sudhir behera19-Apr-10 17:59 
AnswerRe: how to display current time. Pin
Harvey Saayman19-Apr-10 18:03
Harvey Saayman19-Apr-10 18:03 
AnswerRe: how to display current time. Pin
Abhinav S19-Apr-10 18:10
Abhinav S19-Apr-10 18:10 
AnswerRe: how to display current time. Pin
Dr.Walt Fair, PE19-Apr-10 18:16
professionalDr.Walt Fair, PE19-Apr-10 18:16 
AnswerRe: how to display current time. Pin
Luc Pattyn20-Apr-10 1:13
sitebuilderLuc Pattyn20-Apr-10 1:13 
QuestionSystem.OutOfMemoryException when creating big array Pin
Xmen Real 19-Apr-10 17:17
professional Xmen Real 19-Apr-10 17:17 
AnswerRe: System.OutOfMemoryException when creating big array Pin
Not Active19-Apr-10 17:22
mentorNot Active19-Apr-10 17:22 
GeneralRe: System.OutOfMemoryException when creating big array Pin
Xmen Real 19-Apr-10 17:27
professional Xmen Real 19-Apr-10 17:27 
GeneralRe: System.OutOfMemoryException when creating big array Pin
Not Active19-Apr-10 18:01
mentorNot Active19-Apr-10 18:01 
GeneralRe: System.OutOfMemoryException when creating big array Pin
Xmen Real 19-Apr-10 21:07
professional Xmen Real 19-Apr-10 21:07 

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.