Utbildning
Utbildningsväg
Skapa och hantera bakgrundsjobb och schemalagda jobb i Windows PowerShell - Training
Skapa och hantera bakgrundsjobb och schemalagda jobb i Windows PowerShell
Den här webbläsaren stöds inte längre.
Uppgradera till Microsoft Edge och dra nytta av de senaste funktionerna och säkerhetsuppdateringarna, samt teknisk support.
Important APIs
Learn how to create a work item that runs after a timer elapses.
Use the CreateTimer method to create a timer for the work item. Supply a lambda that accomplishes the work, and use the delay parameter to specify how long the thread pool waits before it can assign the work item to an available thread. The delay is specified using a TimeSpan structure.
Note You can use CoreDispatcher.RunAsync to access the UI and show progress from the work item.
The following example creates a work item that runs in three minutes:
TimeSpan delay = TimeSpan.FromMinutes(3);
ThreadPoolTimer DelayTimer = ThreadPoolTimer.CreateTimer(
(source) =>
{
//
// TODO: Work
//
//
// Update the UI thread by using the UI core dispatcher.
//
Dispatcher.RunAsync(
CoreDispatcherPriority.High,
() =>
{
//
// UI components can be accessed within this scope.
//
});
}, delay);
TimeSpan delay;
delay.Duration = 3 * 60 * 10000000; // 10,000,000 ticks per second
ThreadPoolTimer ^ DelayTimer = ThreadPoolTimer::CreateTimer(
ref new TimerElapsedHandler([this](ThreadPoolTimer^ source)
{
//
// TODO: Work
//
//
// Update the UI thread by using the UI core dispatcher.
//
Dispatcher->RunAsync(CoreDispatcherPriority::High,
ref new DispatchedHandler([this]()
{
//
// UI components can be accessed within this scope.
//
ExampleUIUpdateMethod("Timer completed.");
}));
}), delay);
If needed, handle cancellation and completion of the work item with a TimerDestroyedHandler. Use the CreateTimer overload to supply an additional lambda. This runs when the timer is cancelled or when the work item completes.
The following example creates a timer that submits the work item, and calls a method when the work item finishes or the timer is cancelled:
TimeSpan delay = TimeSpan.FromMinutes(3);
bool completed = false;
ThreadPoolTimer DelayTimer = ThreadPoolTimer.CreateTimer(
(source) =>
{
//
// TODO: Work
//
//
// Update the UI thread by using the UI core dispatcher.
//
Dispatcher.RunAsync(
CoreDispatcherPriority.High,
() =>
{
//
// UI components can be accessed within this scope.
//
});
completed = true;
},
delay,
(source) =>
{
//
// TODO: Handle work cancellation/completion.
//
//
// Update the UI thread by using the UI core dispatcher.
//
Dispatcher.RunAsync(
CoreDispatcherPriority.High,
() =>
{
//
// UI components can be accessed within this scope.
//
if (completed)
{
// Timer completed.
}
else
{
// Timer cancelled.
}
});
});
TimeSpan delay;
delay.Duration = 3 * 60 * 10000000; // 10,000,000 ticks per second
completed = false;
ThreadPoolTimer ^ DelayTimer = ThreadPoolTimer::CreateTimer(
ref new TimerElapsedHandler([&](ThreadPoolTimer ^ source)
{
//
// TODO: Work
//
//
// Update the UI thread by using the UI core dispatcher.
//
Dispatcher->RunAsync(CoreDispatcherPriority::High,
ref new DispatchedHandler([&]()
{
//
// UI components can be accessed within this scope.
//
}));
completed = true;
}),
delay,
ref new TimerDestroyedHandler([&](ThreadPoolTimer ^ source)
{
//
// TODO: Handle work cancellation/completion.
//
Dispatcher->RunAsync(CoreDispatcherPriority::High,
ref new DispatchedHandler([&]()
{
//
// Update the UI thread by using the UI core dispatcher.
//
if (completed)
{
// Timer completed.
}
else
{
// Timer cancelled.
}
}));
}));
If the timer is still counting down, but the work item is no longer needed, call Cancel. The timer is cancelled and the work item won't be submitted to the thread pool.
DelayTimer.Cancel();
DelayTimer->Cancel();
Universal Windows Platform (UWP) apps can't use Thread.Sleep because it can block the UI thread. You can use a ThreadPoolTimer to create a work item instead, and this will delay the task accomplished by the work item without blocking the UI thread.
See the thread pool sample for a complete code sample that demonstrates work items, timer work items, and periodic work items. The code sample was originally written for Windows 8.1 but the code can be re-used in Windows 10.
For information about repeating timers, see Create a periodic work item.
Utbildning
Utbildningsväg
Skapa och hantera bakgrundsjobb och schemalagda jobb i Windows PowerShell - Training
Skapa och hantera bakgrundsjobb och schemalagda jobb i Windows PowerShell
Dokumentation
Create a periodic work item - UWP applications
Learn how to create a work item that repeats periodically using the CreatePeriodicTimer method of the Universal Windows Platform (UWP) ThreadPoolTimer API.
DispatcherQueue - Windows apps
Describes the purpose and function of the Windows App SDK DispatcherQueue class, and how to program with it.
Using Windows Runtime objects in a multithreaded environment - UWP applications
This article discusses the way the .NET Framework handles calls from C# and Visual Basic code to objects that are provided by the Windows Runtime or by Windows Runtime components.
Threading functionality migration - Windows apps
This topic describes how to migrate the threading code in a Universal Windows Platform (UWP) application to the Windows App SDK.