Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
5.0k 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...