gpt4 book ai didi

c++ - 如何将元素添加到从另一个线程绑定(bind)到 XAML 的 IVector

转载 作者:行者123 更新时间:2023-11-28 04:46:06 26 4
gpt4 key购买 nike

我目前正在 Visual Studio Community 2017 for Windows 10 版本 10.0.16299.0 下开发 Windows 10 UWP C++ 项目。

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

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

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

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

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

//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 而它绑定(bind)到 xaml;要更改 UI,您需要在 UI 线程中。使用Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync进入UI线程,然后添加到集合

关于c++ - 如何将元素添加到从另一个线程绑定(bind)到 XAML 的 IVector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49246961/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com