• 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

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.
      https://stackoverflow.com/questions/...fferent-thread

      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.
          http://ninjatrader.com/support/helpG...-threading.htm

          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 jshapen, Yesterday, 06:35 PM
          0 responses
          8 views
          0 likes
          Last Post jshapen
          by jshapen
           
          Started by leocrespo, 11-06-2015, 01:48 AM
          5 responses
          1,767 views
          0 likes
          Last Post nkhoi
          by nkhoi
           
          Started by NT8Newbie, Yesterday, 12:49 PM
          0 responses
          16 views
          0 likes
          Last Post NT8Newbie  
          Started by pdennis, Yesterday, 11:43 AM
          0 responses
          5 views
          0 likes
          Last Post pdennis
          by pdennis
           
          Started by Duval, Yesterday, 08:38 AM
          0 responses
          17 views
          0 likes
          Last Post Duval
          by Duval
           
          Working...
          X