views:

87

answers:

4

Hello there,

I am building up a multi-threaded application where I spawn three threads when application starts and these threads continue to run for application lifetime. All my threads are exclusive and do not interfere with each other in anyway. Now a user can suspend the application and, here I want to suspend or, say, abort my threads.

I am currently spawning threads as foreground threads, but I guess changing them to background threads wouldn't affect my application anyway (except they(foreground threads) would keep the application alive until they finish).

I would ask people here to suggest an approach to suspend the application via thread.suspend() or thread.abort(). I know thread.suspend is obsolete and risky, but is it harmful for my application also where I am not using any type of synchronization.

PS: My threads are saving and retrieving some data to & from embedded database(sqlite) every minute.

+2  A: 

Use the Blocking mechanisms like WaitHandles (ManualResetEvent, AutoResetEvent), Monitor, Semaphore etc...

Andrew

P.S. the question is quite broad so I would ultimately recommend reading up on proven practices and principles of Multi Threading which will include synchronization. Your requirements do not sound too complex so I am sure you will be able to research the best way which suits your needs.

REA_ANDREW
Can Wait handles be used to immediately suspend or abort a thread ? Also, I fail to understand that what problem can be there if I immediately abort my threads since I am not using any critical region or syncronization.
+1  A: 

You could create a mutex and let the threads wait for a signal on that mutex. This way your threads are not destroyed but they will sleep almost without consuming resources.

Mutex.WaitOne

dbemerlin
During the application, my threads sleep for most of the time and then wakes up to perform their work and then, go to sleep again. This process will continue until the application is temporarily suspended or signed out permanently. However, I want the thread to quit immediately as soon as a user suspends or sign-out the application. Do you believe this can be achieved using Mutex or wait handles ? If so, can you please elaborate (just a humble request) because I have just started on this multi-threading thing.Thanks.
No, that is not possible but do you really need to _destroy_ the thread or do you just want to stop the thread from processing? The answer should mostly depend on the resources your threads require (i.e. keeping big objects in memory)
dbemerlin
Well, I want my threads to be suspended temporarily when user suspends and then when user resumes, I want my thread to run again. An User can also Sign-out of the application where I want to abort all my thread. What I am currently doing is that I am aborting the thread in both cases.
I have no time to write an example for your case but take a look at the example here: http://msdn.microsoft.com/en-us/library/58195swd.aspx It suspends the thread until the main thread signals it to continue. Using this technique you can suspend your child threads with WaitOne() when the user suspends and then signal with an ManualResetEvent or similar ( http://msdn.microsoft.com/en-us/library/system.threading.manualresetevent.aspx ) when the user clicks continue. The threads won't do anything in the meantime.
dbemerlin
A: 

Actually, this situation is the only one where Thread.Suspend does make sense. The reason it's obsoleted is because people misuse it to fake synchronization, or use it on threads they do not own (e.g., ThreadPool threads).

Stephen Cleary
I feel the same. Cheers !!
+1  A: 

I always use ManualResetEvent for this:

class Myclass
{
    ManualResetEvent _event;
    Thread _thread;

    public void Start()
    {
        _thread = new Thread(WorkerThread);
        _thread.IsBackground = true;
        _thread.Start();
    }

    public void Stop()
    {
        _event.Set();
        if (!_thread.Join(5000))
            _thread.Abort();
    }

    private void WorkerThread()
    {
        while (true)
        {
            // wait 5 seconds, change to whatever you like
            if (_event.WaitOne(5000))
                break; // signalled to stop

            //do something else here
        }
    }
}
jgauffin