Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Calling thread cant access different thread work around?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Calling thread cant access different thread work around?

    Hello,

    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
    Last edited by Entwaze; 07-02-2023, 12:47 PM.

    #2
    Hello Entwaze,

    Thanks for your post.

    You can only use InvokeAsync in NinjaScript, not BeginInvoke/Invoke.

    Further, ensure you use the Dispatcher for the object you want to use threading with instead of just using Dispatcher.

    For example:

    if (ChartControl != null)
    ChartControl.Dispatcher.InvokeAsync

    See this help guide page for more information about Dispatcher: https://ninjatrader.com/support/help...patcher​

    This forum thread will also be open for other community members to share their insights on the topic.​
    Brandon H.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by burtoninlondon, Today, 12:38 AM
    0 responses
    5 views
    0 likes
    Last Post burtoninlondon  
    Started by AaronKoRn, Yesterday, 09:49 PM
    0 responses
    14 views
    0 likes
    Last Post AaronKoRn  
    Started by carnitron, Yesterday, 08:42 PM
    0 responses
    11 views
    0 likes
    Last Post carnitron  
    Started by strategist007, Yesterday, 07:51 PM
    0 responses
    13 views
    0 likes
    Last Post strategist007  
    Started by StockTrader88, 03-06-2021, 08:58 AM
    44 responses
    3,983 views
    3 likes
    Last Post jhudas88  
    Working...
    X