首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Myo集线器初始化过程中未触发调度程序

在Myo集线器初始化过程中未触发调度程序
EN

Stack Overflow用户
提问于 2015-01-09 15:01:57
回答 1查看 353关注 0票数 0

我正在将Myo臂章的初始化代码移植到WPF应用程序,该应用程序使用C#包装器http://goo.gl/HfwqQe与设备交互。

但是,当我在用户控件后面的代码中添加InitializeComponent();下的初始化代码时,永远不会触发更新具有连接状态的文本框的行,this.Dispatcher.Invoke((Action)(() =>

我在dispatcher代码之前在行上设置一个断点来调试它,它被称为hub.MyoConnected += (sender, e) =>,这意味着Myo已经连接,但是在更新之后的dispatcher行永远不会调用和跳过statusTbx

有人知道这是什么导致的吗?

我不知道为什么它不会将连接状态输出到textbox。这段代码以前同样有效,但这是我正在使用的C#包装器的一个新版本。

控制台示例运行良好,http://goo.gl/RFHLym和输出连接到控制台,但我无法让它输出到textbox的连接。

这是获取Myo arm波段连接状态的完整代码:

代码语言:javascript
复制
using MyoSharp.Communication;
using MyoSharp.Device;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MyoTestv4
{
    /// <summary>
    /// Interaction logic for ProductsView.xaml
    /// </summary>
    public partial class AdductionAbductionFlexionView : UserControl
    {
        public AdductionAbductionFlexionView()
        {
            InitializeComponent();



            // create a hub that will manage Myo devices for us
            using (var channel = Channel.Create(ChannelDriver.Create(ChannelBridge.Create())))
            using (var hub = Hub.Create(channel))
            {
                 //set a bpoint here, gets triggered
                // listen for when the Myo connects
                hub.MyoConnected += (sender, e) =>
                {
                     //set a bpoint here, doesn't get triggered
                    this.Dispatcher.Invoke((Action)(() =>
                    {
                        statusTbx.Text = "Myo has connected! " + e.Myo.Handle;
                        //Console.WriteLine("Myo {0} has connected!", e.Myo.Handle);
                        e.Myo.Vibrate(VibrationType.Short);

                    }));
                };

                // listen for when the Myo disconnects
                hub.MyoDisconnected += (sender, e) =>
                {
                    this.Dispatcher.Invoke((Action)(() =>
                    {
                        statusTbx.Text = "Myo has disconnected!";
                        //Console.WriteLine("Oh no! It looks like {0} arm Myo has disconnected!", e.Myo.Arm);
                        e.Myo.Vibrate(VibrationType.Medium);
                    }));
                };

                // start listening for Myo data
                channel.StartListening();
            }
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-09 15:21:43

您将得到此错误,因为channelhub在调用后立即被释放。

代码语言:javascript
复制
channel.StartListening();

using是一种方便的方式来为您释放对象,在这种情况下,这是不需要的。请参考使用语句(C#引用)获得更多信息。

尝试以下步骤来解决问题。1.将通道和集线器声明为类的私有字段。2.不要使用using关键字。3.当hubchannel被释放时,记住要处理AdductionAbductionFlexionViewAdductionAbductionFlexionView

代码语言:javascript
复制
public partial class AdductionAbductionFlexionView : UserControl
{
    IChannel channel;
    IHub hub;

    public AdductionAbductionFlexionView()
    {
        InitializeComponent();

        // create a hub that will manage Myo devices for us
        channel = Channel.Create(ChannelDriver.Create(ChannelBridge.Create()));
        hub = Hub.Create(channel);

        //set a bpoint here, gets triggered
        // listen for when the Myo connects
        hub.MyoConnected += (sender, e) =>
        {
            //set a bpoint here, doesn't get triggered
            this.Dispatcher.Invoke((Action)(() =>
            {
                statusTbx.Text = "Myo has connected! " + e.Myo.Handle;
                //Console.WriteLine("Myo {0} has connected!", e.Myo.Handle);
                e.Myo.Vibrate(VibrationType.Short);

            }));
        };

        // listen for when the Myo disconnects
        hub.MyoDisconnected += (sender, e) =>
        {
            this.Dispatcher.Invoke((Action)(() =>
            {
                statusTbx.Text = "Myo has disconnected!";
                //Console.WriteLine("Oh no! It looks like {0} arm Myo has disconnected!", e.Myo.Arm);
                e.Myo.Vibrate(VibrationType.Medium);
            }));
        };

        // start listening for Myo data
        channel.StartListening();
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27863413

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档