等待事件被处理

发布于 2024-11-18 20:39:46 字数 718 浏览 2 评论 0原文

我有一个函数,我们将其称为 Func1,它包含 Func2 &事件处理程序。

现在我想要实现的是不让函数(Func1)返回值,直到Func2触发并处理事件。

基本上 Func1 将字符串作为返回值,并且字符串值在事件处理程序内设置。所以我需要等待事件被处理然后返回值。

代码示例

    public static string Fun1 ()
    {
        string stringToReturn = String.Empty;
        Func2(); //Func2 will after few sec fire event bellow 

        example.MyEvent += (object sender, WebBrowserDocumentCompletedEventArgs e) =>
                               {
                                   stringToReturn = "example"; //this wont be hardcoded
                               };

        //wait for event to be handled and then return value
        return stringToReturn;
    }

I have a function, let's call it Func1 and it contains Func2 & event handler.

Now what I would like to achieve is not let function (Func1) return value till Func2 fires and handles event.

Basically Func1 has string as return value and string value is set inside event handler. So I need to wait for event to be handled and then return value.

Code Example

    public static string Fun1 ()
    {
        string stringToReturn = String.Empty;
        Func2(); //Func2 will after few sec fire event bellow 

        example.MyEvent += (object sender, WebBrowserDocumentCompletedEventArgs e) =>
                               {
                                   stringToReturn = "example"; //this wont be hardcoded
                               };

        //wait for event to be handled and then return value
        return stringToReturn;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人间不值得 2024-11-25 20:39:46

您可以使用 AutoResetEvent 类。使用 var evt = new AutoResetEvent(false); 实例化它,在要等待的地方调用 evt.WaitOne(),然后 evt.Set();< /code> 您想要在其中发出等待代码可以继续进行的信号。

如果您遇到许多涉及事件的“等到”情况,您还可以查看 反应式扩展 (接收)

You could use the AutoResetEvent class. Instantiate it with var evt = new AutoResetEvent(false);, call evt.WaitOne() where you want to wait, and evt.Set(); where you want to signal that the waiting code may proceed.

If you have many "wait until" situations that involve events, you could also look into Reactive Extensions (Rx).

笙痞 2024-11-25 20:39:46

一个简单的信号量还不够吗?

public static string Fun1 ()
{
    Semaphore sem = new Semaphore(1,1);

    string stringToReturn = String.Empty;
    Func2(); //Func2 will after few sec fire event bellow 

    example.MyEvent += (object sender, WebBrowserDocumentCompletedEventArgs e) =>
                           {
                               stringToReturn = "example"; //this wont be hardcoded
                               sem.Release();
                           };
    sem.WaitOne();

    //wait for event to be handled and then return value
    return stringToReturn;
}

Wouldn't a simple semaphore suffice?

public static string Fun1 ()
{
    Semaphore sem = new Semaphore(1,1);

    string stringToReturn = String.Empty;
    Func2(); //Func2 will after few sec fire event bellow 

    example.MyEvent += (object sender, WebBrowserDocumentCompletedEventArgs e) =>
                           {
                               stringToReturn = "example"; //this wont be hardcoded
                               sem.Release();
                           };
    sem.WaitOne();

    //wait for event to be handled and then return value
    return stringToReturn;
}
沫离伤花 2024-11-25 20:39:46

由于 Func2Func1 在同一线程上运行,因此 Func1 不会在 Func2 返回之前返回。这意味着可以保证在 Func2 中触发的事件在将控制权返回给 Func1 之前被触发。只需在调用 Func2 之前附加事件处理程序,它就会按预期工作。

在这种情况下,使用 SemaphoreAutoResetEvent 就显得有些过分了,因为两者都需要获取操作系统资源来管理线程同步。

As Func2 runs on the same thread as Func1, Func1 will not return before Func2 returns. This means it is guaranteed that the event fired in Func2 is getting fired before it returns control to Func1. Just attach your event handler before calling Func2 and it should work as expected.

Using a Semaphore or an AutoResetEvent is overkill in this scenario as both acquire OS resources to manage thread synchronization.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文