0

我正在尝试在 Microsoft HPC 上测试一个简单设计的代码片段。看起来这项工作没有完成所有代码。我的测试方法可能是错误的。我只是打印一些检查点。这是代码:

using System;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;

class S
{
    static void Main()
    {
        pcount = Environment.ProcessorCount;
        Console.WriteLine("Proc count = " + pcount);
        ThreadPool.SetMinThreads(4, -1);
        ThreadPool.SetMaxThreads(4, -1);

      //  System.Threading.Thread.Sleep(20000);

        Console.WriteLine("check point 0 ");
        t1 = new Task(A, 1);
        t2 = new Task(A, 2);
        t3 = new Task(A, 3);
        t4 = new Task(A, 4);

        Console.WriteLine("Starting t1 " + t1.Id.ToString());
        t1.Start();
        Console.WriteLine("Starting t2 " + t2.Id.ToString());
        t2.Start();
        Console.WriteLine("Starting t3 " + t3.Id.ToString());
        t3.Start();
        Console.WriteLine("Starting t4 " + t4.Id.ToString());
        t4.Start();

        //  Console.ReadLine();
    }

    static void A(object o)
    {
        Console.WriteLine("check point A ");
        B(o);
    }
    static void B(object o)
    {
        int temp = (int)o;
        Console.WriteLine("check point B ");
        if (temp == 1)
        {
            C(o);
        }
        else
        {
            F(o);
        }
    }
    static void C(object o)
    {
        Console.WriteLine("check point C ");
        D(o);
    }
    static void D(object o)
    {
        int temp = (int)o;
        Console.WriteLine("check point D " + temp);

        if (temp == 2)
        {
            F(o);
        }
        else
        {
            E(o);
        }
    }
    static void E(object o)
    {
        Console.WriteLine("check point E ");

    }
    static void F(object o)
    {
        Console.WriteLine("check point F ");
        G(o);
    }
    static void G(object o)
    {
        Console.WriteLine("check point G ");

    }



    static Task t1, t2, t3, t4;

    static int pcount;
}

在作业输出中,直到最后才打印。打印输出在任意点随机结束(在随机函数中)。好的,我明白了,它可能无法打印所有内容,因为执行速度比打印快(可能的解释)。但是,如果我尝试放置断点并尝试通过附加到进程进行调试,它只会命中代码第一部分中的断点。看起来与测试打印输出的范围相同。我如何确保代码运行到最后,以便可以命中任何点的断点?

4

1 回答 1

1

ATask被设计为在所谓的后台线程上运行。这意味着:如果主线程终止,后台线程也将终止。因此,一旦您的Main()方法完成,所有线程(以及所有任务)都会终止,您的程序也会结束。

解决方案是等待您的任务完成。

static void Main()
{
    pcount = Environment.ProcessorCount;
    Console.WriteLine("Proc count = " + pcount);
    ThreadPool.SetMinThreads(4, -1);
    ThreadPool.SetMaxThreads(4, -1);

  //  System.Threading.Thread.Sleep(20000);

    Console.WriteLine("check point 0 ");
    t1 = new Task(A, 1);
    t2 = new Task(A, 2);
    t3 = new Task(A, 3);
    t4 = new Task(A, 4);

    Console.WriteLine("Starting t1 " + t1.Id.ToString());
    t1.Start();
    Console.WriteLine("Starting t2 " + t2.Id.ToString());
    t2.Start();
    Console.WriteLine("Starting t3 " + t3.Id.ToString());
    t3.Start();
    Console.WriteLine("Starting t4 " + t4.Id.ToString());
    t4.Start();

    //  Console.ReadLine();

    t1.Wait();
    t2.Wait();
    t3.Wait();
    t4.Wait();
}
于 2013-05-23T23:58:12.683 回答