Microsoft.MediaCenter.UI.Timer is not ticking!

Post Reply
.Nico

Posts: 111
Joined: Fri Jun 10, 2011 9:59 am
Location: Maarssen - the Netherlands

HTPC Specs: Show details

Microsoft.MediaCenter.UI.Timer is not ticking!

#1

Post by .Nico » Tue Jun 21, 2011 9:45 pm

Can any one help me with this? Please excuse me if this is the wrong area but I couldn't find a programmers area...

I'm trying to write a background media center application but the timer doesn't work.

Code: Select all

Microsoft.MediaCenter.UI.Timer Scan_Timer = new Microsoft.MediaCenter.UI.Timer();

public void Start()
{
  Scan_Timer.TimeSpanInterval = new TimeSpan(0, 1, 0); 
  Scan_Timer.Tick += new EventHandler(Scannen);
  Scan_Timer.Start();
  if (Scan_Timer.Enabled)
  {
    DialogBox("Scanner on!");
  }
  else
  {
    DialogBox("Scanner off!");
  }
}

void Scannen(object sender, EventArgs e)
{
  DialogBox("Tick!");
}
I do get the "Scanner on!" message but the never the "Tick!". Why not??

kind regards,

.Nico

eddyc

Posts: 5
Joined: Tue Jun 21, 2011 5:08 pm
Location:

HTPC Specs: Show details

#2

Post by eddyc » Wed Jun 22, 2011 11:28 pm

.Nico wrote:I'm trying to write a background media center application but the timer doesn't work.
I have a feeling Media Center UI timers rely on the UI thread to tick, and since background plug-ins don't have a UI thread, the timer never gets going. (Or, possibly, the timer gets going fine, but there is no UI thread to process your tick events on, so they just back up in a big queue somewhere -- possibly you might get a bunch of them as your app exits.)

What are you trying to accomplish with the timer? It might be easier to just spin off a separate thread that runs loops your desired Tick action, with a Thread.Sleep(60 * 1000) in between iterations.

Eddy

.Nico

Posts: 111
Joined: Fri Jun 10, 2011 9:59 am
Location: Maarssen - the Netherlands

HTPC Specs: Show details

#3

Post by .Nico » Thu Jun 23, 2011 9:03 am

eddyc wrote:
.Nico wrote:I'm trying to write a background media center application but the timer doesn't work.
I have a feeling Media Center UI timers rely on the UI thread to tick, and since background plug-ins don't have a UI thread, the timer never gets going. (Or, possibly, the timer gets going fine, but there is no UI thread to process your tick events on, so they just back up in a big queue somewhere -- possibly you might get a bunch of them as your app exits.)

What are you trying to accomplish with the timer? It might be easier to just spin off a separate thread that runs loops your desired Tick action, with a Thread.Sleep(60 * 1000) in between iterations.

Eddy
Eddy,

Thanks for your reply! I want the background application to check Email on a POP3 server every 30 minutes. More details on why I want this: http://www.click2record.nl/eng (just want to change it into a MC add-in)
So with the thread.sleep() it will just wait for a while? It is suppose to do something every 30 minutes. I understand the UI is missing and I wrote the same functions in a foreground application and that works like a charm. However, I want media center to do this in the background...

.Nico

eddyc

Posts: 5
Joined: Tue Jun 21, 2011 5:08 pm
Location:

HTPC Specs: Show details

#4

Post by eddyc » Thu Jun 23, 2011 12:10 pm

.Nico wrote:So with the thread.sleep() it will just wait for a while? It is suppose to do something every 30 minutes.
Yes, Thread.Sleep(x) will pause the current thread for x milliseconds, so Thread.Sleep(30 * 60 * 1000) will wait for 30 minutes. (Remember to "use System.Threading;" to get access to the Thread functions.)

Often, you might like a little more control than that, so you could use a signal with a 30-minute timeout. That way, you can signal your POP3 thread if you want it to do something else, like check immediately or exit.

For example:

Code: Select all

// Class declarations
AutoResetEvent pollEvent = new AutoResetEvent(false);
TimeSpan pollTime = new TimeSpan(0, 30, 0);  // 30 minute interval
Thread pollThread;

// Initialisation code
pollThread = new Thread(new ThreadStart(this.runPollThread));
pollThread.IsBackground = true;
pollThread.Start();

// Thread function
void runPollThread()
{
    while (true)
    {
        CheckForMail();        // Go check mail
        if (pollEvent.WaitOne(polltime))
        {
             // Received an external signal, so do whatever we need to do (e.g. check a state variable to exit, restart poll, reread poll settings, etc)
        }
        else
        {
             // Timed out after 30 minutes - may not need to do anything explicit for this.
        }
    }
}
Then you can say pollEvent.Set(); from your main code if you need to wake up the poll loop before having the full 30 minutes elapse.

Eddy

.Nico

Posts: 111
Joined: Fri Jun 10, 2011 9:59 am
Location: Maarssen - the Netherlands

HTPC Specs: Show details

#5

Post by .Nico » Thu Jun 23, 2011 3:54 pm

eddyc wrote:
.Nico wrote:So with the thread.sleep() it will just wait for a while? It is suppose to do something every 30 minutes.
Yes, Thread.Sleep(x) will pause the current thread for x milliseconds, so Thread.Sleep(30 * 60 * 1000) will wait for 30 minutes. (Remember to "use System.Threading;" to get access to the Thread functions.)

Often, you might like a little more control than that, so you could use a signal with a 30-minute timeout. That way, you can signal your POP3 thread if you want it to do something else, like check immediately or exit.

For example:

Code: Select all

// Class declarations
AutoResetEvent pollEvent = new AutoResetEvent(false);
TimeSpan pollTime = new TimeSpan(0, 30, 0);  // 30 minute interval
Thread pollThread;

// Initialisation code
pollThread = new Thread(new ThreadStart(this.runPollThread));
pollThread.IsBackground = true;
pollThread.Start();

// Thread function
void runPollThread()
{
    while (true)
    {
        CheckForMail();        // Go check mail
        if (pollEvent.WaitOne(polltime))
        {
             // Received an external signal, so do whatever we need to do (e.g. check a state variable to exit, restart poll, reread poll settings, etc)
        }
        else
        {
             // Timed out after 30 minutes - may not need to do anything explicit for this.
        }
    }
}
Then you can say pollEvent.Set(); from your main code if you need to wake up the poll loop before having the full 30 minutes elapse.

Eddy
Thank you very much, I'm going to give it a try ;)

.Nico

Post Reply