Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Threads in MFC

Posted on 1999-07-15
6
Medium Priority
?
301 Views
Last Modified: 2010-04-02
I got a little MFC Application that has a window, some buttons. It executs a non userinterface task that might take some time. How do I asure that the user can press cancel when the task runs. Is CWinThread the answer ??
Either the task or the userinterface could run in it's
own thread.
I need a little codesample that shows me how to do it !!
0
Comment
Question by:Jeb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 340 total points
ID: 1200145
>>How do I asure that the user can press cancel when the
>>task runs

Pressing 'cancel' is always possible, the question is how to signal the thread to terminate - using CWinThread requires to use a message loop, so you could do this passing a message to the thread. As a loop is mandantory for this, you could also use the following approach:

(in your app:)

// create notification event
HANDLE hEvent = CrteateEvent ( NULL, FALSE, FALSE, NULL);

// Create the thread
hThread = CreateThread ( NULL, 0, ProcessingThread, (LPVOID) hEvent, ...);

// if cancel is pressed: Set event, so the thread will terminate
  SetEvent( hEvent);


(in the thread:)

ULONG WINAPI StartThread ( LPVOID pv)
{
 HANDLE hEvent = ( HANDLE) pv;

 for ( ;;)
 {
   if ( WAIT_OBJECT_0 == WaitForSingleObject( hEvent, 1))
      {
        // event is signaled, do cleanup

        ExitThread ( 0);
      }

   // do processing...
 }
}

Feel free to ask if you need more information!
0
 

Author Comment

by:Jeb
ID: 1200146
I got a Progressbar that should tell the user how many records out of known number of records have been written to a Database.
The user can cancel the process half way through !!

I think I need a thread to do so ??
0
 
LVL 86

Expert Comment

by:jkr
ID: 1200147
>>I think I need a thread to do so ??

Yes, this would make things more convenient - anothe method would be to use a message pumpt in the thread's processing loop to receive the cancel event:

MSG msg;

   if (GetMessage(&msg,0,0,0))
     {
       TranslateMessage(&msg);
       DispatchMessage(&msg);
     };

This will ensure that all messages are dispatched correctly during the execution of the loop

Be sure to also check out

http://www.codeguru.com/internet/urlfile.shtml (sample for doing sth. similar)

and go to http://www.microsoft.com/msj to download the source code for Microsoft Systems Journal February 1997 'Wicked Code'

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Jeb
ID: 1200148
I got a Progressbar that should tell the user how many records out of known number of records have been written to a Database.
The user can cancel the process half way through !!

I think I need a thread to do so ??
0
 
LVL 86

Expert Comment

by:jkr
ID: 1200149
See my previous comment...
0
 

Author Comment

by:Jeb
ID: 1200150
I got a Progressbar that should tell the user how many records out of known number of records have been written to a Database.
The user can cancel the process half way through !!

I think I need a thread to do so ??
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

661 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question