C#读取操纵杆输入而不阻止UI

时间:2014-11-30 03:13:31

标签: c# asynchronous joystick

对于你们中的一些人来说这可能听起来很奇怪,但我无法找到正确的方法来读取操纵杆输入而不会阻止我的UI形式。我在互联网上找到了这个例子:

static void Main()
{
    // Initialize DirectInput
    var directInput = new DirectInput();

    // Find a Joystick Guid
    var joystickGuid = Guid.Empty;

    foreach (var deviceInstance in directInput.GetDevices(DeviceType.Gamepad, 
                DeviceEnumerationFlags.AllDevices))
        joystickGuid = deviceInstance.InstanceGuid;

    // If Gamepad not found, look for a Joystick
    if (joystickGuid == Guid.Empty)
        foreach (var deviceInstance in directInput.GetDevices(DeviceType.Joystick, 
                DeviceEnumerationFlags.AllDevices))
            joystickGuid = deviceInstance.InstanceGuid;

    // If Joystick not found, throws an error
    if (joystickGuid == Guid.Empty)
    {
        Console.WriteLine("No joystick/Gamepad found.");
        Console.ReadKey();
        Environment.Exit(1);
    }

    // Instantiate the joystick
    var joystick = new Joystick(directInput, joystickGuid);

    Console.WriteLine("Found Joystick/Gamepad with GUID: {0}", joystickGuid);

    //Query all suported ForceFeedback effects
    var allEffects = joystick.GetEffects();
    foreach (var effectInfo in allEffects)
        Console.WriteLine("Effect available {0}", effectInfo.Name);

    //Set BufferSize in order to use buffered data.
    joystick.Properties.BufferSize = 128;

    // Acquire the joystick
    joystick.Acquire();

    // Poll events from joystick
    while (true)
    {
        joystick.Poll();
        var datas = joystick.GetBufferedData();
        foreach (var state in datas)
            Console.WriteLine(state);
    }
}

我安装了sharpdx,我可以在控制台中看到操纵杆轴输出。

现在我想在我的UI上的单独文本框中显示每个轴数据。所以我改了几行代码:

joystick.Poll();
var data = joystick.GetBufferedData();
foreach (var state in data) 
{
    if (state.Offset == JoystickOffset.X)
    {
       textBox1.Text = state.Value.ToString();
    }
}

所以这应该将数据传递给文本框。如果我在这里写Console.Writeline(state.Value),我会得到我想要显示的数据。问题是,这个while loop会阻止UI。

我想将此while循环放在private void timer_tick中,并设置10毫秒的刷新率。但我不知道如何在单独的函数中访问var joystick之类的变量。我还读到这可以通过异步编程来完成。可悲的是,我现在被困住了。

有人可以帮我解决这个问题的简单解决方案吗?代码示例很棒。

1 个答案:

答案 0 :(得分:1)

假设您使用WinForm进行编码(在您的问题中从blocking my UI form推导出来)。

从设计器向您的表单添加计时器。

并在Form的.ctor中添加这些行:

public partial class JoyStickForm : Form
{
    public JoyStickForm()
    {
        InitializeComponent();

        // ...Other initialization code has been stripped...

        // Instantiate the joystick
        var joystick = new Joystick(directInput, joystickGuid);

        //Console.WriteLine("Found Joystick/Gamepad with GUID: {0}", joystickGuid);

        timer1.Interval = 100; 
        timer1.Tick += (s, e) =>
            {
                joystick.Poll();
                var lastState = joystick.GetBufferedData().Last(); //only show the last state

                if (lastState != null)
                {
                    if (lastState.Offset == JoystickOffset.X)
                    {
                        textBox1.Text = lastState.Value.ToString();
                    }
                }
            };

        //start the timer
        timer1.Enabled = true;
    }
}

说明:

  1. 每秒100次的刷新率不必要地快,请记住大多数显示器的刷新率约为60 Hz。我将其设置为100毫秒(每秒10次刷新),这是可以接受的。

  2. 使用lambda expression,您可以从计时器的tick处理程序访问var joystick之类的变量。

  3. 在每次刷新时,只有数据中的最后一个状态显示在TextBox