• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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.
    http://ninjatrader.com/support/forum...327#post499327
    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.
        https://ninjatrader.com/support/foru...686#post492686

        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.

            https://ninjatrader.com/support/foru...744#post719744


            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 jamarc, Today, 02:30 AM
            1 response
            6 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by uday12, Yesterday, 04:19 PM
            1 response
            7 views
            0 likes
            Last Post NinjaTrader_PaulH  
            Started by grose, Today, 06:53 AM
            2 responses
            7 views
            0 likes
            Last Post grose
            by grose
             
            Started by Bobisam, Yesterday, 04:57 PM
            1 response
            21 views
            0 likes
            Last Post NinjaTrader_PaulH  
            Started by voltlon, Yesterday, 05:18 PM
            1 response
            10 views
            0 likes
            Last Post NinjaTrader_PaulH  
            Working...
            X