Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
230 views
in Technique[技术] by (71.8m points)

c# - Parallel.For not to use my main thread

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 then await the result of calling Task.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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...