المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Using lock statement inside overloaded methods



C# Programming
10-23-2009, 12:21 AM
Hello to all.

I'm new in this message board, and I couldn't find this subject elsewhere.
The thing is, I need to know if I can use the lock satement inside an overloaded method, i.e.:
public static string MyString = 0;
private static object objMyLock = new object();

public static void DoSomething(int Index, string Field)
{
lock (objMyLock)
{
MyString = Field + Index.ToString();
}
}

public static bool DoSomething(string Name, string Field)
{
lock (objMyLock)
{
MyString = Field + Name;
}
}

Does the lock Statement used like this, ensures that the variable MyString is changed by one thread at a time?

Thank you!

NMFPacheco - Portugal