I want to call two methods inside a class with reflection but with different instances. Is it possible to lock first method in first instance and while first method is locked , second instance couldn't call second method ? because first method is locked . and when first method's lock is released, second method be able to be fired
consider this :
private static object globalLock = new object();
public bool method1()
{
lock (globalLock)
{
Console.WriteLine("in Method 1 wait");
Thread.Sleep(30000);
Console.WriteLine("in Method 1 ReleaseMutex");
return true;
}
}
public bool method2()
{
lock (globalLock)
{
Console.WriteLine("in Method 2 wait");
Thread.Sleep(30000);
Console.WriteLine("in Method 2 ReleaseMutex");
return true;
}
}
and then make instances with
Assembly testAssembly = Assembly.LoadFile(@"C:\Users\visual studio 2013\Projects\LockTest\ClassLibrary1\bin\Debug\ClassLibrary1.dll");
Type calcType = testAssembly.GetType("ClassLibrary1.Class1");
object calcInstance = Activator.CreateInstance(calcType);
var x = (bool)calcType.InvokeMember("method1",BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public,null, calcInstance, null);
Console.WriteLine("11111 " + x);
Console.ReadLine();
and this :
Assembly testAssembly2 = Assembly.LoadFile(@"C:\Users\visual studio 2013\Projects\LockTest\ClassLibrary1\bin\Debug\ClassLibrary1.dll");
Type calcType2 = testAssembly2.GetType("ClassLibrary1.Class1");
object calcInstance2 = Activator.CreateInstance(calcType2);
var y = (bool)calcType2.InvokeMember("method2", BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public, null, calcInstance, null);
Console.WriteLine("22222 " + y);
Console.ReadLine();
Aucun commentaire:
Enregistrer un commentaire