等待里面的方法直到事件被捕获 [英] Wait inside method until event is captured

查看:125
本文介绍了等待里面的方法直到事件被捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法在C#中有这个问题。我做了一个方法,从一个dll调用一个函数,称为 Phone.GetLampMode();
现在 Phone.GetLampMode 不返回任何东西。在 onGetLampModeResponse '事件的事件中返回数据。有没有办法可以等待我的方法,直到我从onGetLampModeResponse事件获取数据?

  public bool checkLamp(int iLamp)
{
Phone.ButtonIDConstants btn = new Phone.ButtonIDConstants();
btn = Phone.ButtonIDConstants.BUTTON_1;
btn + = iLamp;
Phone.GetLampMode(btn,null);

返回true;
}

private void Phone_OnGetLampModeResponse(object sender,Phone.GetLampModeResponseArgs e)
{
var test = e.getLampModeList [0] .getLampMode.ToString();
}


解决方案

一个解决方案是使用 AutoResetEvent

  public bool checkLamp(int iLamp)
{
Phone.ButtonIDConstants btn = new Phone.ButtonIDConstants();
btn = Phone.ButtonIDConstants.BUTTON_1;
btn + = iLamp;

AutoResetEvent waitHandle = new AutoResetEvent(false);

//将waitHandle作为用户状态传递
Phone.GetLampMode(btn,waitHandle);

//等待事件完成
waitHandle.WaitOne();

返回true;
}

private void Phone_OnGetLampModeResponse(object sender,Phone.GetLampModeResponseArgs e)
{
var test = e.getLampModeList [0] .getLampMode.ToString();

//事件处理程序完成
//我猜在GetLampModeResponseArgs类中有一些UserState属性
((AutoResetEvent)e.UserState).Set();
}

注意:
您正在使用电话作为静态类/变量,可以认为你正在Windows Phone上开发...如果是这样,请注意WP和异步编程的整体概念是不以这种方式锁定UI线程


I have this issue with a method in C#. I made a method that calls a function from a dll its called Phone.GetLampMode(); Now Phone.GetLampMode doesnt return anything. The data gets returned in a event the 'onGetLampModeResponse' event. Is there a way i can wait in my method until i get the data from the onGetLampModeResponse event?

public bool checkLamp(int iLamp)
{
    Phone.ButtonIDConstants btn = new Phone.ButtonIDConstants();
    btn = Phone.ButtonIDConstants.BUTTON_1;
    btn += iLamp;
    Phone.GetLampMode(btn, null);

    return true;
}

private void Phone_OnGetLampModeResponse(object sender, Phone.GetLampModeResponseArgs e)
{
    var test = e.getLampModeList[0].getLampMode.ToString();    
}

解决方案

One solution is to use AutoResetEvent:

public bool checkLamp(int iLamp)
{
    Phone.ButtonIDConstants btn = new Phone.ButtonIDConstants();
    btn = Phone.ButtonIDConstants.BUTTON_1;
    btn += iLamp;

    AutoResetEvent waitHandle = new AutoResetEvent(false); 

    // Pass waitHandle as user state
    Phone.GetLampMode(btn, waitHandle);

    // Wait for event completion
    waitHandle.WaitOne();

    return true;
}

private void Phone_OnGetLampModeResponse(object sender, Phone.GetLampModeResponseArgs e)
{
    var test = e.getLampModeList[0].getLampMode.ToString();

    // Event handler completed
    // I guess there is some UserState property in the GetLampModeResponseArgs class
    ((AutoResetEvent)e.UserState).Set();
}

NOTE: Ad you're using Phone as a static class/variable, one can think you're developing on Windows Phone... If it is the case, do note that the whole concept of WP and async programming is to not lock the UI thread in a such way.

这篇关于等待里面的方法直到事件被捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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