如何避免Windows应用程序挂起/冻结? [英] How to avoid windows application hang/freeze ?

查看:196
本文介绍了如何避免Windows应用程序挂起/冻结?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个Windows应用程序(UI)和一个Windows服务。这个Windows应用程序不断调用Windows服务中的方法,该服务完成其工作UI和服务之间的通信是通过.Net远程实现的。





根据我的应用程序要求,UI应该调用服务不同持续时间的方法(例如每1分钟),此过程应持续3天(可能大于此)。



但UI可以调用服务方法约10-12小时,然后UI挂断。在我第二天上任后,客户端不工作消息框显示Debug和Close按钮。一旦我点击调试,它只显示StackOverflowException,没有其他细节。



然后我开始删除代码中的不必要的对象并处理对象。然后也没用。



请提出宝贵的建议。



2014年11月17日下面的示例代码部分:



为此问题添加我的预感下面,请查看并更新我的猜测是否正确。





  public   void  XXX()
{
// 变量声明 - > 10个变量(包括string,int,bool变量)
尝试
{
// 两个整数变量声明
TreeList selectedTreeList =(TreeList)oTabPage.Controls [ 0 ]; // treelist类型为DevExpress.XtraTreeList.TreeList

foreach (TreeListNode scenarioNode in selectedTreeList.Nodes)
{

// 某些操作
/ * 执行business =>的一些代码行使用范围内使用的新创建的~4-5变量* /
// 一个特殊情况
如果(特殊情况)
{

// c的某些行ode为这个特例做一些操作(包括~2-3变量声明)

XXX();
break ;
}
/ * 一些代码行,还包括对服务的调用* /

Thread.Sleep( 60000 );
}
// 10-15行代码

}

catch (例外情况)
{
// 记录代码

}



}





这是我的怀疑:



在主要方法XXX()中,我们正在调用XXX()。就像我们在另一个XXX()中调用XXX()并且它继续。这个过程是无限的。所以当我们调用XXX()时,

创建新变量并将一些空间分配给它们在RAM中。



注意:在每个XXX()方法中,进入特殊条件需要5分钟,然后调用XXX()。(假设之后) foreach 中的5次迭代它进入特殊状态)



不久,我可以说这整个事情是永远不会结束递归





因此每个XXX()方法的范围永远不会结束.Hence垃圾收集器不会尝试清除内存。所以总RAM将被填充并获得StackOverflowException然后挂起。





请提供您的意见。



谢谢和问候,

Anil

解决方案

请看我对这个问题的评论。所以,非常一般的想法:







祝你好运。

-SA

您正尝试通过UI应用程序定期定期调用Windows服务。你有没有选择Windows应用程序(UI)的原因。为什么不将它转换为控制台应用程序。


Hi,

I have a windows application(UI) and a windows service.This windows application continuously calls methods in the windows service and the service does its work accordingly.The communication between UI and service is achieved by .Net remoting.


As per my application requirement, the UI should call service methods in different durations(for ex. every 1 minute) and this process should continue for 3 days(may be greater than this).

But UI is able to call methods of service for ~10-12 hrs and then UI hangs up. After I coming to office in next morning "client not working" message box is displayed with "Debug" and "Close" buttons .Once I clicked on debug which is just displaying "StackOverflowException" and no other details.

Then I started removing unnecessary objects in my code and disposing objects.Then also no use.

Please give your valuable suggestions.

Added below sample code part on 17-Nov-2014:

Adding my hunch for this problem below, please review and update my guess is correct or not.


public void XXX()
{
  //Variable declaration ->10 variables(consists string,int,bool variables) 
  try
  {
    //two integer variable declaration
     TreeList selectedTreeList = (TreeList)oTabPage.Controls[0]; // treelist type is  DevExpress.XtraTreeList.TreeList 
                  
                  foreach (TreeListNode scenarioNode in selectedTreeList.Nodes)
                  {
                     
                     //Some operations
                        /* Some lines of code to perform business=>which uses newly created ~4-5 variables used in the scope*/
                     //One special case
                     if(special condition)
                     {

                     //Some lines of code to do some operations for this special     case(includes ~2-3 variable declaration)
                      
                       XXX();
                       break;
                     }
                    /* Some lines of code and also includes calls to service */
                    
                   Thread.Sleep(60000);
                   }
     //10-15 lines of code
    
  }

  catch(Exception ex)
  {
   //logging code

  }



}



Here is my suspicion:

In main method XXX(), we are calling XXX(). Like that we call XXX() in another XXX() and it goes on.This process is infinite.So each time when we call XXX(),
new variables are created and some space is allotted to them in RAM.

Note:In each XXX() method, it will take 5 minutes to enter into special condition and then calls XXX().(Assume that after 5 iterations in foreach it enters into special condition)

Shortly, I can say this whole thing as never ending recursion.


So scope of each XXX() method never ends.Hence garbage collector will not try to clear memory.So that total RAM will be filled and get StackOverflowException and then hang.


Please give your inputs.

Thanks and Regards,
Anil

解决方案

Please see my comment to the question. So, very general ideas:



Good luck.
—SA


You are trying to periodically invoke a windows service periodically through a UI appl. Is there a reason why you opted for windows application (UI). Why don't you convert this to a console app.


这篇关于如何避免Windows应用程序挂起/冻结?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆