0

I need to run a test on some code for 2 hours. I'm using four async Task methods and in stead of calling all at once each time I would like to call two one time, then one the next time, then all four the next time, and so on, so on, so on, ect..

It does not matter which ones get called I just need to mix up the calls each time and not call all of the methods each time.

I need to know how to set up a timer so after 2 hours is up it will stop and a way to mix up the method calls.

The code I'm looking for would go in the static void Main(string[] args){}

Basically I need some type of loop.

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); }
        }
    }

}

}

2 Answers2

0

You can make a delegate that matches their signatures, then add them to a list of that delegate. Then using Random to determine how many methods and what the indexes of said methods are in your list of that delegate, then execute them, and wait on all of them before proceeding to the next randomly determined group.

Rob G
  • 3,496
  • 1
  • 20
  • 29
0

Here is what I ended up implementing and it worked really well.

    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); }
    }
}