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

Multi-threading Dispatcher

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

    Multi-threading Dispatcher

    I'm trying to add a custom dialog in State.Configure section, which launches in a different thread. I tried to access it as described in the help guide. Using InvokeAsync caused the code to continue running without running this part of the code. If I use Invoke instead it generates an error 'Error on calling 'OnStateChange' method: The calling thread cannot access this object because a different thread owns it.' After reading some forum posts, I found that the Dispatcher can be accessed in Globals.RandomDispatcher.InvokeAsync((), and I also tried other methods which generated the same error. InvokeAsync didn't generate an exception, but it didn't run the code either. Is there a way to run the code at the expected time using InvokeAsync or another method?

    #2
    Hello Matts,

    Dispatcher.InvokeAsync() is used to invoke into another existing thread, such as the UI thread.

    What are you trying to do?

    What code do you have?

    Are you trying to launch a new thread or invoke into the UI thread?

    The WPF examples I have demonstrate invoking into the existing UI thread.
    Hello All, Moving forward this will be maintained in the help guide reference samples and no longer maintained on the forum. Creating Chart WPF (UI) Modifications from an Indicator - https://ninjatrader.com/support/help...ui)-modifi.htm (https://ninjatrader.com/support/helpGuides/nt8/creating-chart-wpf-(ui)-modifi.htm) I've
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      I'm trying to add a dialog from Windows API code pack, which I believe would invoke into the UI thread. It's fairly simple code. Thanks, I will look at the examples.
      Code:
       CommonOpenFileDialog dialog = new CommonOpenFileDialog();
      dialog.IsFolderPicker = true;
      CommonFileDialogResult result = dialog.ShowDialog();
      Last edited by Matts; 03-03-2019, 11:03 PM.

      Comment


        #4
        Hello Matts,

        Dialog windows stop the thread. What you are trying to achieve?

        Below is a link to an example of prompting the user for information without stopping any threads.


        Stopping a NinjaScript thread will definitely cause undesired behavior as it will stop receiving data.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          I want the user to select a folder with this dialog. I was using the standard FolderBrowserDialog which works fine, but it's very outdated.

          This is the section of code that I was following from the example file. I want to replace the OpenFileDialog section with previous code:
          Code:
          case State.Configure:
                          {
                              if (FileNames != null)
                                  return;
          
                              OpenFileDialog dialog = new OpenFileDialog()
                              {
                                  FileName            = Custom.Resource.FileName,
                                  Filter                = Custom.Resource.FileFilterAnyWinForms,
                                  InitialDirectory    = Core.RecentFolders.GetRecentFolder("HistoryImport", Environment.GetFolderPath(Environment.SpecialFolder.Personal)),
                                  Multiselect            = true,
                                  Title                = Custom.Resource.Load
                              };
          
                              if (dialog.ShowDialog() != true)
          Last edited by Matts; 03-04-2019, 03:06 AM.

          Comment


            #6
            Hello Matts,

            I do not recommend doing this in State.Configure. I would recommend you create a public property and have this file selected before the script begins running.




            Which example are you referring to?
            Are you talking about a reference sample in the help guide?
            If so, may I have link to this reference sample?
            Chelsea B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by SantoshXX, Today, 03:09 AM
            0 responses
            11 views
            0 likes
            Last Post SantoshXX  
            Started by DanielTynera, Today, 01:14 AM
            0 responses
            2 views
            0 likes
            Last Post DanielTynera  
            Started by yertle, 04-18-2024, 08:38 AM
            9 responses
            41 views
            0 likes
            Last Post yertle
            by yertle
             
            Started by techgetgame, Yesterday, 11:42 PM
            0 responses
            12 views
            0 likes
            Last Post techgetgame  
            Started by sephichapdson, Yesterday, 11:36 PM
            0 responses
            2 views
            0 likes
            Last Post sephichapdson  
            Working...
            X