1

I have to implement validation on datatable in my project but it should be running on different background thread, not main UI thread. And once it's done it should show list of errors on screen. This validation runs from differences different places, there are 6 to 7 places which might causes data table error. I have below function written which can be called from those 6 to 7 places to perform validation:

Thread validationThread = null;

    public void RunDataTableValidation()
        {      
                validationThread = new Thread(new ThreadStart(() => validationHandler()));
                validationThread.Name = "DataGridValidationThread";
                validationThread.IsBackground = true;
                validationThread.Start();
            }
        }

    public void validationHandler(Label FakeLabel)
        {

         try { //all validation code}

         finally
            {
                System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() => ProcessingDone()));
            }
        }

      void ProcessingDone()
        {          
            if (validationThread != null && validationThread.ThreadState ==   ThreadState.Stopped)
           {
                validationThread.Abort();
            }           
        }

My Issue is: Every time I abort thread when once it's done with it's work. But I also want to Abort thread if current thread is still running and some other code calls this function and start new thread. I know I can use ThreadPool but please help me - how I can implement thread pool in my above code? So it automatically kills all running thread in thread pool before it starts new thread

Edit:

       public void RunDataTableValidation()
        {
           ThreadPool.QueueUserWorkItem(o =>
            {
                try
                {
                    validationHandler();

                }
                catch (NotSupportedException) { }
            });
      }

      public void validationHandler(Label FakeLabel)
      {

        try
        {
            foreach (DataRow drRow in this.CurrentDataTable.Rows)
            {

            }     
            Thread.Sleep(7000); ( i put sleep right now for testing becoz this code takes few secs becoz of 500+ rows of datagrid         
        }
        finally
        {
            System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() => ProcessingDone()));
        }

    }
user2203652
  • 67
  • 2
  • 8

1 Answers1

0

Never call Thread.Abort(). See for example here for an explanation. It isn't even necessary in your example code, as a Thread automatically terminates when it returns from the method that you passed to its constructor.

Anyway, just run your validation method in a ThreadPool thread by QueueUserWorkItem:

private bool terminate;

public void TerminateThread()
{
    terminate = true;
}

public void RunDataTableValidation()
{    
    ThreadPool.QueueUserWorkItem(
        o =>
        {
            try
            {
                while (!terminate)
                {
                    // all validation code
                }
            }
            finally
            {
                Dispatcher.BeginInvoke((Action)ProcessingDone);
            }
            terminate = false;
        });
}

private void ProcessingDone()
{
    // update the UI here
}
Community
  • 1
  • 1
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • my only question is,in your case what if there is previous thread is running while new thread is just started. I want to kill previous thread. – user2203652 Apr 19 '13 at 16:45
  • You wouldn't kill a thread. Just return from the thread method. You may for example have a terminate flags that is periodically checked by the thread method. – Clemens Apr 19 '13 at 16:50
  • I tried your code but it's affecting my UI thread. Please see my code in Edit: – user2203652 Apr 19 '13 at 17:14
  • can you also share example how set terminate flag option, and return from method. May be I am not that good at threading – user2203652 Apr 19 '13 at 17:52
  • I understand what you are saying, but it's affecting my UI thread. it makes my UI operation pretty slow. It should run in background and only shows error at the end on screen. But my question is why not it's running in background. – user2203652 Apr 19 '13 at 18:25