Click here to Skip to main content
15,887,361 members
Home / Discussions / C#
   

C#

 
GeneralRe: getting data over website Pin
SantokhSingh18-Nov-10 17:41
SantokhSingh18-Nov-10 17:41 
QuestionGrid view Caption Pin
4anusha417-Nov-10 19:57
4anusha417-Nov-10 19:57 
AnswerRe: Grid view Caption Pin
Pete O'Hanlon17-Nov-10 23:07
mvePete O'Hanlon17-Nov-10 23:07 
QuestionThreading in C# Pin
Anil Kumar.Arvapalli17-Nov-10 19:29
Anil Kumar.Arvapalli17-Nov-10 19:29 
AnswerRe: Threading in C# Pin
Abhinav S17-Nov-10 20:56
Abhinav S17-Nov-10 20:56 
GeneralRe: Threading in C# Pin
Anil Kumar.Arvapalli17-Nov-10 23:49
Anil Kumar.Arvapalli17-Nov-10 23:49 
GeneralRe: Threading in C# Pin
Dave Kreskowiak18-Nov-10 1:52
mveDave Kreskowiak18-Nov-10 1:52 
GeneralRe: Threading in C# Pin
_Erik_18-Nov-10 2:59
_Erik_18-Nov-10 2:59 
Your code is not synchronizing anything. Look:

C#
class Thread1
{
    private Object thisLock;

    public void ThreadWriter(StreamWriter sw)
    {

        thisLock = new Object();
        lock (this)
        {
            for (int i = 0; i < 10; i++)
            {
                sw.WriteLine("Thread 1 = " + i);
                Thread.Sleep(100);
            }
            sw.Close();
        }
    }
}


Your thisLock object is completely useless: You create a new instance of it within ThreadWriter method but use it for nothing else. Using it as the argument for lock would change nothing becouse of two reasons: you create a new instance of it every time the method runs, so you would always be loosing the previously locked reference, and it is private, so the other classes cannot use it for their locks.

Using lock(this) is useless as well, becouse each class just locks itself for accessing the StreamWriter more than once at a time, but does not lock the other classes since the other classes are doing the same, I mean, each one uses a different reference for their respective locks.

What I still don't know is if you want all of your threads to be able to write at the same time in a thread safe way, or if you want only one thread writting everything it has to write and the other ones waiting. What kind of output would you like to get?
GeneralRe: Threading in C# Pin
Anil Kumar.Arvapalli18-Nov-10 3:53
Anil Kumar.Arvapalli18-Nov-10 3:53 
GeneralRe: Threading in C# [modified] Pin
_Erik_18-Nov-10 4:08
_Erik_18-Nov-10 4:08 
GeneralRe: Threading in C# Pin
Anil Kumar.Arvapalli18-Nov-10 17:54
Anil Kumar.Arvapalli18-Nov-10 17:54 
AnswerRe: Threading in C# Pin
Pete O'Hanlon17-Nov-10 22:05
mvePete O'Hanlon17-Nov-10 22:05 
QuestionPlot histogram Pin
pancakeleh17-Nov-10 18:47
pancakeleh17-Nov-10 18:47 
AnswerRe: Plot histogram Pin
Dr.Walt Fair, PE17-Nov-10 19:28
professionalDr.Walt Fair, PE17-Nov-10 19:28 
GeneralRe: Plot histogram [modified] Pin
pancakeleh17-Nov-10 19:41
pancakeleh17-Nov-10 19:41 
GeneralRe: Plot histogram Pin
Richard MacCutchan18-Nov-10 1:36
mveRichard MacCutchan18-Nov-10 1:36 
GeneralRe: Plot histogram Pin
pancakeleh18-Nov-10 15:02
pancakeleh18-Nov-10 15:02 
Questiontree visualization Pin
pstsp91117-Nov-10 18:40
pstsp91117-Nov-10 18:40 
AnswerRe: tree visualization Pin
Alan Balkany23-Nov-10 3:59
Alan Balkany23-Nov-10 3:59 
QuestionMerge image problem Pin
pancakeleh17-Nov-10 15:48
pancakeleh17-Nov-10 15:48 
AnswerRe: Merge image problem Pin
Dave Kreskowiak17-Nov-10 16:00
mveDave Kreskowiak17-Nov-10 16:00 
GeneralRe: Merge image problem Pin
pancakeleh17-Nov-10 16:03
pancakeleh17-Nov-10 16:03 
GeneralRe: Merge image problem Pin
fjdiewornncalwe17-Nov-10 16:32
professionalfjdiewornncalwe17-Nov-10 16:32 
GeneralRe: Merge image problem Pin
pancakeleh17-Nov-10 16:36
pancakeleh17-Nov-10 16:36 
AnswerRe: Merge image problem Pin
Pete O'Hanlon17-Nov-10 21:59
mvePete O'Hanlon17-Nov-10 21:59 

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.