views:

91

answers:

1

How can I setup a timer to call a method if it expires before receiving a return value?

                myTimer.Interval = 100000;
                bool status = methodReturningValue();
                myTimer.Start();

                if (status == true && myTimer.expired == false)
                    // Do Something
                else
                    // So Something Else

this is just something I typed up to give you an idea of what I would like to accomplish, I hope it makes sense.

I guess I could try something like:

                Timer myTimer = new Timer();
                myTimer.Interval = 100000;
                myTimer.Enabled;
                myTimer.Start();
                myTimer.Tick += new EventHandler(Method(args));

but that just seems sloppy, there has to be a better way...

A: 

This SO post should answer your question.

Eric J.
I did read that post but it looks like the OP does not have control over the code hes calling and has a few more issues which complicate things a bit (not to mention that half that code is over my head) but it looks like two people have now recommend that post so maybe I should take another look.
Leroy Jenkins
Fundamentally, in order to get timeout behavior, you need to make the call on one thread while monitoring the call on another thread. The approach in that post is a relatively easy way to accomplish that goal.
Eric J.