的Parallel.For不要用我的主线程 [英] Parallel.For not to use my main thread
问题描述
在我的应用我要做个不使用其他任何东西我的主线程。我必须做一些并行处理,我想用不同的线程来完成。对于我使用的Parallel.For如下:
静态无效的someMethod()
{
控制台。的WriteLine(的String.Format(前并行循环主线程ID - >>>>>>> {0},System.Threading.Thread.CurrentThread.ManagedThreadId));
的Parallel.For(0,10,I =>
{
Console.WriteLine(的String.Format(输出 - >>>>>>> {0},System.Threading.Thread.CurrentThread.ManagedThreadId));
});
Thread.sleep代码(100);
Console.WriteLine(的String.Format(并行循环后,主线程ID - >>>>>>> {0},System.Threading.Thread.CurrentThread.ManagedThreadId)) ;
}
正如你可以从输出主线程使用的ThreadID 1,某些线程看。前并行循环从的Parallel.For也使用相同的线程
主线程ID - >>>>> ;>> 1
输出 - >>>>>>> 1
输出 - >>>>>>> 1
输出 - >>>>>>> 3
输出 - >>>>>>> 4
输出 - >>>>>>> 4
输出 - >>>>>>> 4
输出 - >>>>>>> 4
输出 - >>>>>>> 5
输出 - >>>>>>> 3
输出 - >>>>>>>并行循环后1
主线程ID - >>>>>>> 1
有一些方法,以确保在任何的Parallel.For总是在独立的线程,使运行主线程是免费的。
有一些方法,以确保任何并行。对于总是在独立的线程中运行,以便主线程是免费的。
块引用>
$ b $。 b
的Parallel.For
将永远阻塞,直到一切都完了 - 所以即使它的没有的做原来的线程上的任何东西,线程仍然不会是免费
如果你想保持主线程自由你可能要考虑异步和等待 - 你可以使用
Task.Run
启动10任务异步方法,然后等待
调用的结果Task.WhenAll
。
另外,你可以仍然使用
的Parallel.For
但是在做任务的是的。例如:工作任务= Task.Run(()=>的Parallel.For(0,10,I => ;
{
Console.WriteLine(输出 - >>>>>>> {0},
Thread.CurrentThread.ManagedThreadId);
}));
然后你可以的await的是的任务。任务的主线可能会在
的Parallel.For
循环中使用,不过没关系,因为它仍然不是你原来的主线程,如果你明白我的意思In my application I want my main thread to be not used by anything else. I have to do some parallel processing that I would like to be done by different threads. For that I am using Parallel.For as follows
static void SomeMethod() { Console.WriteLine(string.Format("Main Thread ID before parallel loop ->>>>>>> {0} ", System.Threading.Thread.CurrentThread.ManagedThreadId)); Parallel.For(0, 10, i => { Console.WriteLine(string.Format("Output ->>>>>>> {0} ", System.Threading.Thread.CurrentThread.ManagedThreadId)); }); Thread.Sleep(100); Console.WriteLine(string.Format("Main Thread ID after parallel loop ->>>>>>> {0} ", System.Threading.Thread.CurrentThread.ManagedThreadId)); }
As you can see from the output main thread is using ThreadID 1 and some threads from Parallel.For are also using same thread.
Main Thread ID before parallel loop ->>>>>>> 1 Output ->>>>>>> 1 Output ->>>>>>> 1 Output ->>>>>>> 3 Output ->>>>>>> 4 Output ->>>>>>> 4 Output ->>>>>>> 4 Output ->>>>>>> 4 Output ->>>>>>> 5 Output ->>>>>>> 3 Output ->>>>>>> 1 Main Thread ID after parallel loop ->>>>>>> 1
Is there some way to make sure that anything in Parallel.For always run on separate thread so that main thread is always free.
解决方案Is there some way to make sure that anything in Parallel.For always run on separate thread so that main thread is always free.
Parallel.For
will always block until everything is finished - so even if it didn't do anything on the original thread, the thread still wouldn't be "free".If you want to keep the main thread "free" you might want to look into async and await - you could use
Task.Run
to start the 10 tasks in an async method, and thenawait
the result of callingTask.WhenAll
.Alternatively, you could still use
Parallel.For
but do that in a task. For example:Task task = Task.Run(() => Parallel.For(0, 10, i => { Console.WriteLine("Output ->>>>>>> {0} ", Thread.CurrentThread.ManagedThreadId); }));
You could then await that task. The "main thread" of the task will probably be used in the
Parallel.For
loop, but that's okay because it's still not your original main thread, if you see what I mean.这篇关于的Parallel.For不要用我的主线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!