延迟的函数调用 [英] Delayed function calls

查看:80
本文介绍了延迟的函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b

例如

有没有简单的方法延迟函数调用,同时让线程继续执行? pre> public void foo()
{
//做东西!

//延迟调用bar()后的x个数ms

//做更多的东西
}

public void bar ()
{
//只执行一次foo完成
}

我知道这可以通过使用计时器和事件处理程序来实现,但我想知道是否有标准的c#方法来实现这一点?



如果任何人都好奇,这是必需的原因是foo()和bar()是不同的(单例)类,我需要在特殊情况下互相调用。问题在于,这是在初始化时完成的,所以foo需要调用bar,它需要正在创建的foo类的实例...因此,延迟调用bar()以确保foo完全实例化。几乎是坏的设计!



编辑

我会在劝告下采取有关糟糕设计的观点!我一直认为我可以改进系统,但是,这种恶劣的情况只会在抛出异常时发生,而在其他所有时间这两个单例并存非常好。我认为我不会讨厌讨厌的异步模式,而是要重构其中一个类的初始化。

解决方案<这听起来像是这些对象创建的控制和相互依赖需要在外部进行控制,而不是在类之间进行控制。

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
}

I'm 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

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

解决方案

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.

这篇关于延迟的函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆