CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2005
    Posts
    3

    DoEvents in MFC?

    my program has function that incorperates an intensive and long loop for testing purposes. i want to know if there is a function i can call, or a way to implement a function, that will check to see if there are any other events waiting in the queue to be handled. that way i can have an option to abort the process if the user so desires.

    thanks,
    D

  2. #2
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    Re: DoEvents in MFC?

    well, yes
    It all depends on how you are running the loop.

    If you are running it in the GUI thread, (which you should not) Then you can use
    GetMessage() and DispatchMessage() to pump the message queue;

    here is an example

    Code:
    void FOO::OnClickStartButton() 
    {
    
    	while (true)
    	{
    		MSG msg;
    		while(PeekMessage(&msg, GetSafeHwnd(), 0, 0, PM_REMOVE))
    		{
    			DispatchMessage(&msg);
    		}
    		if(KillDAQ_)
    		{
    			DAQRunning_ = false;
    			return;
    		}
    		
    		//// LOTS OF PROCESSING DOWN HERE
    	}
    }
    
    void FOO::OnClickStopButton() 
    {
    	KillDAQ_ = true;	
    }
    If you are running the long process in a worker thread then you can use kernal event for synchronization

    Code:
    unsigned __stdcall FOO::WorkerThread( LPVOID pParam )
    {
    
    	std::auto_ptr<FOO_WORKER_DATA> thread_data(static_cast<FOO_WORKER_DATA*>(pParam));
    			
    		
    	while(true)
    	{	
    		if(WAIT_OBJECT_0 == ::WaitForSingleObject(thread_data->MurderThreadEvent_, 0))
    		{
    			return 0;
    		}
    
    		//LOTS OF PROCESSING DOWN HERE
    	}
    				
    	return 0;
    
    }
    Here, MurderThreadEvent_ is an event that can be set in the gui thread or where ever.
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  3. #3
    Join Date
    Apr 2005
    Location
    Mumbai,India
    Posts
    185

    Arrow Re: DoEvents in MFC?

    MSG msg; // can be declared globally, in application source file

    void DoEvents()
    {
    if(::PeekMessage(&msg,NULL,0,0,PM_REMOVE))
    {
    ::TranslateMessage(&msg);
    :ispatchMessage(&msg);
    }
    }

  4. #4
    Join Date
    Apr 2005
    Posts
    3

    Re: DoEvents in MFC?

    Thanks, that worked great!

  5. #5
    Join Date
    Feb 2002
    Posts
    3,788

    Re: DoEvents in MFC?

    Quote Originally Posted by dougrockwell
    Thanks, that worked great!

    i think you should you souldog's second solution (worker thread)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured