-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/check if a instance of macro deck is already running and show…
… main window if (#162) * First non-working test * Switched pipe process spawn to STAThread (#161) * Added SynchronizationContext to prevent thread issues in the ui * Added SynchronizationContext to prevent thread issues in the ui * Fixed: tray icon was shown multiple times when Macro Deck was started while running Co-authored-by: RecklessBoon <16234384+RecklessBoon@users.noreply.github.com>
- Loading branch information
1 parent
a4aabfc
commit 1a51870
Showing
5 changed files
with
186 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using Newtonsoft.Json.Linq; | ||
using SuchByte.MacroDeck.Logging; | ||
using SuchByte.MacroDeck.Model; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.IO.Pipes; | ||
using System.Text; | ||
|
||
namespace SuchByte.MacroDeck.Pipes | ||
{ | ||
public class MacroDeckPipeClient | ||
{ | ||
|
||
internal static bool SendShowMainWindowMessage() | ||
{ | ||
return SendPipeMessage("show"); | ||
} | ||
|
||
|
||
internal static bool SendPipeMessage(string message) | ||
{ | ||
var client = new NamedPipeClientStream("macrodeck"); | ||
client.Connect(2000); | ||
if (client.IsConnected) | ||
{ | ||
byte[] buffer = Encoding.ASCII.GetBytes(message); | ||
client.Write(buffer, 0, buffer.Length); | ||
client.Close(); | ||
client.Dispose(); | ||
return true; | ||
} else | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using Newtonsoft.Json.Linq; | ||
using SuchByte.MacroDeck.Logging; | ||
using SuchByte.MacroDeck.Model; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.IO.Pipes; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.RegularExpressions; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace SuchByte.MacroDeck.Pipes | ||
{ | ||
|
||
public delegate void DelegateMessage(string message); | ||
public class MacroDeckPipeServer | ||
{ | ||
public static event DelegateMessage PipeMessage; | ||
|
||
|
||
private static void WaitForConnectionCallBack(IAsyncResult iar) | ||
{ | ||
try | ||
{ | ||
using (var pipeServer = (NamedPipeServerStream)iar.AsyncState) | ||
{ | ||
pipeServer.EndWaitForConnection(iar); | ||
byte[] buffer = new byte[255]; | ||
pipeServer.Read(buffer, 0, 255); | ||
string stringData = Encoding.ASCII.GetString(buffer).Trim('\0'); | ||
Thread t = new Thread(new ThreadStart(() => PipeMessage.Invoke(stringData))); | ||
t.SetApartmentState(ApartmentState.STA); | ||
t.Start(); | ||
pipeServer.Close(); | ||
SpawnServerStream(); | ||
} | ||
} | ||
catch | ||
{ | ||
return; | ||
} | ||
} | ||
|
||
private static void SpawnServerStream() | ||
{ | ||
try | ||
{ | ||
MacroDeckLogger.Trace(typeof(MacroDeckPipeServer), $"Spawning new server stream..."); | ||
NamedPipeServerStream pipeServer = new NamedPipeServerStream("macrodeck", PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous); | ||
pipeServer.BeginWaitForConnection(new AsyncCallback(WaitForConnectionCallBack), pipeServer); | ||
} | ||
catch (Exception ex) | ||
{ | ||
MacroDeckLogger.Error(typeof(MacroDeckPipeServer), $"Failed: {ex.Message}"); | ||
} | ||
} | ||
|
||
public static void Initialize() | ||
{ | ||
MacroDeckLogger.Info(typeof(MacroDeckPipeServer), $"Initializing pipe server"); | ||
SpawnServerStream(); | ||
MacroDeckLogger.Info(typeof(MacroDeckPipeServer), $"Initializing pipe server complete"); | ||
} | ||
} | ||
} |