0
votes

I am rarely use task / await to call my function. please have a look at my code example. i am using .net v4.5.2.

private async void button1_Click(object sender, EventArgs e)
{
     bool retvalue = false;
     var periodTimeSpan = TimeSpan.FromSeconds(10);
     var task1 = SimulateJob(); 
     var task2 = Task.Delay(periodTimeSpan);
     await Task.WhenAll(task1, task2);
     retvalue = (bool) task1.Result;
}

private Task<bool> SimulateJob()
{
     bool status = false;

     Task.Delay(10000);
     status = true;

    return Task.FromResult(status);
}

please run the code and understand that SimulateJob() function getting called twice.

in really my SimulateJob() function loads big excel file into spread sheet control. if that function gets called twice then it will be problem for me.

how to change my code in such a way that when this line will execute await Task.WhenAll(task1, task2); then my SimulateJob() function will be called.

in real code SimulateJob() function load multiple big excel file. i saw when this line execute var task1 = SimulateJob(); then my routine load all those big files which is my problem. i want that when this line would execute await Task.WhenAll(task1, task2); then my SimulateJob function should load those big excel file. so tell me what i need to change in my code example.

please help me to handle this situation. thanks

1
it is not called twice - Selvin
What are you trying to achieve by delaying 10 seconds in task 2? Do you want to wait your current thread until you complete your function SimulateJob? - Abhilash Augustine
@AbhilashAugustine yes i want to execute SimulateJob() after that 10 sec will be delay. but i saw SimulateJob() calling twice when i debug code line by line. - user15940620
in real code SimulateJob() function load multiple bug excel file. i saw when this line var task1 = SimulateJob(); then my routine load all those big files which is my problem. i want that when this line would execute ` await Task.WhenAll(task1, task2);` then my SimulateJob function should load those big excel file. so tell me what i need to change. thanks - user15940620
Be clear - neither await nor WhenAll are responsible for starting anything. You give them already running Tasks, and they wait for them to finish. It sounds like you SimulateJob method, despite returning a Task is actually synchronous. - Damien_The_Unbeliever

1 Answers

0
votes
private async void button1_Click(object sender, EventArgs e)
        {
            bool retvalue = await SimulateJob();
            Console.WriteLine(retvalue); // this line of code will execute after excel file loaded
        }

    private async Task<bool> SimulateJob()
    {
        bool status = false;

        await Task.Delay(10000); // call your asynchronus excel loading here
        status = true; // once it's done,  then set status to true

        return status;
    }

this is how you can call an asynchronous function from your button click handler. No need to register unwanted task to just delay for 10 seconds.

Use keyword async on your asynchronous function definition like above.