Re: Starting/Stopping a function with Button Control
- From: "Arman Sahakyan" <armancho_x@xxxxxxxxxx(donotspam)>
- Date: Mon, 15 Aug 2005 00:08:05 -0700
Hi,
See below..
"Bill Brehm >" wrote:
> I tried to do something with an extra thread, but ran into a difficulty. My
> loop was not only supposed to stop when a button was pressed, it was also
> supposed to update edit controls and an image on the dialog with the buton
> that started it. Once I was in another thread, I couldn't access the dialog
> directly without getting an ASSERT warning me that I couldn't do that. When
> I tried to use the handle of the dialog to create a new DMyDialog object, it
> was also not updating the actual dialog. How to get around that difficulty?
What you need to do is implement a user-defined message. First, define a
message id like this:
const UINT WM_UPDTAE_MY_DLG = WM_USER + 5;
Second, declare and define a handler for CYourDlg class:
LRESULT CMyDlg::OnUpdateMyDlg(WPARAM w, LPARAM l)
{
// the code to update..
return 0;
}
// And finally, assign the message id with the handler:
ON_MESSAGE(WM_UPDATE_MY_DLG, OnUpdateMyDlg)
Now, your loop, having the dlg's hanlde, can send the message
to the dlg. Assume your thread's controller's PVOID parameter contains
the dlg's handle (HWND):
HWND hDlg = (WHND) pParam; // PVOID lpParam;
::SendMessage(hDlg, WM_UPDATE_MY_DLG, 0, 0);
You are supposed to start your thread from the dlg class like this:
AfxBeginThread(YourThreadController, GetSafeHwnd());
> "Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp> wrote in message
> news:%23aH1UCIoFHA.3568@xxxxxxxxxxxxxxxxxxxxxxx
> > Marcus wrote:
> >> I'd like to be able to start and stop the running of a function on the
> >> action of some button controls... unfortunately, the function I'd like
> >> to start is in a seperate class from the GUI/API class. What would be
> >> the best way to do this? I thought of perhaps creating an array class
> >> that would house a single Boolean value which would be added on the
> >> action of the button controls (True for Start, False for Stop). Then I
> >> would be able to use a simple call from the function for the Boolean
> >> value to know wether to proceed or return. I'm not sure if this is just
> >> a hack and there are better/more efficient ways.
> >>
> >> Any input would be greatly appreciated,
> >> Marcus
> >>
> >
> > When your function is executing the program will ignore button clicks. You
> > only get button clicks after returning to the MFC message dispatcher. In
> > order to continue responding to clicks and other messages your functions
> > have to be brief and then return.
> >
> > To execute a lengthy time-consuming operation without blocking the GUI
> > message processing put the time-consuming operation in another thread.
> > Call AfxBeginThread with a function name. It will begin executing
> > concurrently with the main GUI thread. To start/stop it you can use a
> > bool or SetEvent/WaitForMultipleObjects.
> >
> > --
> > Scott McPhillips [VC++ MVP]
> >
>
>
--
======
Arman
.
- References:
- Starting/Stopping a function with Button Control
- From: Marcus
- Re: Starting/Stopping a function with Button Control
- From: Scott McPhillips [MVP]
- Re: Starting/Stopping a function with Button Control
- From: Bill Brehm
- Starting/Stopping a function with Button Control
- Prev by Date: Re: C++ pure virtual member function problem
- Next by Date: Simulate pressing on a button in a win32 application
- Previous by thread: Re: Starting/Stopping a function with Button Control
- Next by thread: Re: Starting/Stopping a function with Button Control
- Index(es):
Relevant Pages
|