I want an auto logoff feature in my WPF application and have implemented it with hooks. However whenever the mouse is over the application, the performance freezes, degrades and becomes unbearably unresponsive. Once the mouse is off the window performance goes back to normal. If I turn the auto logoff off, performance is fine always, so it’s definitely this causing it. Any idea how to do it different to avoid this?
private void InitializeAutoLogoffFeature()
{
//var windowSpecificOSMessageListener = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
//if (windowSpecificOSMessageListener != null)
// windowSpecificOSMessageListener.AddHook(CallBackMethod);
//AutoLogOffHelper.LogOffTime = _viewModel.logOffTime;
//AutoLogOffHelper.MakeAutoLogOffEvent += AutoLogOffHelper_MakeAutoLogOffEvent;
//AutoLogOffHelper.StartAutoLogoffOption();
AutoLogOffHelper
}
private static IntPtr CallBackMethod(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
try
{
// Listening OS message to test whether it is a user activity
if ((msg >= 0x0200 && msg <= 0x020A) || (msg <= 0x0106 && msg >= 0x00A0) || msg == 0x0021)
{
AutoLogOffHelper.ResetLogoffTimer();
}
else
{
// For debugging purpose
// If this auto logoff does not work for some user activity, you can detect the integer code of that activity using the following line.
//Then All you need to do is adding this integer code to the above if condition.
Debug.WriteLine(msg.ToString());
}
}
catch (Exception ex)
{
MessageHelper.LogError(ex);
}
return IntPtr.Zero;
}
class AutoLogOffHelper
{
static System.Windows.Forms.Timer _timer;
public static int LogOffTime { get; set; }
public delegate void MakeAutoLogOff();
static public event MakeAutoLogOff MakeAutoLogOffEvent;
static public void StartAutoLogoffOption()
{
System.Windows.Interop.ComponentDispatcher.ThreadIdle += DispatcherQueueEmptyHandler;
}
static void _timer_Tick(object sender, EventArgs e)
{
if (_timer == null) return;
System.Windows.Interop.ComponentDispatcher.ThreadIdle -= DispatcherQueueEmptyHandler;
_timer.Stop();
_timer = null;
if (MakeAutoLogOffEvent != null)
{
MakeAutoLogOffEvent();
}
}
static void DispatcherQueueEmptyHandler(object sender, EventArgs e)
{
if (_timer == null)
{
_timer = new System.Windows.Forms.Timer
{
Interval = LogOffTime * 60 * 1000
};
_timer.Tick += _timer_Tick;
_timer.Enabled = true;
}
else if (_timer.Enabled == false)
{
_timer.Enabled = true;
}
}
static public void ResetLogoffTimer()
{
if (_timer == null) return;
_timer.Enabled = false;
_timer.Enabled = true;
}
}
Try taking your
debug.writelineout – It’s very slow and since you might be dealing with a lot of events, it could easily be the problem.Failing that, have you tried using the profiler to see what’s eating up resources?