Cauldron/Cauldron.Macos/ScriptRunner.cs

51 lines
1.1 KiB
C#
Raw Normal View History

2023-07-19 22:09:25 +00:00
using System;
using System.Threading;
using System.Threading.Tasks;
2023-07-21 20:11:56 +00:00
using Cauldron.Core;
2023-07-19 22:09:25 +00:00
2023-07-21 20:11:56 +00:00
namespace Cauldron.Macos;
2023-07-19 22:09:25 +00:00
public static class ScriptRunner
{
public static void RunScript(MainWindow window)
{
window.SetScriptRunState(true);
window.ScriptOutputTextBox.Value = "";
TaskScheduler uiThread = TaskScheduler.FromCurrentSynchronizationContext();
2023-07-21 20:11:56 +00:00
CauldronWriter writer = new(obj =>
2023-07-19 22:09:25 +00:00
{
if (obj is string str)
{
window.BeginInvokeOnMainThread(() =>
window.ScriptOutputTextBox.Value += str + "\n");
}
return Task.CompletedTask;
});
window.ScriptCancellationTokenSource = new CancellationTokenSource();
string script = window.ScriptText;
Task task = Task
.Run(async () =>
{
try
{
await RoslynHost.RunScript(script, Array.Empty<string>(),
new RoslynHostGlobals(writer),
window.ScriptCancellationTokenSource.Token);
}
catch (Exception ex)
{
window.BeginInvokeOnMainThread(() =>
window.ScriptOutputTextBox.Value += ex.ToString());
}
}, window.ScriptCancellationTokenSource.Token)
.ContinueWith((t) => window.SetScriptRunState(false),
uiThread);
}
}