locked
C# exceptions within a periodic method RRS feed

  • Question

  • Hello,

    I'm writing a Widows Runtime component which has a periodic function. Like the following:

    private ThreadPoolTimer aTimer; public void StartTimer() { try { aTimer = ThreadPoolTimer.CreatePeriodicTimer

    (new TimerElapsedHandler(async (Timer_Tick)=> await RunTimer()),

    TimeSpan.FromMinutes(1)); } catch (Exception e) { throw e; } } private IAsyncAction RunTimer() { try { return Task.Run(async () => { await ..... }).AsAsyncAction(); } catch (Exception exp) { throw exp; } }

    But if running for a while, the App references the WinRT crashed and the app did use try catch when calling StartTimer.

    A few questions here:

    1. If RunTimer throws an exception, will it pop up to StartTimer, then App? If not, how to make it so, since it is the app handing all exceptions.

    2. Should the "Try Catch" in RunTimer be inside the lambda expression instead (in above example, surrounding the highlighted await function)

    3. for RunTimer, should use "private async Task" instead? I know IAsyncAction should be used for public functions.

    Thanks.

    Sunday, August 11, 2013 5:49 AM

Answers

  • The following line of code created an anonymous void async method so all the exceptions will go nowhere

    new TimerElapsedHandler(async (Timer_Tick)=> await RunTimer()

    If you want to catch them you nee to introduce try/catch inside of async methods. For example the following code demonstrates two places where exceptions are catchable

    public void StartTimer()
    {
    	_timer = ThreadPoolTimer.CreatePeriodicTimer(
                        new TimerElapsedHandler(OneTick), 
    TimeSpan.FromSeconds(10));
    }
    
    private async void OneTick(ThreadPoolTimer timer)
    {
    	try
    	{
    		await RunTimer();
    	}
    	catch (Exception e)
    	{
    		// CATCHED...
    	}
    }
    
    private IAsyncAction RunTimer()
    {
    	return Task.Run(async () =>
    	{
    		try
    		{
    			throw new Exception("Error");
    		} 
    		catch (Exception e)
    		{
    			// CATCHED...
    		}
    	})
    	.AsAsyncAction();
    }
    


    Blog: http://lunarfrog.com/blog

    • Marked as answer by Chunmiao Tuesday, August 13, 2013 4:02 PM
    Tuesday, August 13, 2013 2:30 PM

All replies

  • What exception are you getting?  
    Sunday, August 11, 2013 1:21 PM
  • The recent exception is System.IndexOutOfRange.

    I more concern how to make the exceptions thrown in WinRT be caught in the app.

    Thanks.

    Sunday, August 11, 2013 9:10 PM
  • Any ideas?
    Monday, August 12, 2013 5:00 PM
  • The following line of code created an anonymous void async method so all the exceptions will go nowhere

    new TimerElapsedHandler(async (Timer_Tick)=> await RunTimer()

    If you want to catch them you nee to introduce try/catch inside of async methods. For example the following code demonstrates two places where exceptions are catchable

    public void StartTimer()
    {
    	_timer = ThreadPoolTimer.CreatePeriodicTimer(
                        new TimerElapsedHandler(OneTick), 
    TimeSpan.FromSeconds(10));
    }
    
    private async void OneTick(ThreadPoolTimer timer)
    {
    	try
    	{
    		await RunTimer();
    	}
    	catch (Exception e)
    	{
    		// CATCHED...
    	}
    }
    
    private IAsyncAction RunTimer()
    {
    	return Task.Run(async () =>
    	{
    		try
    		{
    			throw new Exception("Error");
    		} 
    		catch (Exception e)
    		{
    			// CATCHED...
    		}
    	})
    	.AsAsyncAction();
    }
    


    Blog: http://lunarfrog.com/blog

    • Marked as answer by Chunmiao Tuesday, August 13, 2013 4:02 PM
    Tuesday, August 13, 2013 2:30 PM
  • Thanks a lot!
    Tuesday, August 13, 2013 4:02 PM
  • Sorry, I know I have marked the answer, however, even though OneTick caught the exception, but where does the exception throwing from OneTick go? Like the following code, where does the "e" eventually go? I hope it will reach App level since that's place handling all exceptions.

    private async void OneTick(ThreadPoolTimer timer) { try { await RunTimer(); } catch (Exception e) { throw e;// where does it go

    } }

    Thanks a lot!

    Thursday, August 15, 2013 6:52 AM