0

I am developing one small excel application , in which i have three threads accessing the same function. I want that function must be accessed by one thread when other thread is done. can some one help me in this. below code i tried.

              if ((checkBox1.Checked == true) && (textBox2.Text != ""))
                {
                    Run_thread = new Thread(() => READ_MAPPING_FILE_PATHS(textBox2.Text, 5, 15));
                    Run_thread.Start();

                }
                if ((checkBox2.Checked == true) && (textBox5.Text != ""))
                {
                    Run_thread1 = new Thread(() => READ_MAPPING_FILE_PATHS(textBox5.Text, 7, 9));
                    Run_thread1.Start();
                    //READ_MAPPING_FILE_PATHS(textBox5.Text, 7, 9);
                    check++;
                }
                if ((checkBox3.Checked == true) && (textBox6.Text != ""))
                {
                   Run_thread2 = new Thread(() => READ_MAPPING_FILE_PATHStextBox6.Text, 5, 15);
                    Run_thread.Start(); 
                }

//function definition which is accessed by threads.

    public void READ_MAPPING_FILE_PATHS(string path , int A429, int ACSB)
    {
        // do something
     }

i tried using AutoResetEvent event property to handle but this leads to my application on not responding mode.

AutoResetEvent myResetEvent = new AutoResetEvent(false);

  waitHandle.WaitOne();

 waitHandle.Set();

1 Answers1

0

You can simply use lock :

static object _lock = new object();
public void READ_MAPPING_FILE_PATHS(string path , int A429, int ACSB)
{
   lock(_lock)
   {
      // do something
   }
}
Zein Makki
  • 29,485
  • 6
  • 52
  • 63