question

TZacks-2728 avatar image
0 Votes"
TZacks-2728 asked AgaveJoe edited

C# calling function by await actually calling my function twice

Here i am sharing a sample code

 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 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







dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AgaveJoe avatar image
0 Votes"
AgaveJoe answered AgaveJoe edited

if you run my code & debug each line then you can see the routine SimulateJob() gets called twice.

My best guess is the button is clicked more than once. I'm not sure what kind of application this is; Web or Windows. If this is a Windows application then disable the click event at the start of the click method body. Reassign the click event at the end.

 public partial class Form1 : Form
 {
     private int count;
     public Form1()
     {
         InitializeComponent();
         count = 0;
     }

     private async void button1_Click(object sender, EventArgs e)
     {
         button1.Click -= button1_Click;

         var task1 = SimulateJob();
         var task2 =  Delay(2);
         await Task.WhenAll(task1, task2);

         label1.Text = $"Count = {(int)task1.Result}";
         button1.Click += button1_Click;
     }

     private async Task Delay(int Seconds)
     {
         await Task .Delay(TimeSpan.FromSeconds(Seconds));
     }
     private async Task<int> SimulateJob()
     {
         await Delay(3);
         return await Task.FromResult(++count);
     }

 }


If this is an ASP.NET application then use JavaScript to disable the button.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
1 Vote"
karenpayneoregon answered TZacks-2728 commented

I see no reason from what is shown the code would be called twice e.g.

 private static async Task<bool> SimulateJob()
 {
     await Task.Delay(10000);
    
     return Environment.UserName == "PayneK";
 }
    
 private async void button1_Click(object sender, EventArgs e)
 {
     var result = await SimulateJob();
     MessageBox.Show(result.ToString());
 }

And a example for WhenAll

 [TestMethod]
 [TestTraits(Trait.TaskWhenAll)]
 public async Task WhenAll()
 {
     Task<List<CustomerItem>> customersTask1 = CustomersOperations.GetCustomersWithProjectionAsync();
     Task<List<CustomerItemSort>> customersTask2 = CustomersOperations.GetCustomersWithProjectionSortAsync();
     await Task.WhenAll(customersTask1, customersTask2);
    
     List<CustomerItem> test1 = customersTask1.Result;
     List<CustomerItemSort> test2 = customersTask2.Result;
    
     Assert.AreEqual(customersTask1.Result.Count, 91);
     Assert.AreEqual(customersTask2.Result.Count, 91);
        
 }
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks Madam for your help & sample code. if you run my code & debug each line then you can see the routine SimulateJob() gets called twice.
my routine SimulateJob() load many big excel files. i have seen routine SimulateJob() load excel files twice which is wrong. please run my code and load / read excel file from SimulateJob() routine.

you shared code approach is different.
i run two task this way await Task.WhenAll(task1, task2);

Thanks




0 Votes 0 ·