task-parallel-library

Which process is best to implement continuous work between thread and task?

放肆的年华 提交于 2020-01-14 07:02:11
问题 I want the below process run continuously.But confused to use Thread or Task. I am also new in implementing Thread or Task.Is the process is right I am implementing? Between Thread and Task which is better for getting fast performance for long running process? private void BtnStart_Click(object sender, EventArgs e) { IClockThread(); } This method is creating thread for every machine. public void IClockThread() { txtStatus.BeginInvoke((Action)(() => txtStatus.Text = "Thread for IClock Starts..

How can code within a Task know that it has timed out?

只谈情不闲聊 提交于 2020-01-14 04:09:15
问题 Consider the following code: public class EventManager { public Task<string> GetResponseAsync(string request) { CancellationTokenSource tokenSource = new CancellationTokenSource(); return new Task<string>( () => { // send the request this.Send(request); // wait for the response until I've been cancelled or I timed out. // this is important because I want to cancel my "wait" if either occur // WHAT CODE CAN I WRITE HERE TO SEE IF THIS TASK HAS TIMED OUT? // (see the example below) // // Note

Async/Await: Unexpected behaviour of ConfigureAwait

无人久伴 提交于 2020-01-13 12:13:07
问题 If you execute the following code in ASP.NET MVC, you can see in Debugging Window that it will correctly restore the thread's culture after await , even if ManagedThreadId changes: public async Task<ActionResult> Index() { Thread.CurrentThread.CurrentUICulture = new CultureInfo("de-DE"); Debug.WriteLine(Thread.CurrentThread.ManagedThreadId); Debug.WriteLine(Thread.CurrentThread.CurrentUICulture); await SomeMethod(); Debug.WriteLine(Thread.CurrentThread.ManagedThreadId); Debug.WriteLine(Thread

Task.ContinueWith() parent task doesn't wait for child task to finish [duplicate]

邮差的信 提交于 2020-01-13 10:12:12
问题 This question already has answers here : Task.WaitAll doesn't wait for child task? (2 answers) Closed 5 years ago . Since I was understanding the Task in context of nested task, I really don't understand that- Why the 3rd print before 2nd print? Even though, I have used Task.WaitAll(t) , it print 3rd line before 2nd line. Code: public static void Main() { Task t = new Task( () => { Thread.Sleep(2000); Console.WriteLine("1st print..."); }); t.ContinueWith( x => { Thread.Sleep(2000); Console

Task.ContinueWith() parent task doesn't wait for child task to finish [duplicate]

隐身守侯 提交于 2020-01-13 10:12:11
问题 This question already has answers here : Task.WaitAll doesn't wait for child task? (2 answers) Closed 5 years ago . Since I was understanding the Task in context of nested task, I really don't understand that- Why the 3rd print before 2nd print? Even though, I have used Task.WaitAll(t) , it print 3rd line before 2nd line. Code: public static void Main() { Task t = new Task( () => { Thread.Sleep(2000); Console.WriteLine("1st print..."); }); t.ContinueWith( x => { Thread.Sleep(2000); Console

TransformBlock never completes

我是研究僧i 提交于 2020-01-13 08:04:50
问题 I'm trying to wrap my head around "completion" in TPL Dataflow blocks. In particular, the TransformBlock doesn't seem to ever complete. Why? Sample program My code calculates the square of all integers from 1 to 1000. I used a BufferBlock and a TransformBlock for that. Later in my code, I await completion of the TransformBlock . The block never actually completes though, and I don't understand why. static void Main(string[] args) { var bufferBlock = new BufferBlock<int>(); var calculatorBlock

Specify task timeout in parallel linq to objects

一笑奈何 提交于 2020-01-13 03:48:24
问题 I have a list of Pictures that I want to process in parallel, but with a timeout. My old code did this by paging through the items and using WaitHandles, but I want to use the new Parallel Linq or Tasks library available in .Net 4. The following snippet is working, how do I add a timeout to it? (Timeout would be for each task executing, not a timeout for all items to be processed) private PictureList FetchPictures(List<Picture> wallResults) { wallResults .AsParallel() .WithDegreeOfParallelism

Safely stop long running task

吃可爱长大的小学妹 提交于 2020-01-12 14:02:48
问题 My question is how can I stop a long running task (.net 4)? I have implemented TPL and tried using the CancellationTokenSource but it doesn’t seem to work for my scenario. All examples I’ve seen assume you’re doing work in a while-loop so that you can check if the task has been cancelled, whereas I just have a single operation that takes long. I cannot wait for the work to be completed as I need to assume it might never complete. Here is the code I have tried: bool? result = null; var cs =

How to empty a BlockingCollection

≡放荡痞女 提交于 2020-01-12 13:34:10
问题 I have a thread adding items to a BlockingCollection . On another thread I am using foreach (var item in myCollection.GetConsumingEnumerable()) If there is a problem I want to break out of my foreach and my method and clear whatever is left in the BlockingCollection however I can't find a way to do it. Any ideas? 回答1: Possibly use the overload of GetConsumingEnumerable that takes a CancellationToken ; then, if anything goes wrong from the producing side, it can cancel the consumer. 回答2: I'm

Deadlock when combining app domain remoting and tasks

泄露秘密 提交于 2020-01-12 04:21:08
问题 My app needs to load plugins into separate app domains and then execute some code inside of them asynchronously. I've written some code to wrap Task in marshallable types: static class RemoteTask { public static async Task<T> ClientComplete<T>(RemoteTask<T> remoteTask, CancellationToken cancellationToken) { T result; using (cancellationToken.Register(remoteTask.Cancel)) { RemoteTaskCompletionSource<T> tcs = new RemoteTaskCompletionSource<T>(); remoteTask.Complete(tcs); result = await tcs.Task