Re: BN_CLICKED Event Handler in Background
You can use a worker thread or do a message loop inside your code that is
blocking the UI thread by calling a function like:
//
// Release main thread for background processing
//
void GiveTime()
{
// Idle until the screen redraws itself, et. al.
MSG msg;
while (::PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) ) {
if (!AfxGetThread()->PumpMessage( )) {
::PostQuitMessage(0);
break;
}
}
// let MFC do its idle processing
LONG lIdle = 0;
while (AfxGetApp()->OnIdle(lIdle++ ))
;
}
Tom
<vv_ramana@yahoo.com> wrote in message
news:1181318100.266288.153180@e65g2000hsc.googlegroups.com...
On Jun 7, 11:01 pm, "Scott McPhillips [MVP]" <org-dot-mvps-at-
scottmcp> wrote:
vv_ram...@yahoo.com wrote:
On Jun 7, 9:05 pm, "Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp>
wrote:
vv_ram...@yahoo.com wrote:
Hello Gurus,
Is there a way to process "OnOK", the BN_CLICKED event handler in
background?
Usually the user opens the dialog and clicks the "OK" button which
will trigger the above event.
I want to "Automate" this and do the same (Trigger the Event) without
user interaction!!
Also, I would like to do the same in a while loop with a sleep in
between.
Is this possible and if so what is the best way? Any example code is
much appreciated.
Thanks in advance.
- Newbie
Is this OK button part of your program, or are you trying to "automate"
an external program?
If it is part of your program why is the operation part of the OnOK
function? It sounds like you could just do the operation in a
background thread or a periodic OnTimer call.
--
Scott McPhillips [MVP VC++]
The "OK" button is the part of the program (MFC Application).
Good Question. This operation could be done in background or using the
dialog depending on the user preference.
Thanks.
So call the operation from OnOK and from wherever the decision is made
to operate in the background.
This is how to do a while loop without blocking the GUI. It uses a
"worker" thread.
// In .h class declaration
volatile BOOL m_bRun;
static UINT MyThreadFunc(PVOID pParam);
// In .cpp
// Start thread
void CMySheet::StartThread()
{
m_bRun = TRUE;
AfxBeginThread(MyThreadFunc, (PVOID)this);
}
UINT CMySheet::MyThreadFunc(PVOID pParam)
{
CMySheet* p = (CMySheet*)pParam;
while (p->m_bRun)
{
.. your operation here
}
return 0;
}
The loop should return when finished, or when m_bRun is set FALSE.
--
Scott McPhillips [MVP VC++]
Thanks Scott.
I am using SetTimer, KillTimer and OnTimer for starting this operation
as suggested by you in the beginning.
So far so good. But, the GUI was blocked when the actual operation is
being done!!
Should I use a worker thread to get around this problem?
Can the "OnTimer" member function call this "CMySheet::StartThread()"?