如何避免Windows应用程序挂起/冻结? [英] How to avoid windows application hang/freeze ?
问题描述
我有一个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
请看我对这个问题的评论。所以,非常一般的想法:
- 为您的应用程序和准确的容错设计开发适当的架构。如果你不知道如何,学习它。学习网络,线程,远程处理/ WFC,异常处理和最佳实践。
- 当然,您可能已经实现了上述部分内容。但你仍然有问题。如果不分析你的问题,你就无法前进。要分析它们,您需要适当的诊断。当然,进行12小时非调试执行的测试可能是完全不可行的。那么该怎么办?我们来讨论它。
- 首先,避免异常处理阻止异常传播。确保所有异常都在堆栈中传播,除了您完全理解的一些特殊情况。 (例如,可以使用阻止异常传播来补偿某些库的缺陷,如果没有开源代码,则无法修补。)
- 完成此操作后,捕获最顶层的所有异常每个线程的框架,并且可能在一些主要的处理循环中。在捕获所有异常的位置,添加代码,使用适当的异常信息编写应用程序日志。
- 此外,将日志记录添加到其他似乎对您可疑的点。这通常在您研究的下一次迭代中完成。
- 应收集哪些异常信息并将其记录到日志中。首先,完整类型名称,异常消息,数据字典,内部异常,递归。请特别注意:
http://msdn.microsoft.com/en-us/library/system.exception%28v=vs.110%29.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system .exception.data(v = vs.110).aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.exception.innerexception(v = vs.110)的.aspx [ ^ ]。
现在我提到了对诊断最关键的最重要部分:异常调用堆栈: http://msdn.microsoft.com/en-us/library/system.exception.stacktrace(v = vs.110).aspx [ ^ ]。- 如何进行日志记录?首先,通过类
System.Diagnostics.EventLog
使用系统日志:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog%28v= vs.110%29.aspx [ ^ ]。
另请参阅我在EventLog上的过去答案
:
如何在文件夹下创建事件日志 [ ^ ],
MsBuild OutPut到Windows应用程序中的TextBox [ ^ ]。
另一种选择是流行的第三方开源产品Apache log4net:
http://en.wikipedia.org/wiki/Log4j#Ports [ ^ ],
http://logging.apache.org/log4net [ ^ ]。- 要获得帮助,请学习提出适当的信息性问题并提供相关信息。现在,你没有线索,它看起来像。不用太担心:它也可以学习。 : - )
祝你好运。
-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:
- Develop appropriate architecture for your application and accurate, fault-tolerant design. If you don't know how, learn it. Learn networking, threading, remoting/WFC, exception handling and best practices.
- Of course, you may have achieved part of the above. But you still have problems. You cannot move forward without analyzing your problems. And to analyze them, you need proper diagnostics. Of course, it might be totally infeasible to do testing worth of 12 hours of non-debugged execution. So, what to do? Let's discuss it.
- First of all, avoid exception handling blocking exception propagation. Make sure all exception propagate up the stack, except some special cases you understand completely. (Blocking exception propagation can be used, for example, to compensate for defects of some libraries without open source code, which you cannot patch.)
- When this is done, catch all the exception on the very top stack frame of each thread, and, possibly, in some main processing loops. In the point where all the exceptions are caught, add code writing application logs with appropriate exception information.
- Also, add logging to some other point which seem to be suspicious to you. This is usually done on some next iterations of your research.
- What exception information should be collected and put to log. First of all, full type name, exception message, data dictionary, inner exceptions, recursively. Please see, in particular:
http://msdn.microsoft.com/en-us/library/system.exception%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.exception.data(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.exception.innerexception(v=vs.110).aspx[^].
And now I'm mentioning most important part most critical for diagnostics: exception call stack: http://msdn.microsoft.com/en-us/library/system.exception.stacktrace(v=vs.110).aspx[^].- How to do the logging? First of all, use the system log via the class
System.Diagnostics.EventLog
:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog%28v=vs.110%29.aspx[^].
See also my past answers onEventLog
:
How to create event log under a folder[^],
MsBuild OutPut to the TextBox on the fly in Windows Application[^].
Another option is the popular 3rd-party open-source product Apache log4net:
http://en.wikipedia.org/wiki/Log4j#Ports[^],
http://logging.apache.org/log4net[^].- To get help, learn to ask appropriate informative question and provide relevant information. Right now, you don't have a clue, it looks like. Not too worry: it can be learned, too. :-)
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屋!