How to save state of a recursive function?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • AliRezaGoogle

    #1

    How to save state of a recursive function?

    Dear Members,
    I have written a recursive function. It calls itself recursively. It
    is placed inside a thread. So I can easily suspend and resume the
    thread to suspend or resume the function as well.

    My problem is:

    I run the application. Then run the thread. So the function also runs
    and makes stacks on stacks and goes deep and deep. I want to save an
    arbitrary state of the function (for example the depth or anything
    needed to resume). Then close the application. Then restart the
    application and reload what I saved and finally resume the thread.

    In detail I need to save "process context" of the application
    somewhere and reload it again to return to the saved point of app.
    It is like hibernating Windows!

    Can anybody help me?
    Thanks in advance.
  • Jeroen Mostert

    #2
    Re: How to save state of a recursive function?

    AliRezaGoogle wrote:
    I have written a recursive function. It calls itself recursively. It
    is placed inside a thread. So I can easily suspend and resume the
    thread to suspend or resume the function as well.
    >
    No. Just no. Redesign this. Suspending threads is evil. Can't you use an
    iterator for this?
    My problem is:
    >
    I run the application. Then run the thread. So the function also runs
    and makes stacks on stacks and goes deep and deep. I want to save an
    arbitrary state of the function (for example the depth or anything
    needed to resume). Then close the application. Then restart the
    application and reload what I saved and finally resume the thread.
    >
    The managed environment doesn't give you enough access to process state to
    accomplish this. Tying up the state in the stack is no good if you need to
    be able to store it.

    Rewrite your function so it's not recursive. Or indeed a function. C# isn't
    an O-O language for nothing. State is supposed to be contained in objects;
    going against this is just inviting trouble. What you want to do would work
    a lot better in a pure functional language.

    --
    J.

    Comment

    • Fred Mellender

      #3
      Re: How to save state of a recursive function?

      I suggest you replace the recursive function with a depth-first-search. You
      can look at the (free) library I have written for DFS, and/or download the
      documentation at http://www.lulu.com/content/1995848.

      Your nodes to the DFS can be made Serializable. You save the search by
      writing out the last node delivered (which points to its parent, etc, up to
      the root). You can reload the node and start the search at the point it
      left off by passing the last node as the (new) "root" to a new DFS.


      "AliRezaGoo gle" <asemoonya@yaho o.comwrote in message
      news:2f9ac6e2-258f-4f5c-bb02-49174ab75128@u3 6g2000prf.googl egroups.com...
      Dear Members,
      I have written a recursive function. It calls itself recursively. It
      is placed inside a thread. So I can easily suspend and resume the
      thread to suspend or resume the function as well.
      >
      My problem is:
      >
      I run the application. Then run the thread. So the function also runs
      and makes stacks on stacks and goes deep and deep. I want to save an
      arbitrary state of the function (for example the depth or anything
      needed to resume). Then close the application. Then restart the
      application and reload what I saved and finally resume the thread.
      >
      In detail I need to save "process context" of the application
      somewhere and reload it again to return to the saved point of app.
      It is like hibernating Windows!
      >
      Can anybody help me?
      Thanks in advance.

      Comment

      • AliRezaGoogle

        #4
        Re: How to save state of a recursive function?

        On Jun 8, 9:49 pm, "Fred Mellender" <nospamPlease_f red...@gmail.co m>
        wrote:
        I suggest you replace the recursive function with a depth-first-search.  You
        can look at the (free) library I have written for DFS, and/or download the
        documentation athttp://www.lulu.com/content/1995848.
        >
        Your nodes to the DFS can be made Serializable.  You save the search by
        writing out the last node delivered (which points to its parent, etc, up to
        the root).  You can reload the node and start the search at the point it
        left off by passing the last node as the (new) "root" to a new DFS.
        >
        "AliRezaGoo gle" <asemoo...@yaho o.comwrote in message
        >
        news:2f9ac6e2-258f-4f5c-bb02-49174ab75128@u3 6g2000prf.googl egroups.com...
        >
        >
        >
        Dear Members,
        I have written a recursive function. It calls itself recursively. It
        is placed inside a thread. So I can easily suspend and resume the
        thread to suspend or resume the function as well.
        >
        My problem is:
        >
        I run the application. Then run the thread. So the function also runs
        and makes stacks on stacks and goes deep and deep. I want to save an
        arbitrary state of the function (for example the depth or anything
        needed to resume). Then close the application. Then restart the
        application and reload what I saved and finally resume the thread.
        >
        In detail I need to save "process context" of the application
        somewhere and reload it again to return to the saved point of app.
        It is like hibernating Windows!
        >
        Can anybody help me?
        Thanks in advance.- Hide quoted text -
        >
        - Show quoted text -
        Thank you!
        I'll go for this. It seem that DFS search can solve my problem. Thank
        you very much.

        Comment

        Working...