0

I have some recursive code in my windows10 App (UWP platform) I know that there are no direct way to catch StackOverflowException on C#, but there is a way by using Thread class on Windows Forms API. And I can't apply solution to UWP, because there are no Thread class. So, I have some code and I need to try execute it and stop it's Thread if it's fails. I also want to detect exception if it's possible. User can be satisfied by half finished work. My application is closing if recursion is too big. How to fix someWork(byte[] data) method?

public sealed partial class MainPage : Page {

    private byte[] dataArray; // value is creating by user
    // data can be very big, so not-resursive code can use all App's memory and crush it too!!

    private async void ButtonClick(object sender, RoutedEventArgs ev) { // here I run method
       bool isFinished = someWork(dataArray);
       // then I show dataArray (no matter finished or not) to user
    }

    private static bool someWork(byte[] data) { // I need to fix this method
        int start_arg = 0;
        try {
            someWork_recursive(data, start_arg); 
            return true; // All done
        } catch (Exception) { } // I trying to catch StackOverflowException here
        return false; // Half-finished
    }

    private static void someWork_recursive(byte[] data, int arg) { // My question is NOT about body of this method
        // code that run someWork_recursive() again or not
        // also code that change data[] values
    }

}
  • 3
    Change the recursive implementation to iterative using a Stack – Raskayu Aug 12 '16 at 10:26
  • Clearly what ever you really have instead of `bool someCondition = (123==123);` is always returning false so the recursion never stops. – ChrisF Aug 12 '16 at 10:28
  • This condition is example =). My code is much more difficult. I need to try execute it without crashing the App or show half-finished result. – Russian Nike Aug 12 '16 at 10:34
  • 1
    The correct way to fix this is not to add a thread that detects the stack overflow but to fix the code so it doesn't produce a stack overflow to begin with. Make sure your code can exit the recursiveness either by adding a fixed roof on how many recursive levels it will deal with or try to convert the whole thing into an iterative solution. – Lasse V. Karlsen Aug 12 '16 at 10:39
  • 1
    Also, if you want help with code, post *compiling* code. `someWork_recursive(data), start_arg);` is not so the code as posted is not your actual code. Since you've replaced all the code with placeholders (something) then it is pretty impossible to help you with an actual solution, other than things to try that may or may not apply to your actual code. If you want a clear and concise answer that helps, post real, clear and concise code, like a [mcve]. – Lasse V. Karlsen Aug 12 '16 at 10:39
  • I need to know if it's possible to save UWP-App from crashing if I still use recursion. if so, how? – Russian Nike Aug 12 '16 at 11:08
  • Probably best to state that in your question; the example you give supports iteration, so if you need to perform the calculation then you should probably iterate instead of using recursion. – Ade Stringer Aug 12 '16 at 11:19
  • @LasseV.Karlsen iterative solution can throw "out of memory". byte[] dataArray is defined by user and can be as big as user like. I need to save my app from closing if i'ts so. Maybe I can detect that stack is almost overfloated and finish code myself? – Russian Nike Aug 12 '16 at 12:28
  • In short: You cannot catch a `StackOverflowException`, because there wouldn't be a lot you could do about it. With the limited stack space you certainly cannot run any elaborate recovery code (including the JIT compiler on first run). [Why does .NET behave so poorly when StackOverflowException is thrown?](http://stackoverflow.com/q/22465541/1889329) explains the details, in case you are interested. – IInspectable Aug 12 '16 at 18:13

0 Answers0