views:

911

answers:

5

Is there a nice simple method of delaying a function call whilst letting the thread continue executing?

e.g.

public void foo()
{
    // Do stuff!

    // Delayed call to bar() after x number of ms

    // Do more Stuff
}

public void bar()
{
    // Only execute once foo has finished
}

Im aware that this can be achieved by using a timer and event handlers, but I was wondering if there is a standard c# way to achieve this?

If anyone is curious, the reason that this is required is that foo() and bar() are in different (singleton) classes which my need to call each other in exceptional circumstances. The problem being that this is done at initialisation so foo needs to call bar which needs an instance of the foo class which is being created... hence the delayed call to bar() to ensure that foo is fully instanciated.. Reading this back almost smacks of bad design !

EDIT

Ill take the points about bad design under advisement! Ive long thought that I might be able to improve the system, however, this nasty situation only occurs when an exeption is thrown, at all other times the two singletons co-exist very nicely. I think that Im not going to messaround with nasty async-patters, rather Im going to refactor the initialisation of one of the classes.

+4  A: 

It sounds like the control of the creation of both these objects and their interdependence needs to controlled externally, rather than between the classes themselves.

AdamRalph
+1, this does sound like you need an orchestrator of some sort and perhaps a factory
ng5000
A: 

Almost smacks of bad design?

Peter Morris
Yeah almost! there are some valid reasons for needing to use this setup.
TK
Why not post your actual requirements rather than a pseudo requirement? A roughly accurate question at best gets you a roughly accurate answer.
Peter Morris
+1  A: 

Well, I'd have to agree with the "design" point... but you can probably use a Monitor to let one know when the other is past the critical section...

    public void foo() {
        // Do stuff!

        object syncLock = new object();
        lock (syncLock) {
            // Delayed call to bar() after x number of ms
            ThreadPool.QueueUserWorkItem(delegate {
                lock(syncLock) {
                    bar();
                }
            });

            // Do more Stuff
        } 
        // lock now released, bar can begin            
    }
Marc Gravell
A: 

There is no standard way to delay a call to a function other than to use a timer and events.

This sounds like the GUI anti pattern of delaying a call to a method so that you can be sure the form has finished laying out. Not a good idea.

ng5000
+1  A: 

It's indeed a very bad design, let alone singleton by itself is bad design.

However, if you really do need to delay execution, here's what you may do:

BackgroundWorker barInvoker = new BackgroundWorker();
barInvoker.DoWork += delegate
 {
  Thread.Sleep(TimeSpan.FromSeconds(1));
  bar();
 };
barInvoker.RunWokerAsync();

This will, however, invoke bar() on a separate thread. If you need to call bar() in the original thread you might need to move bar() invocation to RunWorkerCompleted handler or do a bit of hacking with SynchronizationContext.

Anton Gogolev