Skip to content

Commit

Permalink
Prevent ClipboarService from opening unnecessary handles. Clean up Wi…
Browse files Browse the repository at this point in the history
…ndows desktop app startup process.
  • Loading branch information
bitbound committed Dec 16, 2020
1 parent 07517b0 commit 8892f60
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 67 deletions.
2 changes: 1 addition & 1 deletion Desktop.Win/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Remotely.Desktop.Win"
DispatcherUnhandledException="Application_DispatcherUnhandledException" Startup="Application_Startup" Exit="Application_Exit">
DispatcherUnhandledException="Application_DispatcherUnhandledException" Startup="Application_Startup">
<Application.Resources>
<Style x:Key="TitlebarButton" TargetType="Button">
<Setter Property="Background" Value="#FF464646"></Setter>
Expand Down
11 changes: 0 additions & 11 deletions Desktop.Win/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,5 @@ private void Application_Startup(object sender, StartupEventArgs e)
Environment.Exit(0);
}
}

private void Application_Exit(object sender, ExitEventArgs e)
{
Task.Run(async () =>
{
var casterSocket = ServiceContainer.Instance.GetRequiredService<CasterSocket>();
await casterSocket.DisconnectAllViewers();
System.Windows.Forms.Application.Exit();
Environment.Exit(0);
});
}
}
}
21 changes: 9 additions & 12 deletions Desktop.Win/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static async Task Main(string[] args)

if (Conductor.Mode == Core.Enums.AppMode.Chat)
{
StartUiThreads(null);
StartUiThreads(false);
await Task.Run(async () =>
{
var chatService = Services.GetRequiredService<IChatHostService>();
Expand All @@ -67,7 +67,7 @@ public static async Task Main(string[] args)
}
else if (Conductor.Mode == Core.Enums.AppMode.Unattended)
{
StartUiThreads(null);
StartUiThreads(false);
App.Current.Dispatcher.Invoke(() =>
{
App.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
Expand All @@ -76,12 +76,8 @@ public static async Task Main(string[] args)
}
else
{
StartUiThreads(() => new MainWindow());
StartUiThreads(true);
}

TaskHelper.DelayUntil(() => App.Current?.Dispatcher?.HasShutdownStarted != false,
TimeSpan.MaxValue,
1000);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -177,19 +173,20 @@ private static async Task StartScreenCasting()
Services.GetRequiredService<IClipboardService>().BeginWatching();
}

private static void StartUiThreads(Func<Window> createWindowFunc)
private static void StartUiThreads(bool createMainWindow)
{
var wpfUiThread = new Thread(() =>
{
var app = new App();
app.InitializeComponent();
if (createWindowFunc is null)
if (createMainWindow)
{
app.Run();
app.Run(new MainWindow());
}
else
{
app.Run(createWindowFunc());
app.Run();
}
});
wpfUiThread.TrySetApartmentState(ApartmentState.STA);
Expand All @@ -202,7 +199,7 @@ private static void StartUiThreads(Func<Window> createWindowFunc)
winformsThread.TrySetApartmentState(ApartmentState.STA);
winformsThread.Start();


// Wait until WPF app has initialized before moving on.
while (App.Current is null)
{
Thread.Sleep(100);
Expand Down
22 changes: 12 additions & 10 deletions Desktop.Win/Services/ClipboardServiceWin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void BeginWatching()
finally
{
cancelTokenSource = new CancellationTokenSource();
_ = Task.Run(() => WatchClipboard(cancelTokenSource.Token));
WatchClipboard(cancelTokenSource.Token);
}
}

Expand All @@ -51,6 +51,7 @@ public Task SetText(string clipboardText)
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.IsBackground = true;
thread.Start();

return Task.CompletedTask;
Expand All @@ -68,29 +69,30 @@ public void StopWatching()

private void WatchClipboard(CancellationToken cancelToken)
{
while (!cancelToken.IsCancellationRequested &&
!Environment.HasShutdownStarted)
var thread = new Thread(() =>
{
var thread = new Thread(() =>
while (!cancelToken.IsCancellationRequested &&
!Environment.HasShutdownStarted)
{
try
{
Win32Interop.SwitchToInputDesktop();
if (Clipboard.ContainsText() && Clipboard.GetText() != ClipboardText)
{
ClipboardText = Clipboard.GetText();
ClipboardTextChanged?.Invoke(this, ClipboardText);
}
}
catch { }
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
Thread.Sleep(500);
}
Thread.Sleep(500);
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.IsBackground = true;
thread.Start();
}
}
}
6 changes: 5 additions & 1 deletion Desktop.Win/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public MainWindowViewModel()
{
Current = this;

if (Services is null)
{
return;
}

Application.Current.Exit += Application_Exit;

CursorIconWatcher = Services?.GetRequiredService<ICursorIconWatcher>();
Expand Down Expand Up @@ -185,7 +190,6 @@ public async Task GetSessionID()

public async Task Init()
{

SessionID = "Retrieving...";

Host = Config.GetConfig().Host;
Expand Down
7 changes: 6 additions & 1 deletion Desktop.Win/Views/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Remotely.Desktop.Win.ViewModels;
using System;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
Expand Down Expand Up @@ -56,7 +57,11 @@ private void OptionsButton_Click(object sender, RoutedEventArgs e)

private async void Window_Loaded(object sender, RoutedEventArgs e)
{
await ViewModel?.Init();
if (!DesignerProperties.GetIsInDesignMode(this) &&
ViewModel != null)
{
await ViewModel?.Init();
}
}

private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
Expand Down
3 changes: 2 additions & 1 deletion Tests.LoadTester/Tests.LoadTester.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net5.0-windows</TargetFramework>
<AssemblyName>Remotely.Tests.LoadTester</AssemblyName>
<RootNamespace>Remotely.Tests.LoadTester</RootNamespace>
</PropertyGroup>
Expand All @@ -12,6 +12,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Desktop.Win\Desktop.Win.csproj" />
<ProjectReference Include="..\Shared\Shared.csproj" />
</ItemGroup>

Expand Down
29 changes: 0 additions & 29 deletions Tests/ManaulTests.cs

This file was deleted.

3 changes: 2 additions & 1 deletion Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net5.0-windows</TargetFramework>

<IsPackable>false</IsPackable>

Expand All @@ -27,6 +27,7 @@

<ItemGroup>
<ProjectReference Include="..\Desktop.Core\Desktop.Core.csproj" />
<ProjectReference Include="..\Desktop.Win\Desktop.Win.csproj" />
<ProjectReference Include="..\Server\Server.csproj" />
</ItemGroup>

Expand Down

0 comments on commit 8892f60

Please sign in to comment.