I am using C# lock to block code execution. It is not working if we use dynamic string input in Lock.
public class ParameterClass
{
public string A = string.Empty;
public Int64 B = 0;
}
class Program
{
static void Main(string[] args)
{
ParameterClass parm = new ParameterClass { A = "Test", B = 1 };
Thread thread = new Thread(ThreadFun);
thread.Start(parm);
System.Threading.Thread.Sleep(2000);
parm = new ParameterClass { A = "Test", B = 1 };
ThreadFun(parm);
}
public static void ThreadFun(object para)
{
ParameterClass parameter = (ParameterClass)para;
lock (parameter.B.ToString())
{
Console.WriteLine(DateTime.Now);
System.Threading.Thread.Sleep(20000);
}
}
}
Here , I have put Thread.Sleep(20000) - 20 seconds inside Lock statement. my expected result is the code block should be locked based on parameter.B...
Can anyone help me to proceed?