witkowski01
witkowski01

Reputation: 9

Sleep all threads in program except current/main one in C#

Main question is: how to sleep all threads running in program except current/main one?

I have program with use connection to MS SQL Server via EF, the problem is that sometimes when program is running it crush when we put computer to sleep mode and wake up, problem seems to be connection to DB. Program may try to get some data from DB when connection is not yet recovered. Idea was to detect event of PowerModeChanged and when we detect Suspend mode all threads except current should be put into sleep till the next event Resume. I do not know how to put to sleep or programmatically hold work of different threads than current one. I found this:

ProcessThreadCollection currentThreads = Process.GetCurrentProcess().Threads;

but I do not find correct way to cast it, or find them by Id, and be able to put them to sleep.

Is there any way to do this?

P.S. Some more code bellow (SystemEvents_PowerModeChanged function, unfinished yet):

[DllImport("Kernel32", EntryPoint = "GetCurrentThreadId", ExactSpelling = true)]
public static extern Int32 GetCurrentWin32ThreadId();
        private void SystemEvents_PowerModeChanged(object sender, Microsoft.Win32.PowerModeChangedEventArgs e)
        {
            if (e.Mode == PowerModes.Suspend)
            {
               ProcessThreadCollection currentThreads = Process.GetCurrentProcess().Threads;

                int currentThreadId = GetCurrentWin32ThreadId();
                Console.WriteLine("CURRENT thread: " + currentThreadId);

                foreach (ProcessThread thread in currentThreads)
                {
                    if (thread.Id != currentThreadId)
                    {
                        Console.WriteLine("Disable thread: " + thread.Id);
                        // Disable threads
                    }
                }
            }
            else if (e.Mode == PowerModes.Resume)
            {
                ProcessThreadCollection currentThreads = Process.GetCurrentProcess().Threads;
                int currentThreadId = GetCurrentWin32ThreadId();
                Console.WriteLine("CURRENT thread: " + currentThreadId);
                foreach (ProcessThread thread in currentThreads)
                {
                    if (thread.Id != currentThreadId)
                    {
                        Console.WriteLine("Enable thread: " + thread.Id);
                        // Enable threads
                    }
                }
            }
        }

Upvotes: 0

Views: 1268

Answers (1)

René Vogt
René Vogt

Reputation: 43936

I think this is the wrong approach. Your threads themselves should react on those system events and act accordingly.

To suspend and resume them from another thread is dangerous. You have absolutly no control over the code those threads are currently executing, or if this will still work correctly when they get resumed again.

That's one of the reasons why Thread.Suspend() and Thread.Resume() are deprecated and should no longer be used.


Change your working threads so that they detect the power events themself and can act accordingly: go into a defined state and start work again when power is resumed. Or terminate and get completely restartet later.

Upvotes: 2

Related Questions