Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Threading Advice/Solutions

Threading Advice/Solutions

Scheduled Pinned Locked Moved C#
csharpcareer
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    paul9038
    wrote on last edited by
    #1

    Hi, Just looking for some advice as I think my current working solution is probably wrong. I've written a small video player in C# using ffmpeg as the decoder and a thread to process and display the footage. However, as I try and further the player's development the thread idea seems more difficult to control. My main window initialises the video footage but plays its output in a seperate thread to a seperate window. To pause the video I suspend and resume the thread, this works fine. To stop the video I abort the thread and kill the seperate window, I can then initialise another video and then play it fine. If, however the video plays to the end I can't restart the player as the previous thread doesn't seem to have terminated properly. Is it possible to control threads where they can be stopped and restarted. Thanks in advance.

    L P 2 Replies Last reply
    0
    • P paul9038

      Hi, Just looking for some advice as I think my current working solution is probably wrong. I've written a small video player in C# using ffmpeg as the decoder and a thread to process and display the footage. However, as I try and further the player's development the thread idea seems more difficult to control. My main window initialises the video footage but plays its output in a seperate thread to a seperate window. To pause the video I suspend and resume the thread, this works fine. To stop the video I abort the thread and kill the seperate window, I can then initialise another video and then play it fine. If, however the video plays to the end I can't restart the player as the previous thread doesn't seem to have terminated properly. Is it possible to control threads where they can be stopped and restarted. Thanks in advance.

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      paul9038 wrote:

      To stop the video I abort the thread and kill the seperate window

      paul9038 wrote:

      Is it possible to control threads where they can be stopped and restarted.

      I imagine so. I have never implemented an approach that aborts threads since virtually all the documentation you read warns against aborting threads. You should use thread communications techniques to control the operations of the thread internally. The result is that the code executing in the thread has an alternate path that causes the thread procedure to return which ends the thread in a proper controlled fashion.

      public static void ThreadProcedure(...)
      {
      while ( IShouldContinuePlayingRecording() )
      PlayRecording();
      }

      So to cause a thread to end looks like this: 1) parent thread communicates with child thread telling it to end 2) child thread receives the message and alters it's flow of control resulting in the thread procedure returning. is that what you were asking?

      1 Reply Last reply
      0
      • P paul9038

        Hi, Just looking for some advice as I think my current working solution is probably wrong. I've written a small video player in C# using ffmpeg as the decoder and a thread to process and display the footage. However, as I try and further the player's development the thread idea seems more difficult to control. My main window initialises the video footage but plays its output in a seperate thread to a seperate window. To pause the video I suspend and resume the thread, this works fine. To stop the video I abort the thread and kill the seperate window, I can then initialise another video and then play it fine. If, however the video plays to the end I can't restart the player as the previous thread doesn't seem to have terminated properly. Is it possible to control threads where they can be stopped and restarted. Thanks in advance.

        P Offline
        P Offline
        Patrick Etc
        wrote on last edited by
        #3

        paul9038 wrote:

        If, however the video plays to the end I can't restart the player as the previous thread doesn't seem to have terminated properly.

        This should provide some clue as to what's going on. First, you should allow the thread to exit if playback completes (I can infer that you're doing this, I'm just making sure). While you could probably manage to keep the thread alive and consume new playback requests, it is probably much easier to simply let the thread exit and create a new thread when you want to start a new playback. With that in mind, if your thread is using any unmanaged resources that it doesn't release when the thread exits, the thread will hang when it attempts to exit. I've run into this on multiple occasions. If your thread doesn't seem to be terminating properly, I would look at the resources you're using in that thread and make sure any objects you use are properly Dispose()'d if their documentation says to do so.


        It has become appallingly obvious that our technology has exceeded our humanity. - Albert Einstein

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups