本文整理汇总了C#中System.Threading.CancellationTokenSource.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# System.Threading.CancellationTokenSource.Dispose方法的具体用法?C# System.Threading.CancellationTokenSource.Dispose怎么用?C# System.Threading.CancellationTokenSource.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Threading.CancellationTokenSource
的用法示例。
在下文中一共展示了System.Threading.CancellationTokenSource.Dispose方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Run
public void Run(IBackgroundTaskInstance taskInstance)
{
// Check the task cost
var cost = BackgroundWorkCost.CurrentBackgroundWorkCost;
if (cost == BackgroundWorkCostValue.High)
{
return;
}
// Get the cancel token
var cancel = new System.Threading.CancellationTokenSource();
taskInstance.Canceled += (s, e) =>
{
cancel.Cancel();
cancel.Dispose();
};
// Get deferral
var deferral = taskInstance.GetDeferral();
try
{
// Update Tile with the new xml
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(TileUpdaterTask.w10TileXml);
TileNotification tileNotification = new TileNotification(xmldoc);
TileUpdater tileUpdator = TileUpdateManager.CreateTileUpdaterForApplication();
tileUpdator.Update(tileNotification);
}
finally
{
deferral.Complete();
}
}
示例2: OnTimedEvent
private static void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
{
_logger.Debug("Event handler started");
var tokenSource = new System.Threading.CancellationTokenSource();
try
{
tokenSource.CancelAfter(TimeSpan.FromMilliseconds(_timerInterval));
new System.Threading.Tasks.Task(
() =>
{
CheckProcesses();
tokenSource.Token.ThrowIfCancellationRequested();
return;
},
tokenSource.Token).Wait();
}
catch (AggregateException)
{
_logger.Error("Timespan exceeded checking the health of running processes. Verify WMI is A-OK and things aren't otherwise stacked up on this server.");
}
finally
{
tokenSource.Dispose();
}
timer.Start();
_logger.Debug("Event handler completed");
}
示例3: AutoRun
public bool AutoRun(Slider slider, Slider timeSlider, System.Threading.Tasks.TaskScheduler scheduler)
{
if (autoMode == false)
{
autoMode = true;
cancellationTokenSource = new System.Threading.CancellationTokenSource();
System.Threading.CancellationToken cancellationToken = cancellationTokenSource.Token;
System.Threading.Tasks.Task.Factory.StartNew(() =>
{
while (autoMode)
{
if (cancellationToken.IsCancellationRequested)
{
cancellationTokenSource.Dispose();
break;
}
System.Threading.Tasks.Task.Factory.StartNew(() =>
{
if (slider.Maximum == slider.Value)
{
slider.Value = slider.Minimum;
}
else
{
slider.Value = slider.Value + 1;
}
// ChangeLinkNum((int)slider.Value);
sleepTime = timeSlider.Value;
}, cancellationToken, System.Threading.Tasks.TaskCreationOptions.AttachedToParent, scheduler);
System.Threading.Thread.Sleep((int)(sleepTime * 1000));
}
}
);
return true;
}
else
{
autoMode = false;
cancellationTokenSource.Cancel();
return false;
}
}
示例4: PlayStation
public async void PlayStation(Station s, Windows.UI.Xaml.Controls.MediaElement me, bool navigate = true)
{
if (s == null) return;
if (!NetworkCostController.IsConnectedToInternet) //makes sure Hanasu is connected to the internet.
{
Crystal.Services.ServiceManager.Resolve<Crystal.Services.IMessageBoxService>()
.ShowMessage(
LocalizationManager.GetLocalizedValue("InternetConnectionHeader"),
LocalizationManager.GetLocalizedValue("NoInternetConnectionMsg"));
return;
}
if (NetworkCostController.CurrentNetworkingBehavior == NetworkingBehavior.Opt_In) //if the user is roaming and/or over the data limit, notify them
{
Crystal.Services.ServiceManager.Resolve<Crystal.Services.IMessageBoxService>()
.ShowMessage(
LocalizationManager.GetLocalizedValue("DataConstraintsHeader"),
LocalizationManager.GetLocalizedValue("StreamingDisabled2Msg"));
return;
}
// Reset things things are ready to be played.
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () =>
{
SetMediaElement(ref me);
CurrentStation = s;
CurrentStationSongData = null;
CurrentStationStreamedUri = null;
});
StorageFile albumFile = await GetStationAlbumFromCache(); //Grab the current station's logo from the cache.
if (albumFile != null)
MediaControl.AlbumArt = new Uri("ms-appdata:///local/Hanasu/" + albumFile.DisplayName); //Set the logo in the media control.
MediaControl.ArtistName = CurrentStation.Title; //set the station's name
MediaControl.IsPlaying = true;
try
{
if (me.CurrentState == MediaElementState.Playing || me.CurrentState == MediaElementState.Opening || me.CurrentState == MediaElementState.Buffering)
{
me.Pause();
await Task.Delay(1000);
me.Stop();
}
Uri finalUri = new Uri(s.StreamUrl, UriKind.Absolute);
if (await Hanasu.Core.Preprocessor.PreprocessorService.CheckIfPreprocessingIsNeeded(finalUri, s.PreprocessorFormat))
finalUri = await Hanasu.Core.Preprocessor.PreprocessorService.GetProcessor(finalUri, s.PreprocessorFormat).Process(finalUri);
CurrentStationStreamedUri = finalUri;
//if (CurrentStation.ServerType.ToLower() == "shoutcast")
//{
// var str = await ShoutcastService.GetShoutcastStream(finalUri);
// mediaElement.SetSource(str, str.Content_Type);
// mediaElement.Play();
//}
//else
//{
//finalUri = new Uri(finalUri.ToString() + ";stream.nsv", UriKind.Absolute);
try
{
System.Threading.CancellationTokenSource cts = new System.Threading.CancellationTokenSource();
var openTask = mediaElement.OpenAsync(finalUri, cts.Token);
var timeoutTask = Task.Delay(10000); //Wait for a connection for 10 seconds.
var successful = await Task.WhenAny(openTask, timeoutTask);
if (successful == timeoutTask)
{
//timeout. inform the user and back out.
IsPlaying = false;
Crystal.Services.ServiceManager.Resolve<Crystal.Services.IMessageBoxService>()
.ShowMessage(
LocalizationManager.GetLocalizedValue("StreamingErrorHeader"),
LocalizationManager.GetLocalizedValue("StreamingConnectionTimeoutMsg"));
cts.Cancel();
cts.Dispose();
ResetStationInfo();
return;
}
}
catch (Exception ex)
{
if (ex is TaskCanceledException) return;
//.........这里部分代码省略.........