Terminate a thread
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Terminate a thread

  1. #1
    Join Date
    Dec 2007
    Location
    France
    Posts
    268

    Terminate a thread

    Hi,

    I have a thread with a while(1) loop in it. When the user push the stop button I would like that thread to end.

    I thought about creating a bool and checking its value periodically in the thread and when I push the stop
    button I change the value of the bool for that the thread breaks out of the loop and finishes.

    Is it the best way to go about it, that's how professional programmers do it also?

  2. #2
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,100

    Re: Terminate a thread

    if you are attempting to paint the overlay yourself via a timer or update loop: this cannot be done with a picture control. that would only work if the picture control gives you an actual indication as to when the image was updated (which it doesn't).

    If the painting stops, I'm going to assume you're not releasing some GDI resources, and after a while you're exhausing the GDI resources for your application. Creating another font, pen, brush... simply fails because no more can be made. THis is a resource leak and you'll need to fix it.



    The only way I know of that this is possible with a picture control is to use a tranparent layered window approach.
    Have a look at the WS_EX_LAYERED style on MSDN. This is used in combination with UpdateLayeredWindow().


    Do note that depending on your videocard and the OS used, this could have a serious impact on CPU usage and/or framerate.

  3. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,100

    Re: Terminate a thread

    Alternatively, use a more advanced control to playback video which does have a means to allow you to paint overlays.

  4. #4
    Join Date
    Dec 2007
    Location
    France
    Posts
    268

    Re: Terminate a thread

    Thank you OReubens, fortunately there is no painting involved only updating of a listbox.

    So can I assume that my method is the right one for what Im doing?

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,070

    Re: Terminate a thread

    Your method is acceptable under condition that the boolean variable access is thread safe. Though I always prefer to communicate with threads by means of signalling objects, events for example.
    Best regards,
    Igor

  6. #6
    Join Date
    Dec 2007
    Location
    France
    Posts
    268

    Re: Terminate a thread

    Thank you Igor, that's what I wanted to know.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center