简体   繁体   English

C#Windows应用程序未关闭

[英]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 . m在c#中构建一个Win应用程序,然后使用backgroundworkder threadApplication.DoEvents构建该应用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: 并且此循环处于BackGroundWorker_DoWork()事件下,问题无法关闭表单或应用程序,即在taskmanager中我的应用程序仍在运行,我不知道是什么原因,也许是Application.DoEventsBackGroundWorker thread ,我用谷歌搜索了这个问题,但是我没有一个明确的主意..我试图使用以下方法关闭表单:

//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 ? 当在Google上搜索有关此问题的另一件事时,我在Application.DoEvents()发现了很多结果,这是一种错误,我很好奇为什么会这样?

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. 在尝试关闭应用程序时,请设置BackgroundWorker.CancelAsync()方法。 And in you loop check the value of the BackgroundWorker.CancellationPending Property 然后在循环中检查BackgroundWorker.CancellationPending属性的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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