Skip to content

Commit

Permalink
Change how casting thread is started.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Aug 26, 2021
1 parent 82e3074 commit 14291e0
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 41 deletions.
14 changes: 6 additions & 8 deletions Desktop.Core/Services/CasterSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,12 @@ private void ApplyConnectionHandlers()
}
}

_ = Task.Run(() => {
ScreenCaster.BeginScreenCasting(new ScreenCastRequest()
{
NotifyUser = notifyUser,
ViewerID = viewerID,
RequesterName = requesterName,
UseWebRtc = useWebRtc
});
ScreenCaster.BeginScreenCasting(new ScreenCastRequest()
{
NotifyUser = notifyUser,
ViewerID = viewerID,
RequesterName = requesterName,
UseWebRtc = useWebRtc
});
}
catch (Exception ex)
Expand Down
10 changes: 1 addition & 9 deletions Desktop.Core/Services/ScreenCaster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public interface IScreenCaster

public class ScreenCaster : IScreenCaster
{
private readonly ConcurrentDictionary<string, Thread> _castingThreads = new();
private readonly Conductor _conductor;
private readonly ICursorIconWatcher _cursorIconWatcher;
private readonly ISessionIndicator _sessionIndicator;
Expand All @@ -45,13 +44,7 @@ public ScreenCaster(Conductor conductor,

public void BeginScreenCasting(ScreenCastRequest screenCastRequest)
{
var castingThread = new Thread(async () =>
{
await CastScreen(screenCastRequest);
});
castingThread.SetApartmentState(ApartmentState.STA);
_castingThreads.AddOrUpdate(screenCastRequest.ViewerID, castingThread, (k, v) => castingThread);
castingThread.Start();
_ = Task.Run(async () => await CastScreen(screenCastRequest));
}

private async Task CastScreen(ScreenCastRequest screenCastRequest)
Expand Down Expand Up @@ -215,7 +208,6 @@ await viewer.SendScreenCapture(new CaptureFrame()
Logger.Write("No more viewers. Calling shutdown service.");
await _shutdownService.Shutdown();
}
_castingThreads.TryRemove(screenCastRequest.ViewerID, out _);
}
}

Expand Down
15 changes: 4 additions & 11 deletions Desktop.Win/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,26 +315,19 @@ private async void CursorIconWatcher_OnChange(object sender, CursorInfo cursor)
}
}

private void ScreenCastRequested(object sender, ScreenCastRequest screenCastRequest)
private async void ScreenCastRequested(object sender, ScreenCastRequest screenCastRequest)
{
App.Current.Dispatcher.Invoke(() =>
await App.Current.Dispatcher.InvokeAsync(async () =>
{
App.Current.MainWindow.Activate();
var result = MessageBox.Show(Application.Current.MainWindow, $"You've received a connection request from {screenCastRequest.RequesterName}. Accept?", "Connection Request", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.Yes)
{
Task.Run(() =>
{
Services.GetRequiredService<IScreenCaster>().BeginScreenCasting(screenCastRequest);
});
Services.GetRequiredService<IScreenCaster>().BeginScreenCasting(screenCastRequest);
}
else
{
// Run on another thread so it doesn't tie up the UI thread.
Task.Run(async () =>
{
await _casterSocket.SendConnectionRequestDenied(screenCastRequest.ViewerID);
});
await _casterSocket.SendConnectionRequestDenied(screenCastRequest.ViewerID);
}
});
}
Expand Down
17 changes: 8 additions & 9 deletions Desktop.XPlat/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ namespace Remotely.Desktop.XPlat
{
public class App : Application
{
private static Conductor Conductor;
private static IServiceProvider Services => ServiceContainer.Instance;

public override void Initialize()
Expand Down Expand Up @@ -96,24 +95,24 @@ private async Task Startup()

BuildServices();

Conductor = Services.GetRequiredService<Conductor>();
var conductor = Services.GetRequiredService<Conductor>();

var args = Environment.GetCommandLineArgs().SkipWhile(x => !x.StartsWith("-"));
Logger.Write("Processing Args: " + string.Join(", ", args));
Conductor.ProcessArgs(args.ToArray());
conductor.ProcessArgs(args.ToArray());

await Services.GetRequiredService<IDeviceInitService>().GetInitParams();

if (Conductor.Mode == Core.Enums.AppMode.Chat)
if (conductor.Mode == Core.Enums.AppMode.Chat)
{
await Services.GetRequiredService<IChatClientService>().StartChat(Conductor.RequesterID, Conductor.OrganizationName);
await Services.GetRequiredService<IChatClientService>().StartChat(conductor.RequesterID, conductor.OrganizationName);
}
else if (Conductor.Mode == Core.Enums.AppMode.Unattended)
else if (conductor.Mode == Core.Enums.AppMode.Unattended)
{
var casterSocket = Services.GetRequiredService<ICasterSocket>();
await casterSocket.Connect(Conductor.Host).ConfigureAwait(false);
await casterSocket.SendDeviceInfo(Conductor.ServiceID, Environment.MachineName, Conductor.DeviceID).ConfigureAwait(false);
await casterSocket.NotifyRequesterUnattendedReady(Conductor.RequesterID).ConfigureAwait(false);
await casterSocket.Connect(conductor.Host).ConfigureAwait(false);
await casterSocket.SendDeviceInfo(conductor.ServiceID, Environment.MachineName, conductor.DeviceID).ConfigureAwait(false);
await casterSocket.NotifyRequesterUnattendedReady(conductor.RequesterID).ConfigureAwait(false);
Services.GetRequiredService<IdleTimer>().Start();
Services.GetRequiredService<IClipboardService>().BeginWatching();
Services.GetRequiredService<IKeyboardMouseInput>().Init();
Expand Down
5 changes: 1 addition & 4 deletions Desktop.XPlat/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,7 @@ private void ScreenCastRequested(object sender, ScreenCastRequest screenCastRequ
var result = await MessageBox.Show($"You've received a connection request from {screenCastRequest.RequesterName}. Accept?", "Connection Request", MessageBoxType.YesNo);
if (result == MessageBoxResult.Yes)
{
_ = Task.Run(() =>
{
Services.GetRequiredService<IScreenCaster>().BeginScreenCasting(screenCastRequest);
});
Services.GetRequiredService<IScreenCaster>().BeginScreenCasting(screenCastRequest);
}
});
}
Expand Down

0 comments on commit 14291e0

Please sign in to comment.