1

I am developing one small excel application, in that i am using 3 threads reading some function. Its not necessary that all threads are invoked to read particular function, based on checkbox selection , particular thread will run.

Threads i defined under global part as listed below.

 Thread Run_thread = null, Run_thread1 = null, Run_thread2 = null;

/* calling of threads based on checkbox selection*/

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

now if user selects checkbox 1 and checkbox2 then Run_thread1 and Run_thread will be in IsAlive state and Run_thread2 will have null;

now checking thread is alive or not

    if (!(Run_thread.IsAlive || Run_thread1.IsAlive || Run_thread2.IsAlive))
                    {
                      //do something
                    }

                   else
                    {
                             //message thread are in running mode.
                     }

in above check error is coming when Run_thread2 is checked for status

 "Object reference not set to an instance of an object."

can some one help me in fixing this issue. I am new to thread.

1 Answers1

3

Obviously some fields will be null if you do not create instances, which you do based on the corresponding checkboxes.

Try this:

if (!(Run_thread != null && Run_Thread.IsAlive ||
     Run_thread1 != null && Run_thread1.IsAlive ||
     Run_thread2 != null && Run_thread2.IsAlive))
Maarten
  • 22,527
  • 3
  • 47
  • 68
  • its not working for third condition ..is there is some way to initialize thread other than null value.. – Sanjeev Kumar Sep 08 '16 at 08:13
  • Can you explain a little more than 'its not working'? – Maarten Sep 08 '16 at 08:14
  • Sanjeev Kumar please have a look at this course, it is absolutely free https://mva.microsoft.com/en-us/training-courses/c-fundamentals-for-absolute-beginners-16169?l=LK8n0uQIC_306218949 – shfire Sep 08 '16 at 08:19
  • actually what i want is if i run check condition, it should check any of the thread is running or not...if none of them are running then go head perform activity.. but this condition allowing it to do its activity even threads are running.. – Sanjeev Kumar Sep 08 '16 at 08:19
  • some silly mistake is there but unable to figure it out – Sanjeev Kumar Sep 08 '16 at 08:21
  • @SanjeevKumar I think the condition is checking that. The current check is `not (any thread alive)` which translate to `no thread alive`. – Maarten Sep 08 '16 at 08:21
  • by using this condition i am trying to check if none of them are alive then do somethings else not.. (keeping not condition at start) if (!(Run_thread.IsAlive || Run_thread1.IsAlive || Run_thread2.IsAlive)) – Sanjeev Kumar Sep 08 '16 at 08:24
  • thanks Maarten , i modified the condition and it worked.... – Sanjeev Kumar Sep 08 '16 at 08:40