0

我需要对某些代码进行 2 小时的测试。我正在使用四个异步任务方法,而不是每次都调用一次,而是一次调用两个,下一次调用一个,下一次调用所有四个,依此类推,依此类推,等等。。

调用哪些方法并不重要,我只需要每次混合调用而不是每次调用所有方法。

我需要知道如何设置一个计时器,以便在 2 小时后它会停止并且是一种混合方法调用的方法。

我正在寻找的代码将进入static void Main(string[] args){}

基本上我需要某种类型的循环。

class Program
{
    static void Main(string[] args)
    {




    }

    static async Task Test1()
    {
        int[] elevIds = new int[] { 4440, 4441, 4442 };
        IElevation[] elevs = new IElevation[3];

        elevs = await Task.WhenAll(Enumerable.Range(0, elevs.Length).Select(i => AlumCloudPlans.Manager.GetElevationAsync(elevIds[i])));

        using (var pdf = await AlumCloudPlans.Manager.RollUpProjectPDFAsync(new Guid("da91dc34-fa29-4abd-bcc0-d04408310e3e"), elevs, true))
        {
            using (var ms = new MemoryStream()) { pdf.Save(ms, false); }
        }
    }
    static async Task Test2()
    {
        int[] elevIds = new int[] { 4430, 4431, 4434, 4435 };
        IElevation[] elevs = new IElevation[4];

        elevs = await Task.WhenAll(Enumerable.Range(0, elevs.Length).Select(i => AlumCloudPlans.Manager.GetElevationAsync(elevIds[i])));

        using (var pdf = await AlumCloudPlans.Manager.RollUpProjectPDFAsync(new Guid("da91dc34-fa29-4abd-bcc0-d04408310e3e"), elevs, true))
        {
            using (var ms = new MemoryStream()) { pdf.Save(ms, false); }
        }
    }
    static async Task Test3()
    {
        int[] elevIds = new int[] { 4427 };
        IElevation[] elevs = new IElevation[1];

        elevs = await Task.WhenAll(Enumerable.Range(0, elevs.Length).Select(i => AlumCloudPlans.Manager.GetElevationAsync(elevIds[i])));

        using (var pdf = await AlumCloudPlans.Manager.RollUpProjectPDFAsync(new Guid("da91dc34-fa29-4abd-bcc0-d04408310e3e"), elevs, true))
        {
            using (var ms = new MemoryStream()) { pdf.Save(ms, false); }
        }
    }
    static async Task Test4()
    {
        int[] elevIds = new int[] { 2282, 4309 };
        IElevation[] elevs = new IElevation[2];

        elevs = await Task.WhenAll(Enumerable.Range(0, elevs.Length).Select(i => AlumCloudPlans.Manager.GetElevationAsync(elevIds[i])));

        using (var pdf = await AlumCloudPlans.Manager.RollUpProjectPDFAsync(new Guid("da91dc34-fa29-4abd-bcc0-d04408310e3e"), elevs, true))
        {
            using (var ms = new MemoryStream()) { pdf.Save(ms, false); }
        }
    }

}

}

4

2 回答 2

0

您可以制作一个delegate与他们的签名相匹配的,然后将他们添加到该代表的列表中。然后Random用于确定该委托列表中有多少方法和所述方法的索引,然后执行它们,并等待所有这些方法,然后再继续下一个随机确定的组。

于 2013-10-12T04:10:45.833 回答
0

这是我最终实施的,它工作得非常好。

    private async void Form1_Load(object sender, EventArgs e)
{
    await TaskProcessor();
}

async Task TaskProcessor()
{

   DateTime end = DateTime.Now.AddHours(4);
    while (end > DateTime.Now)
    {
        for (byte i = 0; i < 3; i++)
        {
            switch (i)
            {
                case 0:
                    await Test1();
                    break;
                case 1:
                    await Task.WhenAll(Test1(), Test2());
                    break;
                case 2:

                    await Task.WhenAll(Test1(), Test2(), Test3());
                    break;
                case 3:

                    await Task.WhenAll(Test1(), Test2(), Test3());
                    break;
            }
        }
    }
}

static async Task Test1()
{
    int[] elevIds = new int[] { 4440, 4441, 4442 };
    IElevation[] elevs = new IElevation[3];

    elevs = await Task.WhenAll(Enumerable.Range(0, elevs.Length).Select(i => AlumCloudPlans.Manager.GetElevationAsync(elevIds[i])));

    using (var pdf = await AlumCloudPlans.Manager.RollUpProjectImageAsync(new Guid("da91dc34-fa29-4abd-bcc0-d04408310e3e"), elevs, true))
    {
        //using (var ms = new MemoryStream()) { pdf.Save(ms, false); }
    }
}
static async Task Test2()
{
    int[] elevIds = new int[] { 4430, 4431 };
    IElevation[] elevs = new IElevation[2];

    elevs = await Task.WhenAll(Enumerable.Range(0, elevs.Length).Select(i => AlumCloudPlans.Manager.GetElevationAsync(elevIds[i])));

    using (var pdf = await AlumCloudPlans.Manager.RollUpProjectImageAsync(new Guid("da91dc34-fa29-4abd-bcc0-d04408310e3e"), elevs, true))
    {
        // using (var ms = new MemoryStream()) { pdf.Save(ms, false); }
    }
}
static async Task Test3()
{
    int[] elevIds = new int[] { 4427 };
    IElevation[] elevs = new IElevation[1];

    elevs = await Task.WhenAll(Enumerable.Range(0, elevs.Length).Select(i => AlumCloudPlans.Manager.GetElevationAsync(elevIds[i])));

    using (var pdf = await AlumCloudPlans.Manager.RollUpProjectImageAsync(new Guid("da91dc34-fa29-4abd-bcc0-d04408310e3e"), elevs, true))
    {
        // using (var ms = new MemoryStream()) { pdf.Save(ms, false); }
    }
}
于 2013-10-12T16:44:54.883 回答