Click here to Skip to main content
15,913,055 members
Home / Discussions / C#
   

C#

 
AnswerRe: Threading Question Pin
Nader Elshehabi12-Dec-06 7:40
Nader Elshehabi12-Dec-06 7:40 
GeneralRe: Threading Question Pin
NameYou12-Dec-06 8:25
NameYou12-Dec-06 8:25 
GeneralRe: Threading Question Pin
Nader Elshehabi12-Dec-06 8:54
Nader Elshehabi12-Dec-06 8:54 
GeneralRe: Threading Question Pin
NameYou12-Dec-06 9:47
NameYou12-Dec-06 9:47 
GeneralRe: Threading Question Pin
Nader Elshehabi12-Dec-06 17:11
Nader Elshehabi12-Dec-06 17:11 
GeneralRe: Threading Question Pin
NameYou13-Dec-06 2:18
NameYou13-Dec-06 2:18 
GeneralRe: Threading Question Pin
Nader Elshehabi13-Dec-06 6:16
Nader Elshehabi13-Dec-06 6:16 
GeneralRe: Threading Question [modified] Pin
Eric Dahlvang12-Dec-06 10:03
Eric Dahlvang12-Dec-06 10:03 
I'm no threading expert, but I think you need to consider that NameYou is creating multiple instances of MyObject. When you create LockObject in each instance, that means that each instances Connect code has a lock -- which is different from locking that piece of code for EVERY instance. You can see the difference by comparing the following two classes:

1)This just locks the code in this instances connect method.
public class MyObject
{
    public static string cFieldVal;

    public MyObject()
    {
    }

    public void Connect()
    {
        //Starts all the socket stuff..
        object LockObject = new object();
        lock(LockObject)
        {
            MessageBox.Show(MyObject.cFieldVal);
            MyObject.cFieldVal=MyObject.cFieldVal+="x";
            //StreamWriter MyStream = File.AppendText("MyFile.txt");
            //MyStream.WriteLine(Input);
           // MyStream.Close();
        }
    }
    //...OTHER SOCKET RELATED FUNCTIONS GO HERE.
}


2)This code locks a static field, so it is locked for EACH EVERY instance.
public class MyObject
{

    public static string cFieldVal="";

    public MyObject()
    {
    }

    public void Connect()
    {
        //Starts all the socket stuff..

        lock(cFieldVal)
        {
            MessageBox.Show(MyObject.cFieldVal);
            MyObject.cFieldVal=MyObject.cFieldVal+="x";
          //  StreamWriter MyStream = File.AppendText("MyFile.txt");
          //  MyStream.WriteLine(Input);
          //  MyStream.Close();
        }
    }
//...OTHER SOCKET RELATED FUNCTIONS GO HERE.
}






-- modified at 8:57 Wednesday 13th December, 2006

--EricDV Sig---------
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them.
- Laurence J. Peters

GeneralRe: Threading Question Pin
Andy Brummer12-Dec-06 17:21
sitebuilderAndy Brummer12-Dec-06 17:21 
GeneralRe: Threading Question Pin
Eric Dahlvang13-Dec-06 3:02
Eric Dahlvang13-Dec-06 3:02 
GeneralRe: Threading Question Pin
Nader Elshehabi12-Dec-06 17:24
Nader Elshehabi12-Dec-06 17:24 
AnswerRe: Threading Question Pin
karam chandrabose12-Dec-06 22:45
karam chandrabose12-Dec-06 22:45 
GeneralRe: Threading Question Pin
NameYou13-Dec-06 1:32
NameYou13-Dec-06 1:32 
AnswerRe: Threading Question Pin
sno-114-Dec-06 3:52
sno-114-Dec-06 3:52 
Questionfont Pin
hadad12-Dec-06 6:49
hadad12-Dec-06 6:49 
AnswerRe: font Pin
Nader Elshehabi12-Dec-06 7:59
Nader Elshehabi12-Dec-06 7:59 
Questionsending messages to a window from a windows service Pin
George_Lucian12-Dec-06 5:49
George_Lucian12-Dec-06 5:49 
AnswerRe: sending messages to a window from a windows service Pin
Nader Elshehabi12-Dec-06 8:05
Nader Elshehabi12-Dec-06 8:05 
QuestionDisabling the context menu [interop.word.dll] Pin
Eytukan12-Dec-06 4:38
Eytukan12-Dec-06 4:38 
QuestionWeb Service Proxy Problem! Pin
Mehdi Mousavi12-Dec-06 4:32
Mehdi Mousavi12-Dec-06 4:32 
AnswerRe: Web Service Proxy Problem! Pin
Andy Brummer12-Dec-06 17:27
sitebuilderAndy Brummer12-Dec-06 17:27 
AnswerRe: Web Service Proxy Problem! Pin
Mehdi Mousavi12-Dec-06 19:11
Mehdi Mousavi12-Dec-06 19:11 
GeneralRe: Web Service Proxy Problem! Pin
Andy Brummer13-Dec-06 4:10
sitebuilderAndy Brummer13-Dec-06 4:10 
QuestionToolTab Gone Hay Wire Pin
burrows.stephen12-Dec-06 4:30
burrows.stephen12-Dec-06 4:30 
AnswerRe: ToolTab Gone Hay Wire Pin
Judah Gabriel Himango12-Dec-06 6:47
sponsorJudah Gabriel Himango12-Dec-06 6:47 

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.