Skip to content
This repository has been archived by the owner on Apr 21, 2021. It is now read-only.

Commit

Permalink
remove debug lines
Browse files Browse the repository at this point in the history
  • Loading branch information
justcoding121 committed Apr 13, 2018
1 parent 3274471 commit 3a84c1c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 40 deletions.
61 changes: 33 additions & 28 deletions EventHook/ApplicationWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class ApplicationWatcher

private AsyncCollection<object> appQueue;

private List<WindowData> activeWindows;
private Dictionary<IntPtr, WindowData> activeWindows;
private DateTime prevTimeApp;

public event EventHandler<ApplicationEventArgs> OnApplicationWindowChange;
Expand All @@ -74,7 +74,7 @@ public void Start()
{
if (!isRunning)
{
activeWindows = new List<WindowData> { };
activeWindows = new Dictionary<IntPtr, WindowData>();
prevTimeApp = DateTime.Now;

appQueue = new AsyncCollection<object>();
Expand Down Expand Up @@ -206,7 +206,7 @@ private async Task AppConsumer()
private void WindowCreated(WindowData wnd)
{

activeWindows.Add(wnd);
activeWindows.Add(wnd.HWnd, wnd);
ApplicationStatus(wnd, ApplicationEvents.Launched);

lastEventWasLaunched = true;
Expand All @@ -215,19 +215,19 @@ private void WindowCreated(WindowData wnd)
}

/// <summary>
/// invoke user call back
/// Add window handle to active windows collection
/// </summary>
/// <param name="wnd"></param>
/// <param name="appEvent"></param>
private void ApplicationStatus(WindowData wnd, ApplicationEvents appEvent)
private bool lastEventWasLaunched;
private void WindowActivated(WindowData wnd)
{
var timeStamp = DateTime.Now;

wnd.AppTitle = appEvent == ApplicationEvents.Closed ? wnd.AppTitle : WindowHelper.GetWindowText(wnd.HWnd);
wnd.AppPath = appEvent == ApplicationEvents.Closed ? wnd.AppPath : WindowHelper.GetAppPath(wnd.HWnd);
wnd.AppName = appEvent == ApplicationEvents.Closed ? wnd.AppName : WindowHelper.GetAppDescription(wnd.AppPath);

OnApplicationWindowChange?.Invoke(null, new ApplicationEventArgs() { ApplicationData = wnd, Event = appEvent });
if (activeWindows.ContainsKey(wnd.HWnd))
{
if (!lastEventWasLaunched && lastHwndLaunched != wnd.HWnd)
{
ApplicationStatus(activeWindows[wnd.HWnd], ApplicationEvents.Activated);
}
}
lastEventWasLaunched = false;
}

/// <summary>
Expand All @@ -236,26 +236,31 @@ private void ApplicationStatus(WindowData wnd, ApplicationEvents appEvent)
/// <param name="wnd"></param>
private void WindowDestroyed(WindowData wnd)
{
ApplicationStatus(activeWindows.FirstOrDefault(x => x.HWnd == wnd.HWnd) ?? wnd, ApplicationEvents.Closed);
activeWindows.RemoveAll(x => x.HWnd == wnd.HWnd);
if(activeWindows.ContainsKey(wnd.HWnd))
{
ApplicationStatus(activeWindows[wnd.HWnd], ApplicationEvents.Closed);
activeWindows.Remove(wnd.HWnd);
}

lastEventWasLaunched = false;
}



/// <summary>
/// Add window handle to active windows collection
/// invoke user call back
/// </summary>
private bool lastEventWasLaunched;
private void WindowActivated(WindowData wnd)
/// <param name="wnd"></param>
/// <param name="appEvent"></param>
private void ApplicationStatus(WindowData wnd, ApplicationEvents appEvent)
{
if (activeWindows.Any(x => x.HWnd == wnd.HWnd))
{
if ((!lastEventWasLaunched) && lastHwndLaunched != wnd.HWnd)
{
ApplicationStatus(activeWindows.First(x => x.HWnd == wnd.HWnd), ApplicationEvents.Activated);
}
}
lastEventWasLaunched = false;
}
var timeStamp = DateTime.Now;

wnd.AppTitle = appEvent == ApplicationEvents.Closed ? wnd.AppTitle : WindowHelper.GetWindowText(wnd.HWnd);
wnd.AppPath = appEvent == ApplicationEvents.Closed ? wnd.AppPath : WindowHelper.GetAppPath(wnd.HWnd);
wnd.AppName = appEvent == ApplicationEvents.Closed ? wnd.AppName : WindowHelper.GetAppDescription(wnd.AppPath);

OnApplicationWindowChange?.Invoke(null, new ApplicationEventArgs() { ApplicationData = wnd, Event = appEvent });
}
}
}
15 changes: 3 additions & 12 deletions EventHook/Hooks/ShellHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,15 @@ protected override void WndProc(ref Message m)

break;
case ShellEvents.HSHELL_WINDOWDESTROYED:

if (WindowDestroyed != null)
{
WindowDestroyed(this, m.LParam);
}

WindowDestroyed?.Invoke(this, m.LParam);
break;

case ShellEvents.HSHELL_RUDEAPPACTIVATED:
case ShellEvents.HSHELL_WINDOWACTIVATED:
if (WindowActivated != null)
{
WindowActivated(this, m.LParam);
}
WindowActivated?.Invoke(this, m.LParam);
break;
}
}
Console.WriteLine((ShellEvents)m.WParam);

base.WndProc(ref m);
}

Expand Down

0 comments on commit 3a84c1c

Please sign in to comment.