[英]C# WPF application freezed over time
我已经构建了一个C#WPF应用程序,它有几个后台线程,它们执行以下任务:
一个例子:
Thread thread = new Thread(new ThreadStart(Server));
private static void Server()
{
try
{
IPAddress ipAd = IPAddress.Parse("192.168.1.119");
// use local m/c IP address, and
// use the same in the client
/* Initializes the Listener */
TcpListener myList = new TcpListener(ipAd, 8003);
/* Start Listeneting at the specified port */
myList.Start();
System.Diagnostics.Debug.Write("The server is running at port 8003...");
System.Diagnostics.Debug.Write("The local End point is :" +
myList.LocalEndpoint);
System.Diagnostics.Debug.Write("Waiting for a connection.....");
Socket s = myList.AcceptSocket();
System.Diagnostics.Debug.Write("Connection accepted from " + s.RemoteEndPoint);
byte[] b = new byte[100];
k = s.Receive(b);
Console.WriteLine("Recieved...");
string temp = "";
for (int i = 0; i < k; i++)
{
System.Diagnostics.Debug.Write(Convert.ToChar(b[i]));
temp += Convert.ToChar(b[i]);
}
ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes("The string was recieved by the server."));
System.Diagnostics.Debug.Write("\nSent Acknowledgement");
/* clean up */
s.Close();
myList.Stop();
if (k > 0)
{
cont = true;
k = 0;
}
}
catch (Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
}
}
所以我遇到的问题是偶尔应用程序冻结,我根本无法访问它。 主UI线程也冻结了,我甚至无法从Windows中的任务管理器中杀死程序。 通常应用程序运行完全正常,但长时间使用该应用程序后问题会随机出现(例如4~5小时)
我使用的计算机不是只有2G RAM和AMD双核CPU的高端PC。
我对这个问题的猜测如下:
但是,我仍然不确定如何找出问题所在。 我可能仍然对实现很陌生,所以我很想听听C#WPF,网络和多线程应用程序的一些专家的意见,以帮助我在这里。 谢谢。
调试它的最简单方法是附加调试器(Visual Studio可以执行此操作)。 然后看一下线程在做什么。 要使用VS进行调试,请转到“调试” - >“附加到进程”并选择您的进程。 如果这不起作用,您可以尝试使用任务管理器转储进程(如果您使用的是Windows 7或更高版本),只需右键单击该进程并选择Create Dump file。 然后,您可以使用windbg或VS对文件进行离线分析。 你想要看的最有用的是它被锁定时线程正在做什么。 这些通常是此类问题的最佳第一步。
在Visual Studio中是一个并发可视化工具
我会使用它(我认为它自VS 2010以来可用。这也可以附加到外部进程(如Visual Studio Debugger也可以。
您也可以考虑使用redgate的Performance Profiler等工具
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.