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
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:
Application.ApplicationExit += new EventHandler(this.OnExit);
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]
WDC WD10EADS 1TB Data, 320GB System, Asus EN9400GT Silent 512MB, Hauppauge HVR 1300,
XP Home SP3, GB-PVR 2.0, ExternalDisplay v0.3[/SIZE]