0

I am creating a set of thread to access a web-service and return values. I have added threading.timer to each thread and trying to deallocated the resouces used by thread if the timeout timer in threading.timer exceeds.

Here how i did it.

class ThreadTest
    {
        System.Threading.Timer ThreadTimeoutTimer = null;
        private void ThreadStartMain()
            {

      ParameterizedThreadStart start = new ParameterizedThreadStart(new    ThreadTest().ReadData);
                Thread t = new Thread(start);
                t.Start();
            }

        public void ReadData(object stat)
        {
            int _timeOutTimer = 60000;

            Thread currentThread = Thread.CurrentThread;
            ThreadTimeoutTimer = new System.Threading.Timer(ReleaseThread, currentThread, _timeOutTimer, Timeout.Infinite);

            webservcieclient webcl = new webservcieclient();
            webcl.GetData();

            ThreadTimeoutTimer = null;
            UpdateDB();
        }

        private void ReleaseThread(object state)
        {
            Thread runningThread = (Thread)state;
            if (runningThread.IsAlive)
            {
                runningThread.Abort();
            }
        }
    }

So to check how it works , i have made webservcieclient exceeds timeout time. then the timer fired and it abort the thread.

But then what i have seen is the webservcieclient returns after network/http exception after sometimes and it has executed and throws an another exception saying the thread was aborted.Also UpdateDB() has run twice. How it has run because the thread already aborted. Is it because there is another thread started while accessing web-service methods?

FatalError
  • 77
  • 2
  • 9

1 Answers1

0

You get a ThreadAbortException because that's what Thread.Abort does. But more importantly, do NOT use Thread.Abort!

See: What's wrong with using Thread.Abort()

If you want to be able to cancel your threads, you should be using a higher level construct, like TPL's tasks and CancellationToken. Here's a short example:

public void Main()
{
    //create a token that will be automatically cancelled after 60000 seconds
    var cts = new CancellationTokenSource(60000); 

    Task task = Task.Run(() => ReadData(cts.Token));
}

private void ReadData(CancellationToken token)
{
    while (! token.IsCancellationRequested)
    {
        //do something
    }
}
Community
  • 1
  • 1
dcastro
  • 66,540
  • 21
  • 145
  • 155
  • But how come it happens. since the thread already aborted. It is the network exception because it come after around 120 sec . So my question is if the thread already aborted how it has run two time. The updateDB() calls twice. – FatalError Jul 19 '14 at 15:32
  • @FatalError The fact that it runs twice must be explained by some other part of your code, not shown in the question. You should have posted a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) – dcastro Jul 19 '14 at 16:07