Re: Starting/Stopping a function with Button Control

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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
.



Relevant Pages

  • Re: Starting/Stopping a function with Button Control
    ... >> action of some button controls... ... > When your function is executing the program will ignore button clicks. ... > To execute a lengthy time-consuming operation without blocking the GUI ... > concurrently with the main GUI thread. ...
    (microsoft.public.vc.mfc)
  • Re: Cancel BackgroundWorker
    ... The RunWorkerCompleted event definitely should be raised, but there's nothing in the code you post that proves that you're handling it, never mind that it would affect the loop you posted. ... The most likely explanation is that the loop you posted is executing on the main GUI thread, and of course doing so would cause all sorts of issues, including this one. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: RunCommand acCmdRecordsGoToNext
    ... controls unlocked, for instance. ... Another thing that may come in handy if that doesn't is to loop through the ... increasing intRecordCount on each loop. ... Dim strPassword As String ...
    (microsoft.public.access.forms)
  • Re: RunCommand acCmdRecordsGoToNext
    ... controls unlocked, for instance. ... Another thing that may come in handy if that doesn't is to loop through the ... increasing intRecordCount on each loop. ... Dim strPassword As String ...
    (microsoft.public.access.forms)
  • Re: Re-post: Yet another problem with "no current record"
    ... the "no current record" error when I click on one of my controls when I do, ... The Else block won't exeucte if the If block is executing. ... I have a form with 4 unbound text boxes and a button. ... I set "No Additions" to True in my code, so no empty row shows ...
    (microsoft.public.access.formscoding)