Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update WPFCLI to add keep alive startup param handling #12156

Merged
merged 14 commits into from
Apr 11, 2022
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,14 @@ internal WorkspaceViewModel CurrentSpaceViewModel
{
get
{
return viewModel.Workspaces.FirstOrDefault(vm => vm.Model == dynamoModel.CurrentWorkspace);
if (viewModel == null)
{
return null;
}
else
{
return viewModel.Workspaces.FirstOrDefault(vm => vm.Model == dynamoModel.CurrentWorkspace);
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/DynamoWPFCLI/DynamoWPFCLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
Copy link
Contributor

@QilongTang QilongTang Apr 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why we need to add this? Would this prevent running WPF CLI on linux based system?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was necessary with the threading model required to load the ViewModel which makes sence since that also will not load on linux

</ItemGroup>
<ItemGroup>
<Compile Include="..\AssemblySharedInfoGenerator\AssemblySharedInfo.cs">
Expand Down
87 changes: 69 additions & 18 deletions src/DynamoWPFCLI/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using System;
using System.Threading;
using Dynamo.Applications;
using Dynamo.Controls;
using Dynamo.Models;
using Dynamo.ViewModels;
using Dynamo.Wpf.Extensions;
using Dynamo.Wpf.ViewModels.Watch3D;
using static System.Windows.Threading.Dispatcher;

namespace DynamoWPFCLI
{
Expand All @@ -16,29 +20,26 @@ static internal void Main(string[] args)
{
var cmdLineArgs = StartupUtils.CommandLineArguments.Parse(args);
var locale = StartupUtils.SetLocale(cmdLineArgs);
DynamoModel model;
if (!String.IsNullOrEmpty(cmdLineArgs.ASMPath))

if (cmdLineArgs.KeepAlive)
{
model = Dynamo.Applications.StartupUtils.MakeModel(true, cmdLineArgs.ASMPath);
var thread = new Thread(() => RunKeepAlive(cmdLineArgs));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have a name for this thread so that is easily visible when debugging like the Scheduler thread?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name added


thread.SetApartmentState(ApartmentState.STA);
thread.Start();

Console.WriteLine("Starting DynamoWPFCLI in keepalive mode");
Console.ReadLine();

ShutDown();
}
else
{
model = Dynamo.Applications.StartupUtils.MakeModel(true);
var viewModel = StartupDaynamo(cmdLineArgs);

var runner = new CommandLineRunnerWPF(viewModel);
runner.Run(cmdLineArgs);
}
var viewModel = DynamoViewModel.Start(
new DynamoViewModel.StartConfiguration()
{
DynamoModel = model,
Watch3DViewModel = new DefaultWatch3DViewModel(null, new Watch3DViewModelStartupParams(model))
{
Active = false,
CanBeActivated = false
}
});

var runner = new CommandLineRunnerWPF(viewModel);
runner.Run(cmdLineArgs);

}
catch (Exception e)
{
Expand All @@ -56,5 +57,55 @@ static internal void Main(string[] args)
}
}

private static DynamoViewModel StartupDaynamo(StartupUtils.CommandLineArguments cmdLineArgs)
{
DynamoModel model;
if (!String.IsNullOrEmpty(cmdLineArgs.ASMPath))
{
model = Dynamo.Applications.StartupUtils.MakeModel(true, cmdLineArgs.ASMPath);
}
else
{
model = Dynamo.Applications.StartupUtils.MakeModel(true);
}

model.ShutdownCompleted += (m) => { ShutDown(); };

var viewModel = DynamoViewModel.Start(
new DynamoViewModel.StartConfiguration()
{
DynamoModel = model,
Watch3DViewModel = new DefaultWatch3DViewModel(null, new Watch3DViewModelStartupParams(model))
{
Active = false,
CanBeActivated = false
}
});
return viewModel;
}

private static void RunKeepAlive(StartupUtils.CommandLineArguments cmdLineArgs)
{
try
{
StartupDaynamo(cmdLineArgs);

Console.WriteLine("-----------------------------------------");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now I realize that we probably want to show the console only in debug mode ? But we can deal with this later in other PR.

Console.WriteLine("DynamoWPFCLI is running in keepalive mode");
Console.WriteLine("Press Enter to shutdown...");

Run();
}
catch
{
Console.WriteLine("Server is shutting down due to an error");
}
}

private static void ShutDown()
{
Environment.Exit(0);
}

}
}