0

bool値を返すメソッドがありますが、戻りたい値はタイマーの経過イベントに設定されているため、System.Timers.Timerが経過イベントを発生させるまで値を返すまで待つ必要があります。

public static bool RecognizePushGesture()
{
    List<Point3D> shoulderPoints = new List<Point3D>();
    List<Point3D> handPoints = new List<Point3D>();
    shoulderPoints.Add(Mouse.shoulderPoint);
    handPoints.Add(Mouse.GetSmoothPoint());
    Timer dt = new Timer(1000);
    bool click = false;

    dt.Elapsed += (o, s) =>
    {
        shoulderPoints.Add(Mouse.shoulderPoint);
        handPoints.Add(Mouse.GetSmoothPoint());
        double i = shoulderPoints[0].Z - handPoints[0].Z;
        double j = shoulderPoints[1].Z - handPoints[1].Z;
        double k = j - i;
        if (k >= 0.04)
        {
            click = true;
            dt.Stop();
        }
    };

    dt.Start();

    //should wait with returning the value until timer raises elapsed event
    return click;
}

ありがとう、ティム

4

1 に答える 1

0

AutoResetEventを使用する

public static bool RecognizePushGesture()
    {
        AutoResetEvent ar = new AutoResetEvent(false);
        List<Point3D> shoulderPoints = new List<Point3D>();
        List<Point3D> handPoints = new List<Point3D>();
        shoulderPoints.Add(Mouse.shoulderPoint);
        handPoints.Add(Mouse.GetSmoothPoint());
        Timer dt = new Timer(1000);
        bool click = false;
        dt.Elapsed += (o, s) =>
        {
            shoulderPoints.Add(Mouse.shoulderPoint);
            handPoints.Add(Mouse.GetSmoothPoint());
            double i = shoulderPoints[0].Z - handPoints[0].Z;
            double j = shoulderPoints[1].Z - handPoints[1].Z;
            double k = j - i;
            if (k >= 0.04)
            {
                click = true;
                dt.Stop();
            }
            ar.Set();
        };
        dt.Start();

        //should wait with returning the value until timer raises elapsed event
        ar.WaitOne();
        return click;
    }
于 2012-10-23T09:17:00.070 に答える