简体   繁体   中英

C# windows application not closing

my post is same like This ... but that post didn't solve'd my question, so my question is.

m building a win app in c#, and in that im using backgroundworkder thread and Application.DoEvents . here's my code:

for(int i=0;i<gridview.rows.count-1;i++)
{
  if (backThread.CancellationPending)
            {
                e.Cancel = true;
                break;
            }
        string TargetFrame = "";
        byte[] vPost = ASCIIEncoding.ASCII.GetBytes("_PostData");
        string Header = "Content-Type: application/x-www-form-urlencoded" + "\n" + "\r";
        autoWebPage.Navigate("https://xyz.com", TargetFrame, vPost, Header);
        while (car == false)
        {
            System.Threading.Thread.Sleep(500);
            Application.DoEvents();
        }
}

and this loop is under BackGroundWorker_DoWork() event, The Problem is m not able to close the form or application, ie in taskmanager my app is still running, i dn't know what is the cause, maybe Application.DoEvents or BackGroundWorker thread , i google'd about this issue, but i didn't get any clear idea.. i tried to close the form using this:

//Application.Exit();
//mainmdiForm.Dispose();
//mainmdiForm.Close();
//backGroundWorker.CancelAsync();

but no success. And one more thing while searchin' about this issue on google,, i got many results sayin' Application.DoEvents() is an EVIL, m curious why is that so ?

Here is general advice how to solve these issues.

  1. Start your application in debug
  2. Close it
  3. Go to Threads window
  4. Enter threads one by one until yo find the one that blocks app from closing

while trying to close the app set the BackgroundWorker.CancelAsync() method. And in you loop check the value of the BackgroundWorker.CancellationPending Property

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM