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

Problems on throwing exceptions inside InvokeAsync()

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

    Problems on throwing exceptions inside InvokeAsync()

    Hi,

    i have done some testing on catching the exceptions at the AddOnFramework-sample and i wondered that sometimes the code gets them and sometimes not. I found out that's because of the Dispatcher.InvokeAsync():

    At mehods of the following structure:
    PHP Code:
    private void OnSomething(object senderEventArgs eArgs)
    {
        try
        {
            
    Dispatcher.InvokeAsync(() =>
            {
                throw new 
    Exception("Test-Exception");
                
    // Do Something
            
    }
        }
        catch (
    Exception exception)
        {
            
    Dispatcher.InvokeAsync(() =>
            {
                
    Output.Process(exception.ToString(), PrintTo.OutputTab1);
            }
        }

    it seens that the exception is handled, but at another thread and thus it will NOT be printed out whereas this code:
    PHP Code:
    private void OnSomething(object senderEventArgs eArgs)
    {
        
    Dispatcher.InvokeAsync(() =>
        {
            try
            {
                throw new 
    Exception("Test-Exception");
                
    // Do Something
            
    }
            catch (
    Exception exception)
            {
                
    Output.Process(exception.ToString(), PrintTo.OutputTab1);
            }
        });

    handles the exception at the same thread and it is printed out correctly.
    I dont know if that method (InvokeAsync() BEFORE try-catch) is a correct way to handle this problem.

    Please give me some thoughts about that...

    Thanks, GoS

    #2
    ... after some more inquiry on that, i now understand the using of InvokeAsync(): You have to use it to update the UI instantly.

    So i think the solution above is not the correct way.

    I now changed my whole code the following:

    1. Inside all the InvokeAsync() blocks there is only code for outputing or related to the UI.

    2. Because i don't handle any exceptions anyway and i only want to display them on the Log, I now catch them global by the event handler FirstChanceException and send them all to the Log. Even exceptions inside a InvokeAsync() block could be catched (but not handled) this way.

    I think my problem is solved...

    Thanks, GoS

    Comment


      #3
      Hello GoSPvC,

      Which dispatcher are you invoking into?

      Is this a random dispatcher for a new thread?

      Is this the Control Center dispatcher?

      Triggering the exception within a new thread means the try isn't going to wait. You would need to wait for the operation to complete.
      One of my method (Method1) spawns a new thread. That thread execute a method (Method2) and during exectution an exception is thrown. I need to get that exception information on the calling method (


      Where as adding the try within the new thread as well means it will wait.
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Hello Chelsea,

        It is the Dispatcher used at the AddOnSample to open the window, i have not done any changes here:
        PHP Code:
        // Open the AddOn's window when the menu item is clicked on.
        private void OnMenuItemClick(object senderRoutedEventArgs eArgs)
        {
            
        Globals.RandomDispatcher.BeginInvoke(new Action(() => new TradingSystemWindow().Show()));

        Where as adding the try within the new thread as well means it will wait.
        This means, that way is a practicable way to solve the problem ??

        Also the code above ".Wait()" added is working correct.
        PHP Code:
        private void OnSomething(object senderEventArgs eArgs

            try 
            { 
                
        Dispatcher.InvokeAsync(() => 
                { 
                    throw new 
        Exception("Test-Exception"); 
                    
        // Do Something 
                
        }).Wait();
            } 
            catch (
        Exception exception
            { 
                
        Dispatcher.InvokeAsync(() => 
                { 
                    
        Output.Process(exception.ToString(), PrintTo.OutputTab1); 
                });
            } 

        Thank you, GoS

        Comment


          #5
          Hello GoSPvC,

          I'm still not quite understanding what you are trying to achieve.

          It should be that a new window when opened from the Control Center should be started on a new thread.

          Then new window would then be running on that thread.

          In your script you are creating yet another thread so that you can do some out of sync work and then you want to wait for that work to complete before continuing the thread of the addon window?

          (It almost seems as if you are trying to invoke from one thread into itself)

          I'm not quite understanding the purpose of this. If the intention is to do work in another thread, why is there a need to wait and return this to a try happening in another thread?

          When it comes to custom threading, support for this by NinjaTrader is limited to modifying the UI by invoking the thread of the Control Center, Chart, or other window. Threading can become very complicated quickly and would fall under general C# once getting away from UI changes.


          If you addon is the window you are trying to modify, all of that is happening in one thread and there is no need to invoke into another thread.

          So with that said, adding a wait is not a guaranteed fix. While its possible the other thread's work will be finished by the time the wait is over, this entirely depends on how much work is being done and how long the wait is. Basically, you are gearing up for a race condition. I would not recommend dispatching into another thread from within a try if you are needing the exception to be handled by that try.
          Chelsea B.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by BarzTrading, Today, 07:25 AM
          2 responses
          19 views
          1 like
          Last Post BarzTrading  
          Started by devatechnologies, 04-14-2024, 02:58 PM
          3 responses
          20 views
          0 likes
          Last Post NinjaTrader_BrandonH  
          Started by tkaboris, Today, 08:01 AM
          0 responses
          4 views
          0 likes
          Last Post tkaboris  
          Started by EB Worx, 04-04-2023, 02:34 AM
          7 responses
          163 views
          0 likes
          Last Post VFI26
          by VFI26
           
          Started by Mizzouman1, Today, 07:35 AM
          1 response
          10 views
          0 likes
          Last Post NinjaTrader_Gaby  
          Working...
          X