DECLARING:
- DELEGATE_TASK Delegator if its necessary
- TIMER because we just want a 300 ms delay before pressing ESC key
- bool ALREADY_EXECUTED so it doesn't re-execute until finished
GOAL: We press key A, or Any handler to trigger the Event described as (Delaying the press of the ESC key 300 ms) ie. We press A, TIMER runs 300ms then Force sends ESC
1>) We Have a chosen Handler for Key A already subscribed and dumped in configure and terminated to trigger the thing
2>) Key A gets pressed -> starts timer
3>) Timer is handled through the example provided on DelayedEventsExample.zip through 1. TIMER_EVENT_PROCESSOR ,2. TriggerCustom event to trigger our handler 3. TIMER_HANDLER , and we have created DELEGATE_TASK in an effort to access this thread which we do not own
4>) TIMER_HANDLER prints Timer Finished when done this is where we have a problem because
5>) Aims to send the ESC key through the Application commands Key Gesture and CommandBindings with DELGATE_TASK
but we get
ERROR: Error on triggering custom event for NinjaScript 'Blueprint' on bar 4603: The calling thread cannot access this object because a different thread owns it.
CODE:
private delegate void DELEGATE_TASK();
private System.Timers.Timer TIMER;
private bool ALREADY_EXECUTED = false;
private void KEYBOARD ( object sender, System.Windows.Input.KeyEventArgs e )
{
if (e.Key == Key.A)
{
if (!ALREADY_EXECUTED)
{
ALREADY_EXECUTED = true;
TIMER = new System.Timers.Timer (1000);
TIMER.Elapsed += TIMER_EVENT_PROCESSOR;
TIMER.Enabled = true;
Print ("TIMER STARTED");
}
}
}
private void TIMER_EVENT_PROCESSOR ( Object myObject, EventArgs myEventArgs )
{
TriggerCustomEvent (TIMER_HANDLER, 0, TIMER.Interval);
}
//HANDLER:
private void TIMER_HANDLER ( object state )
{
ALREADY_EXECUTED = false;
TIMER.Enabled = false;
Print ("TIMER FINISHED");
var MAIN_CHART_WINDOW = System.Windows.Window.GetWindow (ChartControl.Parent) as Chart;
if (MAIN_CHART_WINDOW != null)
{
if (MAIN_CHART_WINDOW != null)
{
DELEGATE_TASK customDelegate = () =>
{
MAIN_CHART_WINDOW.InputBindings.Add (new KeyBinding (ApplicationCommands.Close, new KeyGesture (Key.Escape)));
try
{
MAIN_CHART_WINDOW.CommandBindings[0].Command.Execute (null);
}
catch (Exception ex)
{
Print (ex.Message);
}
MAIN_CHART_WINDOW.InputBindings.Clear ();
};
MAIN_CHART_WINDOW.Dispatcher.Invoke (customDelegate);
}
}
if (TIMER != null)
{
TIMER.Enabled = false;
TIMER.Elapsed -= TIMER_EVENT_PROCESSOR;
TIMER = null;
}
}
*IS IT POSSIBLE?
until we find a work around i will hand to use AUTOHOTKEY
Comment