471,312 Members | 1,852 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,312 software developers and data experts.

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.
Jun 27 '08 #1
3 2405
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.
http://symbolsprose.blogspot.com
Jun 27 '08 #2
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.
"AliRezaGoogle" <as*******@yahoo.comwrote in message
news:2f**********************************@u36g2000 prf.googlegroups.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.

Jun 27 '08 #3
On Jun 8, 9:49*pm, "Fred Mellender" <nospamPlease_fred...@gmail.com>
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.

"AliRezaGoogle" <asemoo...@yahoo.comwrote in message

news:2f**********************************@u36g2000 prf.googlegroups.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.
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Someonekicked | last post: by
7 posts views Thread by John J. Hughes II | last post: by
7 posts views Thread by Javaman59 | last post: by
6 posts views Thread by nilavya | last post: by
1 post views Thread by Irene | last post: by
1 post views Thread by Allen | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.