Skip to content

Commit

Permalink
add execution mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Huajiang Wei committed Feb 15, 2020
1 parent 623ac4b commit 28ba0ce
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 5 deletions.
5 changes: 2 additions & 3 deletions DemoScriptEditor/LogStatement.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using ScratchNet;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -66,9 +67,7 @@ public ExecutionEnvironment StartCall(ExecutionEnvironment e)

public Completion EndCall(ExecutionEnvironment e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine(DateTime.Now + ":" + DateTime.Now.Millisecond + " " + logValue);
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(DateTime.Now + ":" + DateTime.Now.Millisecond + " " + logValue);
if (WriteLine != null)
{
WriteLine(logValue);
Expand Down
2 changes: 1 addition & 1 deletion DemoScriptEditor/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
</ControlTemplate>
</Button.Template>
</Button>
<Button Margin="5" Grid.Column="3" Click="OnStopRun" Name="ButtonStop" IsEnabled="False">
<Button Margin="5" Grid.Column="3" Click="OnStopRun" Name="ButtonStop" IsEnabled="False" Visibility="Collapsed">
<Button.Template>
<ControlTemplate>
<fa:FontAwesome Name="Icon" FontSize="25" Foreground="DarkRed" Icon="Stop"/>
Expand Down
9 changes: 8 additions & 1 deletion DemoScriptEditor/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
Expand Down Expand Up @@ -125,9 +126,10 @@ private void OnStartRun(object sender, RoutedEventArgs e)
engine = null;
}
engine = new ExecutionEngine();
engine.Mode = ExecutionMode.ExitWhenFinish;
engine.AddInstance(new Instance(Editor.Script));
engine.Start();
engine.RunFunction(func);
engine.Start();
Console.WriteLine("Start run");
return;
}
Expand All @@ -143,6 +145,11 @@ private void OnStopRun(object sender, RoutedEventArgs e)
engine = null;
}
}
protected override void OnClosing(CancelEventArgs e)
{
OnStopRun(this, null);
base.OnClosing(e);
}
public string File { get; set; }
private void OnOpen(object sender, RoutedEventArgs e)
{
Expand Down
15 changes: 15 additions & 0 deletions ExecutionEngine/ExecutionEngine.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
Expand All @@ -12,6 +13,8 @@ public class ExecutionEngine : IExecutionEngine
//List<Class> Classes { get; set; }
List<Instance> Instances { get; set; }
public ExecutionEnvironment BaseEnvironment { get; internal set; }
public ExecutionMode Mode { get; set; } = ExecutionMode.Infinite;
public bool IsCompleted { get; internal set; } = false;
List<RunThread> Threads = new List<RunThread>();
Dictionary<RunThread, DateTime> ThreadNextRun = new Dictionary<RunThread, DateTime>();
Thread RunThread;
Expand Down Expand Up @@ -103,19 +106,31 @@ private void Run()
}
}
}
bool allCompleted = true;
foreach (RunThread t in Threads)
{
if(t.IsCompleted)
{
Threads.Remove(t);
break;
}
else
{
allCompleted = false;
}
}
UnLock();

if (nextRun != null)
{
Thread.Sleep(10);
}
if (allCompleted && Mode == ExecutionMode.ExitWhenFinish)
{
IsCompleted = true;
Console.WriteLine("run finished");
return;
}
}
}
public void RunFunction(Function function)
Expand Down
1 change: 1 addition & 0 deletions ExecutionEngine/ExecutionEngine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ExecutionEngine.cs" />
<Compile Include="ExecutionMode.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Thread.cs" />
</ItemGroup>
Expand Down
14 changes: 14 additions & 0 deletions ExecutionEngine/ExecutionMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ScratchNet
{
public enum ExecutionMode
{
ExitWhenFinish,
Infinite
}
}

0 comments on commit 28ba0ce

Please sign in to comment.