Resume multithreaded application in PyCharm debug - multithreading

Consider the following situation:
We have a multithreaded app
We're paused at one thread at the breakpoint
we set a breakpoint in another line of code
a new threads hits that breakpoint
so we have 2 threads paused at the same time
we want to resume the 2nd thread but still having the 1st one paused
if we select that thread and click resume -> both threads would resume
is there a way to resume only 1 thread? Demo

Related

Python How to have OS block the process and resume on timer

I have been looking around and I can't seem to find a module that allows me to block the current process until the timer is done. What I don't want to do is utilize the CPU while waiting (busy waiting). I want the process to be blocked/suspended by the operating system and will be automatically notified when the timer is done.
# use a system call to create a waitable timer
timer = CreateWaitableTime()
# use another system call that waits on a waitable object
WaitFor(timer) # this will block the current process until the timer is signaled
# .. sometime in the future, the timer might expire and it's object will be signaled
# causing the WaitFor(timer) call to resume operation
do_other_stuff() # after timer
Edit
The reason for this is that I am going to have another process spawn this processes, therefore it doesn't matter if these get blocked. They need to be able to wait without wasting CPU time.
from threading import Event
evnt = Event()
wait_for = 5 #seconds
evnt.wait(wait_for) # will block for 5 seconds, or unless set early
another thread can call set to early finish the wait above
evnt.set()

MFC Threading Issue - The program doesnt end after all threads are completed

I have created a Single Dialog application which basically does a series of complex calculation. The application was first created as a Win32 console application and later I decided to add a progressbar and then I converted the console application to a Single Dialog based application. The dialog has a progressbar on it. in OnInitDialog() function of the dialog, I start the calculations. The calculations are running on a worker thread. This thread is created by calling _beginthreadex function. The progressbar is updated by the thread by posting messages to the Dialog by using PostMessage. After the thread has completed execution, I call CDialog::OnOK() function to close the dialog. The issue is that, even after the dialog is closed, the application is not end immediately. It takes nearly 2 seconds to close the application even though the dialog is closed.
Any help to solve this issue is highly appreciated.
Thanks.
It's because your worker thread is still running. The application will not terminate until all threads are finished running. Since your UI thread closes before the worker thread, the window may be hidden, but the process does not terminate until the worker thread has completed its work.
The worker thread might be still running. To make sure that the thread is stopped use events to signal . you can signal an event to kill the thread when the user presses close button in the dialog.
You can check whether the event is signaled inside your complex calculation (may be a loop) and break from it. Thus stopping the thread without any issues.
while(true)
{
//Some complex task
DWORD dwWaitResult;
dwWaitResult = WaitForSingleObject(hwndShutdownEvent,0);
if (WAIT_OBJECT_0 == dwWaitResult)
{
break;
}
}

Thread scheduling issue with MFC and AfxBeginThread

I'm creating a worker thread in MFC with AfxBeginThread, but the thread is not getting scheduled. Here's the code:
CWinThread* worker = AfxBeginThread(initialUpdateWorkerThread, this);
DWORD dwExitCode = 0;
while(GetExitCodeThread(worker->m_hThread, &dwExitCode))
{
if(dwExitCode != STILL_ACTIVE)
break;
::Sleep(100);
}
When I run this, this loop just livelocks because initialUpdateWorkerThread is never called (I've put break points and message boxes at the top of it) so dwExitCode is always STILL_ACITVE. But if I put in a call to AfxMessageBox before the loop but after AfxBeginThread then the function is called. This makes me think that somehow I'm not calling the right function to get the thread scheduled, but a call to AfxMessageBox causes it to get scheduled.
How can I force the thread to be scheduled? I would think sleep would do that, but in this case it doesn't seem to.
Your worker thread is probably trying to send your main thread a message, but since you aren't processing messages on on the main thread, the worker thread simply waits. You can confirm this by simply breaking into the debugger to see what the worker thread is doing.

How to suspend a running thread using win32 api's?

Here it is
Create a thread in suspended state.
hThrd1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) ThreadProc1, (LPVOID)
&obj1, CREATE_SUSPENDED, &dwFirstThreadID);
Resume the thread whenever required
ResumeThread(hThrd1);
How do I suspend this running thread. I may resume it after sometime, but I want to suspend it now.
I called
SuspendThread(hThrd1);
Still the for loop in the ThreadProc keeps running. Now how do I avoid it? Also suggest me for any alternatives.
I got the problem. Initially thread HANDLE hThrd1 was declared inside WndProc. Since WndProc is being called again & again, the HANDLE that I got during CreateThread was not the same that was passed to suspend thread. (It was an embarrassing mistake)
Now I have declared it globally. This solves the problem and works as intended.

Starting a sleeping thread in .NET

if a threadA is sleeping, how will another thread threadB call threadA to start ?
Please provide an example if possible.
Instead of sleeping you will want to create an EventWaitHandle and use WaitOne with a timeout.
When you want the thread to wake-up early, you will simply set the event to signaled.
First create the EventWaitHandle:
wakeUpEvent = new EventWaitHandle(false, EventResetMode.ManualReset);
Then in your thread:
wakeUpEvent.WaitOne(new TimeSpan(1, 0, 0));
When the main program wants to wake up the thread early:
wakeUpEvent.Set();
Note: You can either set the event to auto reset or manual reset. Auto reset means once WaitOne returns from the event, it will set it back to non signaled. This is useful if you are in a loop and you signal multiple times.
A thread can be started by waiting on a WaitObject and having the other thread calling the Set method on it. Look at the WaitHandle.WaitOne method.
Here's article that may be of help as well.

Resources