有没有一个很好的简单的方法来延迟函数调用,同时让线程继续执行?
例如:。
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
}
我知道这可以通过使用计时器和事件处理程序来实现,但是我想知道是否有一个标准的 c # 方法来实现这一点?
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 !
剪辑
我将接受关于糟糕设计的建议!我一直认为我可以改进这个系统,但是,当抛出一个异常时,这种糟糕的情况就会发生 只有,在其他任何时候,这两个单例模式都能很好地共存。我认为我不打算使用讨厌的异步模式,相反,我将重构其中一个类的初始化。