NextPVR Forums
  • ______
  • Home
  • New Posts
  • Wiki
  • Members
  • Help
  • Search
  • Register
  • Login
  • Home
  • Wiki
  • Members
  • Help
  • Search
NextPVR Forums Public Developers v
« Previous 1 … 39 40 41 42 43 … 93 Next »
Threads in Plugins won't terminate when PVRX2/GBPVR exits

 
  • 0 Vote(s) - 0 Average
Threads in Plugins won't terminate when PVRX2/GBPVR exits
ralphy
Offline

Senior Member

Posts: 255
Threads: 51
Joined: Nov 2006
#1
2007-09-26, 01:13 PM
My plugin starts a thread, but I cannot stop the thread when PVRX2 of GBPVR exits - same problem as here. Unfortunately, I've tried OldDog's solution
Code:
Application.ApplicationExit += new EventHandler(this.OnExit);
without success.

Debug breakpoints in OnExit() confirm that it is not being hit when PVRX2 exits. What am I doing wrong??

Here's the test code I am using (sorry about the formatting - seems to get altered with the cut and paste).
Code:
using System;
using System.Collections;
using System.Diagnostics;

using System.Threading;
using System.Windows.Forms;

using CommonGBPVRUtilities;
using GBPVR;
using GBPVR.Public;
using GBPVR.Backend.Common;


  
  public class PluginTask: /*IMenuTask , */IEventNotification
  {

    private Thread t;
    private bool stopRequested  = false;


    public   PluginTask()
    {
        if (PluginHelperFactory.getPluginHelper() == null) return;
        Logger.Info("PluginTask .ctr called by plugin");
        Start();
        Application.ApplicationExit += new EventHandler(this.OnExit);
        return;
    }
    
    ~PluginTask()
    {
        // this is never reached on PVRX2 exit
        Logger.Verbose("PluginTask: Destructor");
        stopRequested = true;
    }
        
//----------------------------------------------------------------------
// OnExit - Application exit event handler
//----------------------------------------------------------------------
    private void OnExit(object sender, EventArgs e)
    {
        //// this is never reached on PVRX2 exit either
        Logger.Verbose("PluginTask: Exit event.");
        stopRequested = true;
    //    t.Join();
    }
    
    
    
    
private void Start()
      {
          if (t != null && t.IsAlive) //Already started?
          {
              return;
          }  
            Logger.Info("PluginTask plugin starting...");
          try
          {
              //Start the background thread
              stopRequested = false;
              t = new Thread(new ThreadStart(Run));
              t.Priority = ThreadPriority.Lowest;
         //     t.IsBackground=true; //http://forums.nextpvr.com/showthread.php?t=4891&highlight=thread+destructor
              t.Name = "PluginTask";
              t.Start();
          
          }
          catch (Exception ex)
          {
              Logger.Info("DoStart: Exception while starting plugin: " + ex.Message);
              if (t != null && t.IsAlive)
              {
                  t.Abort();
              }
              t = null;
          }
      }
    
         private void Run()
         {
             try
             {
              while (!stopRequested)
            {
                Logger.Verbose("PluginTask Sleeping...");
                Thread.Sleep(15000);
            }
            
            Logger.Verbose("PluginTask  Stopping Plugin.");

                }
             catch (Exception ex)
            {
               Logger.Verbose(ex.ToString());
            }
        Logger.Verbose("PluginTask Exiting run loop.");
      }
        
   public void Notify(EventTypes eventType, string eventText)
    {    
         Logger.Verbose("PluginTask EVENTS NOTIFIED: eventType= " + eventType.ToString());
                 Logger.Verbose("PluginTask EVENTS NOTIFIED: eventText= '" + eventText +"'");
       }
        
  }
[SIZE="1"]Silverstone GD01S-MXR (three dead rows of pixels in the LCD and defective remote control), Power: Zalman ZM460B-APS (blew up - can't remember what's there now); CPU: Pentium D 3.2 GHz with Asus V72 Cooler; MD: Asus P5LD2 Deluxe 2048MB,
WDC WD10EADS 1TB Data, 320GB System, Asus EN9400GT Silent 512MB, Hauppauge HVR 1300,
XP Home SP3, GB-PVR 2.0, ExternalDisplay v0.3[/SIZE]
sub
Offline

Administrator

NextPVR HQ, New Zealand
Posts: 106,707
Threads: 767
Joined: Nov 2003
#2
2007-09-26, 11:22 PM
Sorry, I dont really have any advice for you on this one - its not something I've ever tried myself.
reven
Offline

Posting Freak

Posts: 5,782
Threads: 396
Joined: Sep 2004
#3
2007-09-27, 04:03 AM
you tried just doing
Code:
Thread thread = new Thread();
thread.IsBackground = true;
that should make the thread die when the main thread finishes.
ralphy
Offline

Senior Member

Posts: 255
Threads: 51
Joined: Nov 2006
#4
2007-09-27, 11:17 AM
reven Wrote:you tried just doing
Code:
Thread thread = new Thread();
thread.IsBackground = true;
that should make the thread die when the main thread finishes.

Reven, I assume you effectively mean uncommenting the commented line "t.IsBackground=true" in the code shown in the original post, and repeated here
Code:
t = new Thread(new ThreadStart(Run));
              t.Priority = ThreadPriority.Lowest;
         //   t.IsBackground=true; //http://forums.nextpvr.com/showthread.php?t=4891&highlight=thread+destructor
              t.Name = "PluginTask";
              t.Start();

If so, then yes - I tried this - and just double checked - it does not solve the problem.


Sub - is it easy and appropriate to have GBPVR/PVRX2 call a "Stop()" method in a plugin, or alternatively, generate a new "stop" IEventNotification? (I was hoping you or someone else would know exactly what I was doing wrong here)

I don't understand why the Application.ApplicationExit worked for OldDog but not for the code in this thread.
[SIZE="1"]Silverstone GD01S-MXR (three dead rows of pixels in the LCD and defective remote control), Power: Zalman ZM460B-APS (blew up - can't remember what's there now); CPU: Pentium D 3.2 GHz with Asus V72 Cooler; MD: Asus P5LD2 Deluxe 2048MB,
WDC WD10EADS 1TB Data, 320GB System, Asus EN9400GT Silent 512MB, Hauppauge HVR 1300,
XP Home SP3, GB-PVR 2.0, ExternalDisplay v0.3[/SIZE]
sub
Offline

Administrator

NextPVR HQ, New Zealand
Posts: 106,707
Threads: 767
Joined: Nov 2003
#5
2007-09-27, 11:21 AM
ralphy Wrote:Sub - is it easy and appropriate to have GBPVR/PVRX2 call a "Stop()" method in a plugin, or alternatively, generate a new "stop" IEventNotification? (I was hoping you or someone else would know exactly what I was doing wrong here)
I might be able to add something for a future release, but its going to be awhile until next release (havnt started yet, been out of the country), so it'd probably be more timely for you to find your own work around for now. Even then there is likely to be some situations where the notification does not happen. For example, the Recording Service can terminate the MVP server process in some situations, which would mean the PVRX2.exe MVP server instance does not get a chance to notify its plugins of this.

What are you trying to do that needs to know the app is exiting?
ralphy
Offline

Senior Member

Posts: 255
Threads: 51
Joined: Nov 2006
#6
2007-09-28, 12:36 PM
sub Wrote:I might be able to add something for a future release, but its going to be awhile until next release (havnt started yet, been out of the country), so it'd probably be more timely for you to find your own work around for now. Even then there is likely to be some situations where the notification does not happen.
That would be great if something could be added in the furture. Perhaps calling a Stop() method, if possible, would be better than an event notification, if there are some situations when the notification does not happen. I see that the logs now log PVRX2 exiting. Perhaps somewhere around that code could be where the Stop() would be called?
sub Wrote:What are you trying to do that needs to know the app is exiting?

The plugin is [yet another?] external display, so no menus are required - it just starts a thread to catch the IEventNotifications, and do other things in the background to PVRX2/GBPVR. As OldDog also originally reported, when GBPVR shuts down, the thread started in the plugin does not terminate. GBPVR will not start up again, because the thread is still running. My plugin also has some diagnostic forms opened by the thread and these don't close when GBPVR exits - so this is the obvious way I can tell if the thread has exited.

I know others have used timers to achieve similar functionality, but I'm trying to avoid this if possible, putting my thread to sleep instead instead of having lots of timers going off at different times.


Another problem I've got is with an XMLSerializer, but that might be the topic of another post if I can't resolve that.
[SIZE="1"]Silverstone GD01S-MXR (three dead rows of pixels in the LCD and defective remote control), Power: Zalman ZM460B-APS (blew up - can't remember what's there now); CPU: Pentium D 3.2 GHz with Asus V72 Cooler; MD: Asus P5LD2 Deluxe 2048MB,
WDC WD10EADS 1TB Data, 320GB System, Asus EN9400GT Silent 512MB, Hauppauge HVR 1300,
XP Home SP3, GB-PVR 2.0, ExternalDisplay v0.3[/SIZE]
sub
Offline

Administrator

NextPVR HQ, New Zealand
Posts: 106,707
Threads: 767
Joined: Nov 2003
#7
2007-09-28, 12:39 PM
I cant change existing interfaces, so I'd have to think about whether I wanted to add another interface or an event. Its going to quite a while though, so you're probably best you just work around this for now. I havnt started development of the next release, so it'll be at least three months away.
« Next Oldest | Next Newest »

Users browsing this thread: 1 Guest(s)



Possibly Related Threads…
Thread Author Replies Views Last Post
  Plugins and NPVR. Where do we start? sub 80 68,881 2020-11-26, 10:02 PM
Last Post: mandai
  TitanTv Remote Schedule For GBPVR UncleJohnsBand 51 33,690 2015-08-20, 05:11 PM
Last Post: sub
  I want to start developing plugins...but how? OrenShapir 6 4,075 2014-11-18, 10:38 PM
Last Post: mvallevand
  Is the input file for pvrx2.exe -import unique to NextPVR? spinnaker 1 1,772 2013-10-08, 02:25 AM
Last Post: sub
  Tuner plugins and client id mvallevand 2 2,076 2013-07-03, 01:39 AM
Last Post: mvallevand
  Tuner Plugins - Output folders mvallevand 2 2,051 2013-02-19, 07:45 PM
Last Post: mvallevand
  .NET 4 plugins? McBainUK 20 7,755 2012-12-11, 08:48 PM
Last Post: sub
  Integrated Development Environment (IDE) for plugins osx-addict 5 2,770 2012-10-18, 08:35 PM
Last Post: osx-addict
  Tuner plugins mvallevand 4 2,450 2012-08-05, 11:19 PM
Last Post: mvallevand
  Recorder plugins - Deleting tuners mvallevand 1 1,520 2012-03-29, 12:51 AM
Last Post: sub

  • View a Printable Version
  • Subscribe to this thread
Forum Jump:

© Designed by D&D, modified by NextPVR - Powered by MyBB

Linear Mode
Threaded Mode