Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 与N个并发线程并行运行作业_C#_Unit Testing_Task Parallel Library - Fatal编程技术网

C# 与N个并发线程并行运行作业

C# 与N个并发线程并行运行作业,c#,unit-testing,task-parallel-library,C#,Unit Testing,Task Parallel Library,有时我需要一个测试方法由N个线程同时执行(就像ASP.NET代码中发生的情况) 使用任务库有没有一种简单的方法可以做到这一点 [TestMethod()] public void DoSomethingTest() { // Do something which has concurrency issues i.e. database, IO, ... DoSomething(); } // Something like: [TestMethod()] public void

有时我需要一个测试方法由N个线程同时执行(就像ASP.NET代码中发生的情况)

使用
任务库
有没有一种简单的方法可以做到这一点

[TestMethod()]
public void DoSomethingTest()
{
    // Do something which has concurrency issues i.e. database, IO, ...
    DoSomething();
}

// Something like:

[TestMethod()]
public void DoSomethingTest()
{
    int n = 1000; // run 1000 of DoSomething() simultaneously
    Parallel.Invoke(() => DoSomething(), n);
}

是的,我测试了一些与TPL并行的方法

下面是一些示例代码:

var parent = Task.Factory.StartNew(() =>
            {
                var tasksI = Task.Factory.StartNew(() =>
                {
                    for (int i = 0; i < Constants.TaskCount; i++)
                        DoMethod1Parallel();

                });

                var tasksII = Task.Factory.StartNew(() =>
                {
                    for (int i = 0; i < Constants.TaskCount; i++)
                        DoMethod2Parallel()

                });

                tasksI .Wait();
                tasksII.Wait();
            });

            parent.Wait();
            Assert.AreNotEqual(parent.IsFaulted, "Executing is faulted!");
var parent=Task.Factory.StartNew(()=>
{
var tasksI=Task.Factory.StartNew(()=>
{
对于(int i=0;i
{
对于(int i=0;i
我多次调用Method1和Method2,并与TPL并行。所以我可以验证并行执行的能力


关于,patrick是的,我测试了一些与第三方物流平行的方法

下面是一些示例代码:

var parent = Task.Factory.StartNew(() =>
            {
                var tasksI = Task.Factory.StartNew(() =>
                {
                    for (int i = 0; i < Constants.TaskCount; i++)
                        DoMethod1Parallel();

                });

                var tasksII = Task.Factory.StartNew(() =>
                {
                    for (int i = 0; i < Constants.TaskCount; i++)
                        DoMethod2Parallel()

                });

                tasksI .Wait();
                tasksII.Wait();
            });

            parent.Wait();
            Assert.AreNotEqual(parent.IsFaulted, "Executing is faulted!");
var parent=Task.Factory.StartNew(()=>
{
var tasksI=Task.Factory.StartNew(()=>
{
对于(int i=0;i
{
对于(int i=0;i
我多次调用Method1和Method2,并与TPL并行。所以我可以验证并行执行的能力


你好,帕特里克是的,有一个类似的例子。例如:

[TestMethod()]
public void DoSomethingTest()
{
    int n = 10; // changed to 10, see comment
    Parallel.For(0, n, i =>  DoSomething());
    // here all threads are completed 
}
但是请注意,TPL将决定并行性的大小,就像ASP.NET线程池一样


您可以添加ParallelOptions来设置并行度,但我不会

是的,有一个类似的例子。例如:

[TestMethod()]
public void DoSomethingTest()
{
    int n = 10; // changed to 10, see comment
    Parallel.For(0, n, i =>  DoSomething());
    // here all threads are completed 
}
但是请注意,TPL将决定并行性的大小,就像ASP.NET线程池一样


您可以添加ParallelOptions来设置并行度,但我不会

我需要大量的
N
,无法像您那样手动设置。我需要大量的
N
,无法像您那样手动设置。运行1000秒是不现实的。思考
NumberOfCores*F
,其中F是0.5。。2运行其中1000个是不现实的。思考
NumberOfCores*F
,其中F是0.5。。2.