首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >32英尺客户端连接线程失败

32英尺客户端连接线程失败
EN

Stack Overflow用户
提问于 2018-06-01 04:41:55
回答 1查看 90关注 0票数 0

出于测试的目的,我试图通过我作为客户端制作的应用程序连接到我的手机,但我遗漏了一些东西。在配对设备之后,程序应该打开一个运行client.BeginConnect的新线程,但它只能运行“启动连接线程...”。

代码语言:javascript
复制
        BluetoothDeviceInfo deviceInfo;
    private void listBox1_DoubleClick(object sender, EventArgs e)
    {
        deviceInfo = devices.ElementAt(listBox1.SelectedIndex);
        updateUI(deviceInfo.DeviceName + " was selected. Attempting to connect.");

        if (pairDevice())
        {
            updateUI("Device paired.");
            updateUI("Starting connect thread...");
            Thread bluetoothClientThread = new Thread(new ThreadStart(ClientConnectThread));
        }
        else
        {
            updateUI("Pairing failed.");
        }
    }

    private void ClientConnectThread()
    {
        updateUI("Attempting connect.");
        client.BeginConnect(deviceInfo.DeviceAddress, BluetoothService.SerialPort, new AsyncCallback(BluetoothClientConnectCallback), client);
    }

我尝试重用之前用来扫描设备的线程,并将BeginConnect粘贴到那里,但这只会导致程序崩溃。我不确定它会显示什么错误,因为我是在我的PC上对它进行编程,但只能使用.exe文件在另一台笔记本电脑上测试该程序。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-01 05:23:30

您已经创建了一个线程,但您还没有要求它启动:

代码语言:javascript
复制
Thread bluetoothClientThread = new Thread(new ThreadStart(ClientConnectThread));
bluetoothClientThread.Start(); // <--- this starts the thread

See here for details

当然,您还会遇到另一个问题,因为您调用BeginConnect (异步的),然后函数结束(线程也是如此)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50632471

复制
相关文章

相似问题

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