В приложении WPF у нас есть кнопка, которую пользователь может нажать, чтобы вызвать список видео для загрузки в VLC Media Player:
<Button Content="{Binding RotatorButtonLabel}" Command="{Binding RotateVideosCommand}" />
В модели представления MainWindowVm
мы имеемкоманда для обработки нажатия кнопки:
public ICommand RotateVideosCommand => new RelayCommand(RotateVideos);
private void RotateVideos()
{
IsRotatorActive = !IsRotatorActive;
RotatorButtonLabel = IsRotatorActive
? "Stop Rotator"
: "Rotate Videos";
_rotatorVm = new RotatorVm
{
ImageVms = ImagesView.Cast<ImageVm>().ToList(),
IsRotatorActive = IsRotatorActive
};
// This fires off a new thread to run the rotator, otherwise the UI freezes.
Task.Run(() => Messenger.Default.Send(rotatorVm, "LaunchRotator"));
}
Обратите внимание, что в вышеприведенном обработчике команд мы используем Messenger
из набора инструментов MVVM Light Toolkit для указания кода для запуска ротатора.
Теперь в MainWindow.xaml.cs
, у нас есть следующий c'tor:
private CancellationTokenSource _cancellationTokenSource = null;
private CancellationToken _cancellationToken;
public MainWindow()
{
InitializeComponent();
Messenger.Default.Register<RotatorVm>(this, "LaunchRotator", LaunchRotator);
// Other logic...
}
И затем это то, что выше LaunchRotator
называет:
private void LaunchRotator(RotatorVm rotatorVm)
{
if (_cancellationToken.IsCancellationRequested)
{
_cancellationTokenSource.Dispose();
}
if (_cancellationTokenSource == null || _cancellationToken.IsCancellationRequested)
{
_cancellationTokenSource = new CancellationTokenSource();
_cancellationToken = _cancellationTokenSource.Token;
}
if (!rotatorVm.IsRotatorActive)
{
_cancellationTokenSource.Cancel();
return;
}
RotateVideos();
}
private void RotateVideos()
{
while (true)
{
if (_cancellationToken.IsCancellationRequested)
{
return;
}
// This is to simplify the code and simulate work.
Thread.Sleep(5000);
}
}
Если я нажму «Стоп»Кнопка «Вращатель», код может занять несколько секунд, чтобы перейти к следующей итерации цикла while
и прочитать IsCancellationRequested
.Как мне сделать так, чтобы это немедленно прекратилось в этом сценарии?
Я смотрел на этот пример , но он предполагает, что задача и действия находятся в одном классе;здесь у меня есть модель представления и код-позади.Спасибо.