Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

c# - Execute EventHandler, once value changed in object

I want to get my gaming wheel's values once they are updated.

The problem is the "once they are updated" without constant polling myself.

How I get the values:

var di = new DirectInput();
var jGuid = Guid.Empty;
foreach (var ger?tI in di.GetDevices(DeviceType.Joystick, DeviceEnumerationFlags.AllDevices))
{
    jGuid = ger?tI.InstanceGuid;
}
if (jGuid == Guid.Empty)
{
    return;
}
else
{
    lenkradO = new Joystick(di, jGuid);
    l_status = new JoystickState();
    lenkradO.Properties.AxisMode = DeviceAxisMode.Absolute;
    
    //lenkradO.SetNotification(kbdEvent);
    lenkradO.Acquire();
    Thread tpoll = new Thread(poll);
    tpoll.Start();
}

and

private void poll()
{
    while (1 == 1)
    {
        lenkradO.GetCurrentState(ref l_status);
        using (var writer = new System.IO.StringWriter())
        {
            ObjectDumper.Dumper.Dump(l_status, "Object Dumper", writer);
            Console.Write(writer.ToString());
        }
        Thread.Sleep(200);
    }
}

What I want is something like l_status.ValueChange += new ValueChangeHandler(this.method); instead of the poll method.

As you see, lenkrad0 also exposes a SetNotification method for a WaitHandle. I tried it with a new AutoResetEvent(false); handle, yet a waitOne within the polling function was always skipped even though the object did not change.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...