Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I set number of thread 10, but it works in 3 threads.(I have triple-core processor)
C#
Parallel.ForEach(list,
new ParallelOptions { MaxDegreeOfParallelism = 10 },
                () => Thread.CurrentThread.ManagedThreadId,
                (i, loopState, threadId) =>
                {
                    var cells = i.Split(';');
                    var ip = cells[0];
                    var login = cells[1];
                    var pass = cells[2];
                    try
                    {
                        using (var client = new SshClient(ip, login, pass))
                        {
                            client.Connect();
                            var port = new ForwardedPortLocal("localhost", 10000 + (uint)threadId, "ya.ru", 80);
                            port.Start();
                            client.AddForwardedPort(port);
                            newList.Add(ip);
                            port.Stop();
                            client.Disconnect();
                        }
                    }
                    catch
                    {


                    }

                    return threadId;
                },
                (t

hreadId) => { }
Posted

1 solution

The purpose of MaxDegreeOfParallelism is only to restrict how many threads can be used in maximum. It doesn't meant that the system will use s many threads.

The decision how many threads are used is done by the TaskScheduler[^]. You can either stick with the default scheduler or create your own, but from my point of view the standard scheduler is usually making good decisions.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900