Dabiel Kabuto
Dabiel Kabuto

Reputation: 2862

Event for connection status changing in Windows 8

Is there an event to handle when connection status change?

For example, my app is like OutLook. I would like to handle an event to know when there is Internet connection and then to send all pending e-mails.

Now I can check periodically if there is Internet connection, but it seems a poor solution to me. I would to solve it using an event.

Upvotes: 0

Views: 137

Answers (1)

Damir Arh
Damir Arh

Reputation: 17855

Your best choice would be implementing a background task. This way you could send the pending e-mails even if your app is not open any more when the internet connection becomes available.

When registering a background task you can set a trigger and a condition to configure when you want your task to run:

var trigger = new SystemTrigger(SystemTriggerType.InternetAvailable, false);
var condition = new SystemCondition(SystemConditionType.InternetAvailable);

var builder = new BackgroundTaskBuilder();
builder.Name = "Send pending e-mails task";
builder.TaskEntryPoint = "Tasks.SendPendingEmailTask";
builder.SetTrigger(trigger);
builder.AddCondition(condition);
var task = builder.Register();

Your background task must implement the IBackgroundTask interface. When the task is triggered, the Run method will be called:

public sealed class SendPendingEmailTask : IBackgroundTask
{
    public void Run(IBackgroundTaskInstance taskInstance)
    {
        var deferral = taskInstance.GetDeferral();
        // send your e-mails here
        deferral.Complete();
    }
}

Upvotes: 1

Related Questions