.net 将参数传递给C+中的线程+;CLI
我搜索了每一个主题,以正确创建带有参数(wstring)的新线程,但没有任何效果。我怎样才能解决我的问题? 我为我的.Net UI应用程序创建了这个项目,所以之前我使用了std::thread和std::mutex,但VSC++表单中的“惊人的”.Net不支持它.net 将参数传递给C+中的线程+;CLI,.net,multithreading,visual-c++,c++-cli,.net,Multithreading,Visual C++,C++ Cli,我搜索了每一个主题,以正确创建带有参数(wstring)的新线程,但没有任何效果。我怎样才能解决我的问题? 我为我的.Net UI应用程序创建了这个项目,所以之前我使用了std::thread和std::mutex,但VSC++表单中的“惊人的”.Net不支持它 namespace indx { ref class FileIndex { public: FileIndex(); FileIndex(FileIndex ^); virtual ~FileIndex();
namespace indx
{
ref class FileIndex
{
public:
FileIndex();
FileIndex(FileIndex ^);
virtual ~FileIndex();
// func
void getDrives();
void Diving(const wstring &);
void Processing();
};
void FileIndex::Diving(Object^ data)
{
// do smth.
// any recursion
}
void FileIndex::Processing()
{
vector<DriveInfo>::iterator ittr = LDrivers->begin();
for(counter = 0; ittr != LDrivers->end(); ittr++)
{
if(ittr->type == L"Fixed" || ittr->type == L"Removable")
{
// need new thread(&FileIndex::Diving, this, (ittr->drive + L"*"));
// argument - ittr->drive + L"*";
}
}
// join
}
namespace-indx
{
ref类文件索引
{
公众:
FileIndex();
文件索引(文件索引^);
virtual~FileIndex();
//func
void getDrives();
无效潜水(持续潜水&);
无效处理();
};
void FileIndex::Diving(对象^data)
{
//做smth。
//任何递归
}
void FileIndex::Processing()
{
向量::迭代器ittr=LDrivers->begin();
对于(计数器=0;ittr!=LDrivers->end();ittr++)
{
如果(ittr->类型==L“固定”| | ittr->类型==L“可移动”)
{
//需要新线程(&FileIndex::Diving,this,(ittr->drive+L“*”);
//参数-ittr->drive+L“*”;
}
}
//加入
}
从代码片段中,指向正确的方向并不容易。您需要一个线程对象
using namespace System::Threading;
线程对象:
Thread ^m_Thread;
现在需要的线路是:
m_Thread = gcnew Thread(gcnew ParameterizedThreadStart(this,
&FileIndex::Diving));
m_Thread->Start(ittr->drive + L"*");
正如Hans Passant在他的评论中所建议的。start方法将不接受一个原生C++值,就像我认为的,代码> DRIVILFIONE/COD>是。你必须将它转换成一个真正的C++/CLI对象。 汉斯·帕桑再次指出了正确的方向:
ref class mywrapwstring
{
public:
mywrapwstring(std::wstring str) : str(new std::wstring(str)) {}
!mywrapwstring() : { delete std::string(str); }
std::wstring *str;
};
还有“神奇”的召唤:
而线程方法更像:
void FileIndex::Diving(mywrapwstring ^ data)
{
// do smth.
// any recursion
data->str; // here is your string
}
std::wstring不是一个.NET类,也不是从System::Object派生的。使用它没有任何意义,而是使用字符串“^”。矢量代码看起来也不健康。我有一个非常庞大的函数列表,可以使用everywere为驱动器和带有WinApi函数的std::wstring编制索引,所以我无法更改它。它是非常重要的部分。DriveInfo it I这是一个包含一些wstring和size\t变量的结构。什么是替换“struct”的本机C++/CLI对象?@ArsenBoykanych您可以在包装类中传递整个结构,而不是将std::wstring或事件作为第二个属性一起传递。无论如何,我更喜欢这样。它必须是wstring*,并且必须实现终结器才能销毁它。ref类mywrapwstring{public:mywrapwstring(std::wstring str):str(str){}std::wstring str;wstring不支持托管类类型。@HansPassant老实说,这是两年多前我构建的最后一个C++/CLI程序。你当然是对的。
void FileIndex::Diving(mywrapwstring ^ data)
{
// do smth.
// any recursion
data->str; // here is your string
}