AnsweredAssumed Answered

object clone or copy including method ???

Question asked by TRicci on Nov 9, 2009
i am using backgroundworker to manage thread.  i need to select object to use in do work method below.
private void SelectObject()
        {
            //reference to obj
            if (objControlSelector == "CounterA")
            {
                CounterA obj = new CounterA();
                obj = cntrA;
                obj.msgA();
            }

            if (objControlSelector == "CounterB")
            {
                CounterB obj = new CounterB();
                obj = cntrB;
                obj.msgB();
            }
        }
then use object in do work below


private void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker senderWorker = sender as BackgroundWorker;

            for (index = 0; index< 100; index++) //100
            {
                //do more work here
                obj.msgA();

                Thread.Sleep(10);
                int progressInPercent = (int)(((decimal)(index + 1)/100 * 100));
                worker.ReportProgress(progressInPercent);
                  
                //handle cancellation of the thread
                //explict hanlding of cancellation is needed here to avoud null reference exception

                   if(senderWorker.CancellationPending)
                   {
                        e.Cancel = true;
                        break;
                   }
            }
        }  

Outcomes