CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Andrew Truckle Guest

    [RESOLVED] Threads

    I want to use a thread to run a conversion process. This needs access the the main windows variables. Do I use AfxGetMainWnd() to do this?

    What is the best way for an application to tell the thread to terminate?


  2. #2
    Join Date
    May 1999
    Location
    Toulouse, France
    Posts
    171

    Re: Threads

    if your thread doesn't need user input/output, one way to do this may be:
    1) define a structure containing
    * the variables you want to share between mainframe and thread
    * an handle to two CEvent, you get it with CreateEvent, I will call them suicide and isKilled (Don't forget to reset them);
    * an handle to a mutex for synchronization

    2) Call AfxBeginThread to launch the worker-thread

    In your tread, have an infinite loop, in which you may check if the event is set; This "suicide-event" is set by the MainFrame; if it is, do what you have to do and set the event isKilled; So the mainframe knows that the thread is dead.
    By the shared structure, the thread has access to the variables of frame
    The mutex is used to protect your datas accessed by both frame and thread. (Use WaitForSingleObject(...))

    HTH.

    K.

    Ash to ash and clay to clay, if the enemy doesn't get you, your own folk may.
    We're talking ****, 'cause life is a 'biz
    You know it is
    Everybody tryin' to get rich
    God ****!
    All I wanna do is live !

    KoRn, Children of the Korn

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