如何将元素添加到从另一个线程绑定到 XAML 的 IVector

How to add Elements to IVector which is bound to XAML from another thread

我目前正在为 Windows 10 版本 10.0.16299.0.Visual Studio Community 2017 下的 Windows 10 UWP C++ 项目工作。

以下过程:我的计算机上有一个 TCP 服务器 运行,它会在某些事件中向连接的客户端发送消息。我正在处理的项目现在有我的 TCPClient,它接收消息并应在 UI.

中显示值

在 MainPage 构造函数中,我创建了一个调用我的 "clientThread" 函数的 std:: 线程。在这个函数中,我实例化了我的客户端 class 的一个对象。一旦连接到服务器,我就会创建一个新的 std:: 线程来执行无限循环。这个循环调用 "processPacketType" 函数,它只是调用一个开关并询问接收到的数据包是否是 ChatMessage 数据包。如果是,则检索消息。

现在我的问题是:MainPage class 有一个来自我创建的 class 的私有 AudioSessionViewModel 对象,returns 一个 IVector^。现在我想用收到的消息扩展 Vector。使用我的 "processPacketType" 函数,我只想实例化我的 AudioSession class 的新对象并将其添加到矢量(我将其作为参考从一个线程传递到另一个线程)。但是,我收到以下错误消息:SoundManager - Client 中的 0x778B08C2 引发异常。 exe:Microsoft C++ 异常:Platform::InvalidCastException ^ 用于存储位置 0x0F15EC3C。 HRESULT: 0x80004002 接口不受支持 WinRT 信息:不支持的接口

有趣的是:我的 processPacketType 函数是从我的无限循环中调用的。如果我将新创建的对象传递给循环外的向量,它就可以工作。但是如果我在循环中自己做,就会出现错误信息。

这个问题真的很难解释,但我希望你明白我的需要。

//MainPage.xaml.h
namespace SoundManager___Client {
    public ref class MainPage sealed {
    public:
        MainPage();

        property AudioSessionViewModel^ ViewModel {
            AudioSessionViewModel^ get() { return this->viewModel; };
        }

    private:
        AudioSessionViewModel^ viewModel;
    };
}

//MainPage.xaml.cpp
#include "pch.h"
#include "MainPage.xaml.h"

using namespace SoundManager___Client;

using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;

void createClient(AudioSessionViewModel^ audioSessionViewModel);

MainPage::MainPage() {
    InitializeComponent();

    this->viewModel = ref new AudioSessionViewModel();

    std::thread client(createClient, this->viewModel);
    client.detach();
}

void createClient(AudioSessionViewModel^ audioSessionViewModel) {
    Client myClient("127.0.0.1", 1111, audioSessionViewModel);
    myClient.Connect();

    std::string buffer;

    while (true) {
        Sleep(10000);
    }
}

namespace SoundManager___Client {
    bool Client::processPacketType(const PacketType _packetType, AudioSessionViewModel^ audioSessionViewModel) {
        switch (_packetType) {
        case PacketType::ChatMessage: {
            std::string message;
            if (!getString(message))
                return false;

            OutputDebugStringA((LPCSTR)message.c_str());
            OutputDebugString(L"\n");
            audioSessionViewModel->AudioSessions->Append(ref new AudioSession(L"Test", L"20")); //<--------- Here I get the error

            break;
        }
        default:
            OutputDebugString(L"Unrecognized PacketType.\n");
            break;
        }
        return true;
    }

    void Client::clientThread(Client &_client, AudioSessionViewModel^ audioSessionViewModel) {
        PacketType packetType;
        //If I put the code to add the element over here, it works. But it doesn't inside the while loop or in the processPacketType which get called inside the loop
        while (true) {
            if (_client.mTerminateThreads == true)
                break;

            if (!_client.getPacketType(packetType))
                break;

            if (!_client.processPacketType(packetType, audioSessionViewModel))
                break;
        }

        OutputDebugString(L"Lost connection to the server.\n");
        _client.mTerminateThreads = true;
        if (!_client.closeConnection())
            OutputDebugString(L"Socket to the server was closed successfully.\n");
        else
            OutputDebugString(L"Socket was not able to be closed.\n");
    }

#pragma once

#include <sstream>

namespace SoundManager___Client {
    public ref class AudioSession sealed {
    private:
        Platform::String^ sessionName;
        Platform::String^ sessionVolume;

    public:
        AudioSession(Platform::String^ sessionName, Platform::String^ sessionVolume) : sessionName{ sessionName }, sessionVolume{ sessionVolume } { }

        property Platform::String^ SessionName {
            Platform::String^ get() { return this->sessionName; }
        }
        property Platform::String^ SessionVolume {
            Platform::String^ get() { return this->sessionVolume; }
        }
        property Platform::String^ OneLineSummary {
            Platform::String^ get() {
                std::wstringstream wstringstream;
                wstringstream << this->SessionName->Data();
                wstringstream << this->SessionVolume->Data();
                return ref new Platform::String(wstringstream.str().c_str());
            }
        }
    };

    public ref class AudioSessionViewModel sealed {
    private:
        Windows::Foundation::Collections::IVector<AudioSession^>^ audioSessions;

    public:
        AudioSessionViewModel() {
            this->audioSessions = ref new Platform::Collections::Vector<AudioSession^>();

            AudioSession^ audioSession = ref new AudioSession(L"MASTER", L"100");
            this->audioSessions->Append(audioSession);
            audioSession = ref new AudioSession(L"CHROME", L"50");
            this->audioSessions->Append(audioSession);
        }

        property Windows::Foundation::Collections::IVector<AudioSession^>^ AudioSessions {
            Windows::Foundation::Collections::IVector<AudioSession^>^ get() { return this->audioSessions; }
        }
    };
}

解决方案更新: 我终于想出了如何在 C++/CX 中调用调度程序:

Windows::ApplicationModel::Core::CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, ref new Windows::UI::Core::DispatchedHandler([this]() {

}));

现在我终于明白为什么我的调用有效了,但只是在循环之外:

我至少假设来自 std:: 线程的调用在 UI 线程中执行第一次执行,并且只有当它到达 .detach() 时,代码才会在新线程中执行。如有错误,请在评论中指正!

我认为问题在于您从后台线程向 AudioSessions 添加项目,同时它的边界为 xaml;要更改 UI,您需要在 UI 主题中。使用Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync进入UI线程,然后将其添加到collection

用解决方案和解释更新了主要 post!