Skip to content
This repository has been archived by the owner on Jan 24, 2022. It is now read-only.

Commit

Permalink
multiple threads
Browse files Browse the repository at this point in the history
add unique threads per bar
  • Loading branch information
Michael Higgins committed May 10, 2020
1 parent 200d76e commit 812afda
Showing 1 changed file with 25 additions and 37 deletions.
62 changes: 25 additions & 37 deletions CenterTaskbar/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public class TrayApplication : ApplicationContext
private static readonly String ExecutablePath = "\"" + Application.ExecutablePath + "\"";

private static bool disposed = false;
volatile bool loopCancelled = false;

private readonly NotifyIcon trayIcon;
private static readonly AutomationElement desktop = AutomationElement.RootElement;
Expand All @@ -62,7 +61,8 @@ public class TrayApplication : ApplicationContext
private readonly List<AutomationElement> bars = new List<AutomationElement>();

private readonly int activeFramerate = DisplaySettings.CurrentRefreshRate();
private Thread positionThread;
// private Thread positionThread;
private Dictionary<AutomationElement, Thread> positionThreads = new Dictionary<AutomationElement, Thread>();

[DllImport("user32.dll", SetLastError = true)]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);
Expand Down Expand Up @@ -161,25 +161,21 @@ void Exit(object sender, EventArgs e)

private void CancelPositionThread()
{
if (positionThread != null)
foreach (Thread thread in positionThreads.Values)
{
//positionThread.Abort();
loopCancelled = true;
positionThread.Join();
thread.Abort();
}
}

void Restart(object sender, EventArgs e)
{
CancelPositionThread();

Start();
}

private void ResetAll()
{
CancelPositionThread();

foreach (AutomationElement trayWnd in bars)
{
Reset(trayWnd);
Expand All @@ -204,17 +200,8 @@ private void Reset(AutomationElement trayWnd)
return;
}

// Removed these lines because they currently do nothing.
//Rect trayBounds = trayWnd.Cached.BoundingRectangle;
//bool horizontal = (trayBounds.Width > trayBounds.Height);

IntPtr tasklistPtr = (IntPtr)tasklist.Current.NativeWindowHandle;

//double listBounds = horizontal ? tasklist.Current.BoundingRectangle.X : tasklist.Current.BoundingRectangle.Y;

//Rect bounds = tasklist.Current.BoundingRectangle;
//int newWidth = (int)bounds.Width;
//int newHeight = (int)bounds.Height;

SetWindowPos(tasklistPtr, IntPtr.Zero, 0, 0, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_ASYNCWINDOWPOS);
}

Expand Down Expand Up @@ -254,48 +241,49 @@ private void Start()

bars.Add(trayWnd);
children.Add(trayWnd, tasklist);
positionThreads[trayWnd] = new Thread(new ParameterizedThreadStart(LoopForPosition));
positionThreads[trayWnd].Start(trayWnd);
}
}

UIAeventHandler = new AutomationEventHandler(OnUIAutomationEvent);
Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent, desktop, TreeScope.Subtree, UIAeventHandler);
Automation.AddAutomationEventHandler(WindowPattern.WindowClosedEvent, desktop, TreeScope.Subtree, UIAeventHandler);

positionThread = new Thread(new ThreadStart(LoopForPosition));
positionThread.Start();
}

private void OnUIAutomationEvent(object src, AutomationEventArgs e)
{
Debug.Print("Event occured: {0}", e.EventId.ProgrammaticName);
if (!positionThread.IsAlive)
foreach (AutomationElement trayWnd in bars)
{
positionThread = new Thread(new ThreadStart(LoopForPosition));
positionThread.Start();
if (!positionThreads[trayWnd].IsAlive)
{
Debug.WriteLine("Starting new thead");
positionThreads[trayWnd] = new Thread(new ParameterizedThreadStart(LoopForPosition));
positionThreads[trayWnd].Start(trayWnd);
} else
{
Debug.WriteLine("Thread already exists");
}
}

}

private void LoopForPosition()
private void LoopForPosition(object trayWndObj)
{
int numberOfLoops = activeFramerate / 10; // Why 5?
AutomationElement trayWnd = (AutomationElement)trayWndObj;
int numberOfLoops = activeFramerate / 10;
int keepGoing = 0;
while (keepGoing < numberOfLoops)
while (keepGoing < numberOfLoops)
{
foreach (AutomationElement trayWnd in bars)
if (!PositionLoop(trayWnd))
{
if (!PositionLoop(trayWnd))
{
keepGoing += 1;
}

System.Threading.Tasks.Task.Delay(1000 / activeFramerate).Wait();
keepGoing += 1;
}

if (loopCancelled) break;
System.Threading.Tasks.Task.Delay(1000 / activeFramerate).Wait();
}

Debug.WriteLine("LoopForPosition Thread ended.");
loopCancelled = false;
}

private bool PositionLoop(AutomationElement trayWnd)
Expand Down

0 comments on commit 812afda

Please sign in to comment.