且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 C++ CLI 中将参数传递给线程

更新时间:2022-02-15 23:13:04

从您的代码片段中指出正确的方向并不容易.你需要一个线程对象.

From your code fragment it is not so easy to point in the right direction. You need a thread object.

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 方法不会像我认为的 DriverInfo 那样接受本机 c++ 值.您必须将其转换为真正的 C++/CLI 对象.Hans Passant 再次指出了正确的方向:

As Hans Passant suggest in his comment. The Start method will not accept a native c++ value like I think DriverInfo is. You have to convert it to a real C++/CLI object. And again Hans Passant point into the right direction:

ref class mywrapwstring
{
 public:
  mywrapwstring(std::wstring str) :  str(new std::wstring(str)) {}
  !mywrapwstring() :  { delete std::string(str); }
  std::wstring *str;
};

和魔法"调用:

m_Thread->Start(gcnew mywrapwstring(ittr->drive + L"*") ); 

和线程方法更好:

void FileIndex::Diving(mywrapwstring ^ data)
{
 // do smth.
 // any recursion 
 data->str; // here is your string
}