Skip to content

Commit 2ffef18

Browse files
committed
stop bgtask when app starts up
1 parent 32383de commit 2ffef18

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

Signal-Windows.Lib/LibUtils.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ await dispatcher.RunAsync(priority, () =>
6161
public class LibUtils
6262
{
6363
private static readonly ILogger Logger = LibsignalLogging.CreateLogger<LibUtils>();
64-
public const string GlobalSemaphoreName = "SignalWindowsPrivateMessenger_Mutex";
64+
public const string GlobalMutexName = "SignalWindowsPrivateMessenger_Mutex";
65+
public const string GlobalEventWaitHandleName = "SignalWindowsPrivateMessenger_EventWaitHandle";
6566
public static string URL = "https://textsecure-service.whispersystems.org";
6667
public static SignalServiceUrl[] ServiceUrls = new SignalServiceUrl[] { new SignalServiceUrl(URL, null) };
6768
public static bool MainPageActive = false;
@@ -74,7 +75,7 @@ public class LibUtils
7475
internal static void Lock()
7576
{
7677
Logger.LogTrace("System lock locking, sync context = {0}", SynchronizationContext.Current);
77-
GlobalLock = new Mutex(false, GlobalSemaphoreName, out bool createdNew);
78+
GlobalLock = new Mutex(false, GlobalMutexName, out bool createdNew);
7879
GlobalLockContext = SynchronizationContext.Current;
7980
try
8081
{
@@ -89,7 +90,7 @@ internal static void Lock()
8990

9091
public static bool Lock(int timeout)
9192
{
92-
GlobalLock = new Mutex(false, GlobalSemaphoreName, out bool createdNew);
93+
GlobalLock = new Mutex(false, GlobalMutexName, out bool createdNew);
9394
GlobalLockContext = SynchronizationContext.Current;
9495
Logger.LogTrace("System lock locking with timeout, sync context = {0}", SynchronizationContext.Current);
9596
bool success = false;
@@ -129,5 +130,23 @@ public static void Unlock()
129130
}
130131
Logger.LogTrace("System lock released");
131132
}
133+
134+
public static EventWaitHandle OpenResetEventSet()
135+
{
136+
Logger.LogTrace("OpenResetEventSet()");
137+
var handle = new EventWaitHandle(true, EventResetMode.ManualReset, GlobalEventWaitHandleName, out bool createdNew);
138+
if(!createdNew)
139+
{
140+
Logger.LogTrace("OpenResetEventSet() setting old event");
141+
handle.Set();
142+
}
143+
return handle;
144+
}
145+
146+
public static EventWaitHandle OpenResetEventUnset()
147+
{
148+
Logger.LogTrace("OpenResetEventUnset()");
149+
return new EventWaitHandle(false, EventResetMode.ManualReset, GlobalEventWaitHandleName, out bool createdNew);
150+
}
132151
}
133152
}

Signal-Windows.Lib/SignalLibHandle.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class SignalLibHandle
4444
private SignalServiceMessageSender MessageSender;
4545
private SignalServiceMessageReceiver MessageReceiver;
4646
public BlockingCollection<SignalMessage> OutgoingQueue = new BlockingCollection<SignalMessage>(new ConcurrentQueue<SignalMessage>());
47+
private EventWaitHandle GlobalResetEvent;
4748

4849
public event EventHandler<SignalMessageEventArgs> SignalMessageEvent;
4950

@@ -99,7 +100,9 @@ public async Task Acquire(CoreDispatcher d, ISignalFrontend w) //TODO wrap tryca
99100
Logger.LogTrace("Acquire() locking");
100101
CancelSource = new CancellationTokenSource();
101102
SemaphoreSlim.Wait(CancelSource.Token);
103+
GlobalResetEvent = LibUtils.OpenResetEventSet();
102104
LibUtils.Lock();
105+
GlobalResetEvent.Reset();
103106
var getConversationsTask = Task.Run(() =>
104107
{
105108
return GetConversations(); // we want to display the conversations asap!
@@ -146,7 +149,9 @@ public async Task Reacquire()
146149
Logger.LogTrace("Reacquire() locking");
147150
CancelSource = new CancellationTokenSource();
148151
SemaphoreSlim.Wait(CancelSource.Token);
152+
GlobalResetEvent = LibUtils.OpenResetEventSet();
149153
LibUtils.Lock();
154+
GlobalResetEvent.Reset();
150155
LibsignalDBContext.ClearSessionCache();
151156
Instance = this;
152157
await Task.Run(() =>

Signal-Windows.RC/SignalBackgroundTask.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public sealed class SignalBackgroundTask : IBackgroundTask
2525
private SignalLibHandle Handle;
2626
private ToastNotifier ToastNotifier;
2727
private AutoResetEvent ResetEvent = new AutoResetEvent(false);
28+
private EventWaitHandle GlobalResetEvent;
2829

2930
public void Run(IBackgroundTaskInstance taskInstance)
3031
{
@@ -42,6 +43,13 @@ public void Run(IBackgroundTaskInstance taskInstance)
4243
Deferral.Complete();
4344
return;
4445
}
46+
GlobalResetEvent = LibUtils.OpenResetEventUnset();
47+
Task.Run(() =>
48+
{
49+
GlobalResetEvent.WaitOne();
50+
Logger.LogInformation("Background task received app startup signal");
51+
ResetEvent.Set();
52+
});
4553
try
4654
{
4755
Handle = new SignalLibHandle(true);

Signal-Windows/App.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ protected override async void OnLaunched(LaunchActivatedEventArgs e)
161161
}
162162

163163

164-
Logger.LogInformation("Launching ({0}) ### 1", e.PreviousExecutionState);
164+
Logger.LogInformation("Launching (PreviousExecutionState={0})", e.PreviousExecutionState);
165165
Logger.LogDebug(LocalCacheFolder.Path);
166166

167167
bool createdMainWindow = await CreateMainWindow(null);

0 commit comments

Comments
 (0)