How to save state of a recursive function?

A

AliRezaGoogle

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.
 
J

Jeroen Mostert

AliRezaGoogle said:
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.
 
C

colin

sounds like you need to push the information you need onto a stack
type of list, also if you do this you can sometimes avoid
the recursive calls.

ie your function proceeses parameters from the stacklist instead of from the
stack itself, if it needs to call itself again it just puts the parameters
onto the end of the stack,
ofc you need to be careful about avoiding adding to the list inside an
iteration loop using a list enumerator,
but using an index seems to work, as does removing the items from the
begining,
although I have found this to be a little bit slow.

you can ofc then save the stack list as you wish, although I
have no idea about what you actually neeed to do
in order to ensure your suspending and resuming can actually
work, but at the least you must make sure that when you suspend
that the list is garanteed to be in a usable state.
ie not in the process of being added to.

Colin =^.^=
 
A

AliRezaGoogle

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.










- Show quoted text -

Thank you!
I'll go for this. It seem that DFS search can solve my problem. Thank
you very much.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top