I have 3 seperate project . Named as
- SharedData -> Class Library
- Thread 1 -> Window application
- Thread 2 -> Console Application
SharedData contains the following 2 functions:
public class SharedData
{
static private Mutex mut = new Mutex();
public static void PrintNumbersMutex()
{
mut.WaitOne();
for (int i = 0; i < 500000000; i++)
{
Console.WriteLine(" i = " + i);
}
mut.ReleaseMutex();
}
}
Thread1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SharedData.DataHolder.PrintNumbersMutex();
}
}
Thread 2:
public class User2
{
static void Main(string[] args)
{
SharedData.DataHolder.PrintNumbersMutex();
Console.ReadKey();
}
}
Thread 1
and Thread 2
are using SharedData library
. I have started both project at same time . As I have used lock
and mutex
still both project are able to call shared function differently. My problem is if one project is executing shared function other should wait. But it doesn't happening . What is problem could you tell me please.