-1

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!

losrob
  • 115
  • 1
  • 7
  • 2
    _"it is having an extremely hard time closing the form"_ -- what does that mean, exactly? Code doesn't have the concept of effort. Either it can do something, or it can't. It's not the case that your code can't do something, but then after investing enough effort, all of the sudden can, but that's exactly what "having an extremely hard time" implies. Please fix your question, so that it has a good [mcve] that reliably reproduces the problem. State _clearly_ and _precisely_ what that code does, how that's different from what you want it to do, and what _specifically_ you need help figuring out. – Peter Duniho Oct 29 '17 at 00:03
  • 2
    It would be awesome if you'd invest the time in creating a [mcve] for us. The effort you take in doing that will either make it trivial for us to solve it or you'll figure out the problem along the way. Right now you're just getting a vague guess as the only answer. If you want a proper answer then do the hard work in asking a proper question. – Enigmativity Oct 29 '17 at 02:48

1 Answers1

-1

I would not think the awaited item is preventing the Close from being executed after the items were awaited. I would suggest to comment out each item as a time to see what might be preventing it.

Also, it might just be a normal closing time depending on the resources it's using at the time.

A good reference on an async topic that might be a good read. When correctly use Task.Run and when just async-await

thaDanzar
  • 39
  • 6
  • This is not an answer to the question. An answer to the question would at least attempt to explain the observed behavior (not guess at it) and to provide a _specific_ solution that the question's author can implement. The above is simply pure speculation and recommendation for additional reading, which is content suitable only for a comment, not an answer. – Peter Duniho Oct 29 '17 at 02:58
  • I thought it might be normal at first also but i've noticed a pattern first 2 times it closes within maybe 5 seconds...the third time around it actually hangs for a very long time. – losrob Oct 29 '17 at 14:35
  • @PeterDuniho You are 100% correct, It is more of a comment than any answer. However, I lack the 50 rep points to reply with a comment so I went the route that was available to me. – thaDanzar Oct 30 '17 at 01:33
  • _"I lack the 50 rep points to reply with a comment"_ -- not meeting the requirements to be eligible to post a comment is not in any way an excuse to abuse the Stack Overflow system by posting as an answer something which is not an answer. – Peter Duniho Oct 30 '17 at 02:18