"task with timeout c#" Code Answer's

You're definitely familiar with the best coding language C# that developers use to develop their projects and they get all their queries like "task with timeout c#" answered properly. Developers are finding an appropriate answer about task with timeout c# related to the C# coding language. By visiting this online portal developers get answers concerning C# codes question like task with timeout c#. Enter your desired code related query in the search bar and get every piece of information about C# code related question on task with timeout c#. 

task with timeout c#

By VKangryVKangry on Jan 05, 2021
/// <summary>/// Run an action and kill it when canceling the token/// </summary>/// <param name="action">The action to execute</param>/// <param name="token">The token</param>/// <param name="waitForGracefulTermination">If set, the task will be killed with delay so as to allow the action to end gracefully</param>private static Task RunCancellable(Action action, CancellationToken token, TimeSpan? waitForGracefulTermination=null){    // we need a thread, because Tasks cannot be forcefully stopped    var thread = new Thread(new ThreadStart(action));    // we hold the reference to the task so we can check its state    Task task = null;    task = Task.Run(() =>    {        // task monitoring the token        Task.Run(() =>        {            // wait for the token to be canceled            token.WaitHandle.WaitOne();            // if we wanted graceful termination we wait            // in this case, action needs to know about the token as well and handle the cancellation itself            if (waitForGracefulTermination != null)            {                Thread.Sleep(waitForGracefulTermination.Value);            }            // if the task has not ended, we kill the thread            if (!task.IsCompleted)            {                thread.Abort();            }        });        // simply start the thread (and the action)        thread.Start();        // and wait for it to end so we return to the current thread        thread.Join();        // throw exception if the token was canceled        // this will not be reached unless the thread completes or is aborted        token.ThrowIfCancellationRequested();    }, token);    return task;}

Source: siderite.dev

Add Comment

0

All those coders who are working on the C# based application and are stuck on task with timeout c# can get a collection of related answers to their query. Programmers need to enter their query on task with timeout c# related to C# code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about task with timeout c# for the programmers working on C# code while coding their module. Coders are also allowed to rectify already present answers of task with timeout c# while working on the C# language code. Developers can add up suggestions if they deem fit any other answer relating to "task with timeout c#". Visit this developer's friendly online web community, CodeProZone, and get your queries like task with timeout c# resolved professionally and stay updated to the latest C# updates. 

C# answers related to "task with timeout c#"

View All C# queries

C# queries related to "task with timeout c#"

Browse Other Code Languages

CodeProZone