Chronos Plugins 5.4.0
This documentation covers the plugin interfaces definitions and an example implementation.
Loading...
Searching...
No Matches
MockSampleListWorker.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Diagnostics.CodeAnalysis;
5using System.Linq;
6using System.Runtime.CompilerServices;
7using System.Threading.Tasks;
8using System.Windows;
9using System.Windows.Interop;
12// ReSharper disable LocalizableElement
13
18{
26 // ReSharper disable once UnusedMember.Global
27 // ReSharper disable once ClassNeverInstantiated.Global
28 [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
29 // ReSharper disable once ClassNeverInstantiated.Global
30 public class MockSampleListWorker :
33 INotifyPropertyChanged,
36 {
37 private const string NormalButtonLabel = "Run plugin provided\r\nsample lists";
38 private readonly TaskFactory mGuiFactory;
39
41 {
42 // for GUI synchronized operations
43 mGuiFactory = new TaskFactory(Helpers.Gui.GuiThreadScheduler);
44 // for the remote access example
45 RemotePluginService.StartService();
46 }
47
48 public string ButtonCaption { get; private set; } = NormalButtonLabel;
49
50 public System.Drawing.Icon ButtonIcon { get; private set; } = Properties.Resources.MockNormal;
51
60 private bool OneMoreScheduleWanted(Window ownerWin)
61 {
62 bool oneMore = false;
63 DoOnGUIThread(() => oneMore = MessageBox.Show(ownerWin, "Start one more?",
64 "Restart loop",
65 MessageBoxButton.YesNo,
66 MessageBoxImage.Question) == MessageBoxResult.Yes);
67 return oneMore;
68 }
69
76 public void DoYourJob()
77 {
78 // Use the process' main window as owner, so that our blocking
79 // window can not be hidden behind the main window.
80 ShowPluginIsInCharge win = null;
81 ButtonIcon = Properties.Resources.MockBusy;
82 ButtonCaption = "Plugin busy";
85
86 DoOnGUIThread(() =>
87 {
88 // checking if it also works when called from the GUI thread
90 win = new ShowPluginIsInCharge();
91 var wih = new WindowInteropHelper(win);
92 var winHandle = wih.EnsureHandle();
95 Helpers.Gui.OwnMyWindow(winHandle);
96 win.Show();
97 });
98 try
99 {
100 do
101 {
102 // prevent previously failed planners from stopping us
104 Helpers.Debug.TraceWrite($"Just FYI: Standard sample lists are at {Helpers.Config.PathToSampleLists ?? "(N/A)"} and methods are at {Helpers.Config.PathToMethods ?? "(N/A)"}, the instrument config is at {Helpers.Config.PathToInstrumentConfig}");
106 System.Threading.Thread.Sleep(5000);
107 var ex = RunSampleList?.Invoke(this,
109 {
110 //SampleListFile = @"C:\Users\Patrick\Documents\Chronos\MoveTest.csl"
111 ExtendLastPlanner = false,
112 StartAndWaitForEnd = false,
113 SwitchToSchedulesView = false,
114 RespectSelection = false
115 }
116 );
117 if (ex != null)
118 {
119 System.Windows.Forms.MessageBox.Show(Helpers.Gui.MainWindow,$"Error: {ex.Message}", "Plugin Provided Schedule",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Error);
120 }
121 } while (OneMoreScheduleWanted(win));
122 }
123 finally
124 {
125 DoOnGUIThread(() => win.Close());
126 ButtonIcon = Properties.Resources.MockNormal;
130 }
131 }
132
134 {
135 win.AbortButton.Click += (s, e) =>
136 {
137 // Click handler: This runs on the GUI thread
138 win.AbortButton.IsEnabled = false;
139 var abortWaiter = mStopRun.Invoke(new StopRunArgs() {How = StopRunArgs.StopMode.Immediately});
140 abortWaiter.ContinueWith((t) =>
141 {
142 // after the background abort call has finished, continue with enabling the button on the GUI thread.
143 try
144 {
145 win.AbortButton.IsEnabled = true;
146 }
147 catch
148 {
149 // suppress exceptions in case the button does not exist any more
150 }
152 };
153 }
154
156 {
157 // This is just a quick and dirty piece of code for showing the IStopRun interface usage.
158 //
159 win.StopButton.Click += (s, e) =>
160 {
161 win.StopButton.IsEnabled = false;
162 var stopWaiter = mStopRun.Invoke(new StopRunArgs() { How = StopRunArgs.StopMode.NoNewJobs, RestartRemainingJobs = false,StopQueue = true});
163 stopWaiter.ContinueWith((t) =>
164 {
165 try
166 {
167 win.StopButton.IsEnabled = true;
168 }
169 catch
170 {
171 // suppress exceptions in case the button does not exist any more
172 }
174 };
175 }
176
177 private void TurnOffResets()
178 {
179 foreach (var somePAL in ConfiguredDevices.OfType<IPal3Access>())
180 {
181 somePAL.Options.AlwaysResetAfterSequence = false;
182 somePAL.Options.ResetBeforeSequence = false;
183 }
184 }
185
190 private void DoOnGUIThread(Action theAction)
191 {
192 mGuiFactory.StartNew(theAction).GetAwaiter().GetResult();
193 }
194
196
197 #region Sample List Access
198
203 {
204 set
205 {
206 // not using it in this demo plugin yet
207 }
208 }
209
210 #endregion Sample List Access
211
212 #region Implementation of IDirectDeviceAccess
213
214 public IEnumerable<IDevice> ConfiguredDevices { get; set; }
215
216 #endregion
217
218 public void Dispose()
219 {
221 }
222
223 public event PropertyChangedEventHandler PropertyChanged;
224
225 protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
226 {
227 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
228 }
229
230 public IEnumerable<string> LogPaths => GetFakeLogs(GetType().FullName);
231
237 public static IEnumerable<string> GetFakeLogs(string creator)
238 {
239 for (var i = 1; i <= 5; ++i)
240 {
241 var fakePath = System.IO.Path.GetTempFileName();
242 System.IO.File.WriteAllText(fakePath, $"Fake log entry in file {i} created at {DateTime.Now:hh:mm:ss} by {creator}");
243 yield return fakePath;
244 }
245 }
246
247 private Func<StopRunArgs, Task> mStopRun;
248 public Func<StopRunArgs, Task> StopRun
249 {
250 set => mStopRun = value;
251 }
252 }
253}
Things provided by AxelSemrau Chronos - do not put your own code into this namespace.
Classes and interfaces that are meant for plugins. The classes and interfaces below this namespace ar...
delegate Exception RunSampleListHandler(object sender, RunSampleListEventArgs args)
Runs the given sample list.
The classes in this namespace demonstrate how to interact with the Chronos sample list.
Access to some parts of the core of the Chronos program.
Definition: Core.cs:14
static IScheduleQueue ExecutionQueue
The list of schedules that have run, are running or will be run.
Definition: Core.cs:18
void RemoveFailedPlanners()
Tries to remove failed planners. Use this if you are hiding the normal Chronos GUI from your user and...
Implement this interface if you need direct access to the list of configured devices.
Provides access to some options and configuration information for the PAL3 samplers even out of the s...
System.Threading.Tasks.TaskScheduler GuiThreadScheduler
Use this scheduler to do actions on the thread that owns the main GUI.
Definition: Helpers.cs:22
void OwnMyWindow(IntPtr handle)
Make the window with the given handle owned by the Chronos main window. This prevents it from droppin...
IWin32Window MainWindow
If you need to set the owner window yourself or want to show message boxes.
Definition: Helpers.cs:37
void TraceWrite(string text, params object[] arguments)
Write something to the Chronos trace log.
Static instance for access to utility functions and resources.
Definition: Helpers.cs:78
static IGuiHelper Gui
Utility functions for window handling.
Definition: Helpers.cs:92
static IDebugHelper Debug
Utility functions for debugging.
Definition: Helpers.cs:97
This can be called for a sample list worker or device that writes its own set of log files which shou...
Implement this interface with your device or sample list worker to get fine-grained control about sto...
Options for stopping the schedule/queue.
StopMode
Details how to stop the run.
For automatic generation of sample lists and running the resulting schedules.
Parameters for sample list loading and schedule creation.
Implement this interface in your sample list worker class if you need access to the content of cells.
Provides basic sample list information and lets you iterate over all lines.
Provides an endless supply of nonsense sample lists.
Func< StopRunArgs, Task > StopRun
Callback function returning a task that completes once the schedule queue was stopped.
virtual void OnPropertyChanged([CallerMemberName] string propertyName=null)
void HandleStopButton(ShowPluginIsInCharge win)
System.Drawing.Icon ButtonIcon
Shown in the button, preferred size 22x22.
void DoOnGUIThread(Action theAction)
Execute the specified action in the GUI thread's context.
bool OneMoreScheduleWanted(Window ownerWin)
Ask the user if he wants to start more schedules.
string ButtonCaption
Shown on the sample list page.
void HandleAbortButton(ShowPluginIsInCharge win)
IEnumerable< string > LogPaths
Provide full paths to each of your log files here.
void DoYourJob()
Take over control. When this function exits, Chronos is in charge again.
static IEnumerable< string > GetFakeLogs(string creator)
Creates a few fake log files.
ISampleListAccessor SampleList
Here we get an helper that allows us to manipulate the current sample list.
IEnumerable< IDevice > ConfiguredDevices
List of IDevice for all configured devices in Chronos.
Example for communication with external programs.
Interaction logic for ShowPluginIsInCharge.xaml.