Hello I have the following in code:
await Task.Run(() => SomeLongRunningMethod());
if (dataGridViewExt1.Rows.Count > 0)
{
Log.log("Bringing in items");
BringItemsIntoSystem();
this.Close();
}
else
{
MessageBox.Show("No items to bring in", "Complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
Log.log("No items to bring in.");
this.Close();
}
Here is my BringItemsIntoSystem() function:
private void BringItemsIntoSystem()
{
try
{
for (int q = 0; q < dataGridViewExt1.Rows.Count; q++)
{
Creator.Insert(0);
Creator.SetValueByName(0, "Lookup", dataGridViewExt1.Rows[q].Cells["LOOKUP"].Value.ToString());
Creator.SetValueByName(0, "Qty", dataGridViewExt1.Rows[q].Cells["Qty"].Value.ToString());
Creator.Commit(0);
Log.log("Item added: " + dataGridViewExt1.Rows[q].Cells["LOOKUP"].Value.ToString() + " with QTY: " + dataGridViewExt1.Rows[q].Cells["Qty"].Value.ToString());
}
}
catch(Exception arr)
{
Log.log(arr.ToString());
MessageBox.Show(arr.ToString());
}
}
Here is my SomeLongRUnningMethod():
public void SomeLongRunningMethod()
{
while (ContinueScanning != true)
{
Log.log("ContinueScanning is set to " + ContinueScanning.ToString());
ReturnScannedItems();
}
}
The await function runs and eventually it gets done with said task. Following code the proceeds to execute...and "BringItemsIntoSystem" executes with out a problem fairly quickly and does what it needs.
Now when it gets to this.Close()...it is having an extremely hard time closing the form.
I'm not entirely sure what else to look for since BringItemsIntoSystem runs fine with no issues at all.
Is the await function causing problems? Is there anything I can do to remedy the latency that I'm experiencing getting the form to close?
Looking at the log the BringItemsIntoSystem completes with no problems..as you can see afterwards it should be executing: this.Close();
This doesn't happen instantly this is what the problem is, I'm not sure how else to describe it.
I've created other projects that don't use "await Task.Run..." so I assume it has something to do with this but the BringItemsIntoSystem works fine so dont' know how to prove it.
I have replaced my BringItemsIntoSystem() function with a simple:
MessageBox.Show("Complete");
Again this fires off no problem however.
I then just added a button that when I click it does:
this.Close();
It is almost like the form UI is NOT active...until several seconds later.
Confirmed there is a ~15 second delay before the form becomes active again and I can click on checkboxes.
I hope these new details help somebody help me figure this out.
Thanks for all the help!