Skip to content

Commit

Permalink
Merge branch 'fix_open_handles'
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Dec 16, 2020
2 parents 83df162 + b8925ab commit baaf1e2
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 46 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();
}
}
}
5 changes: 2 additions & 3 deletions Desktop.Win/Services/ScreenCapturerWin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ public Bitmap GetNextFrame()
{
_screenCaptureLock.Wait();

Win32Interop.SwitchToInputDesktop();

if (NeedsInit)
{
Logger.Write("Init needed in GetNextFrame.");
Expand Down Expand Up @@ -124,6 +122,8 @@ public Bitmap GetNextFrame()

public void Init()
{
Win32Interop.SwitchToInputDesktop();

CaptureFullscreen = true;
InitBitBlt();
InitDirectX();
Expand Down Expand Up @@ -166,7 +166,6 @@ private Bitmap GetBitBltFrame()
{
try
{
Win32Interop.SwitchToInputDesktop();
var currentFrame = new Bitmap(CurrentScreenBounds.Width, CurrentScreenBounds.Height, PixelFormat.Format32bppArgb);
using (var graphic = Graphics.FromImage(currentFrame))
{
Expand Down
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
12 changes: 7 additions & 5 deletions Shared/Win32/Win32Interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace Remotely.Shared.Win32
{
public class Win32Interop
{
private static IntPtr _lastInputDesktop;

public static List<WindowsSession> GetActiveSessions()
{
var sessions = new List<WindowsSession>();
Expand Down Expand Up @@ -201,6 +203,7 @@ public static void SetMonitorState(MonitorState state)
{
return (MessageBoxResult)MessageBox(owner, message, caption, (long)messageBoxType);
}

public static bool SwitchToInputDesktop()
{
var inputDesktop = OpenInputDesktop();
Expand All @@ -211,16 +214,15 @@ public static bool SwitchToInputDesktop()
return false;
}

return SetThreadDesktop(inputDesktop) && SwitchDesktop(inputDesktop);
var result = SetThreadDesktop(inputDesktop) && SwitchDesktop(inputDesktop);
CloseDesktop(_lastInputDesktop);
_lastInputDesktop = inputDesktop;
return result;
}
catch
{
return false;
}
finally
{
CloseDesktop(inputDesktop);
}
}
}
}
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
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 baaf1e2

Please sign in to comment.