在呈现UI时长时间运行应用程序后,WPF没有响应 [英] WPF gets not responding after leaving the application for a long time running while rendering the UI
问题描述
在呈现UI时长时间运行应用程序后,WPF没有响应。我需要知道为什么以及如何克服这种情况
WPF gets not responding after leaving the application for a long time running while rendering the UI. I need to know why and how to overcome this scenario
推荐答案
嗨Waleed,
Hi Waleed,
在安全软件开发中,特别是那些需要使用用户名和密码登录服务器的人,经常考虑长期无人操作,程序会自动跳转到用户登录界面。
In the software development for the security, especially those who need to use the username and password to log in to the server, often consider long-term unmanned operation, the program automatically jumps to the user login interface.
判断程序是否长时间无人值守有两个原因,即鼠标长时间不移动且鼠标焦点不在程序很长一段时间,所以这篇文章结合了两种情况,并且很长时间没有操作就实现了实际执行程序的响应
。
There are two reasons for judging whether the program is unmanned for a long time, that is, the mouse does not move for a long time and the mouse focus is not in the program for a long time, so this article combines two situations, and achieves the response of the real implementation program for a long time without operation.
为了防止这个问题,你可以在App.xaml.cs中添加Activated和Deactivated事件,
In order to prevent this issue, you can add Activated and Deactivated event in App.xaml.cs,
public partial class App : Application
{
private DispatcherTimer mousePositionTimer;
private Point mousePosition;
private int checkCount = 0;
private DispatcherTimer deactivatedTimer;
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
mousePosition = GetMousePoint();
mousePositionTimer = new DispatcherTimer();
mousePositionTimer.Tick += new EventHandler(MousePositionTimedEvent);
mousePositionTimer.Interval = new TimeSpan(0, 0, 1);
mousePositionTimer.Start();
deactivatedTimer = new DispatcherTimer();
deactivatedTimer.Tick += new EventHandler(deactivatedTimer_Tick);
deactivatedTimer.Interval = new TimeSpan(0, 0, 10);
}
private void MousePositionTimedEvent(object sender, EventArgs e)
{
if (!HaveUsedTo())
{
checkCount++;
checkCount + 1
if (checkCount == 10)
{
checkCount = 0;
mousePositionTimer.Stop();
System.Windows.Forms.Application.Restart();
Application.Current.Shutdown();
}
}
else
{
checkCount = 0;
}
}
private void deactivatedTimer_Tick(object sender, EventArgs e)
{
deactivatedTimer.Stop();
System.Windows.Forms.Application.Restart();
Application.Current.Shutdown();
}
private void Application_Activated(object sender, EventArgs e)
{
mousePositionTimer.Start();
deactivatedTimer.Stop();
}
private void Application_Deactivated(object sender, EventArgs e)
{
mousePositionTimer.Stop();
deactivatedTimer.Start();
}
private bool HaveUsedTo()
{
Point point = GetMousePoint();
if (point == mousePosition)
{
return false;
}
mousePosition = point;
return true;
}
[StructLayout(LayoutKind.Sequential)]
private struct MPoint
{
public int X;
public int Y;
public MPoint(int x, int y)
{
this.X = x;
this.Y = y;
}
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern bool GetCursorPos(out MPoint mpt);
public Point GetMousePoint()
{
MPoint mpt = new MPoint();
GetCursorPos(out mpt);
Point p = new Point(mpt.X, mpt.Y);
return p;
}
}
最好的问候,
Cherry
这篇关于在呈现UI时长时间运行应用程序后,WPF没有响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!