仅在 Vista (.net) 中存在问题:从线程调用非托管 dll(Shell32.dll,函数:SHEmptyRecycleBin)
********平台:在 Vista(旗舰版或家庭/高级)中它不起作用,其他操作系统(xp、windows7)它起作用************
我正在清空回收站在线程内使用 c++.net(或 c#.net)。 当我直接这样做(没有线程)时,它就起作用了。 但如果使用线程则不会。 请观看下面的代码片段:
namespace EmptyRecycleBin_C{
enum RecycleFlags
{
SHERB_NOCONFIRMATION = 0x00000001,
SHERB_NOPROGRESSUI = 0x00000002,
SHERB_NOSOUND = 0x00000004
};
public ref class Form1 : public System::Windows::Forms::Form{
[DllImport("Shell32.dll",CharSet=CharSet::Unicode)]
static System::UInt32 SHEmptyRecycleBin(IntPtr hwnd, String^ pszRootPath, RecycleFlags dwFlags);
private: void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
Thread^ th = gcnew System::Threading::Thread(gcnew ThreadStart(this, &Form1::doEmpty));
th->Start();
//this->doEmpty(); // this line works just fine
}
private: void doEmpty()
{
try{
SHEmptyRecycleBin(IntPtr::Zero, String::Empty, RecycleFlags::SHERB_NOCONFIRMATION);
}catch(Exception^ ex)
{Diagnostics::Debug::Write(ex->Message);}
}
};
}
这里有什么问题......?
********Platform: in Vista(ultimate or home/premium) it does not work, other OS(xp, windows7) it works***********
I'm emptying recycle bin using c++.net(or c#.net) inside a thread. When i do this straight (without thread) it works. But if thread used it doesn't. Please watch the code snippet below:
namespace EmptyRecycleBin_C{
enum RecycleFlags
{
SHERB_NOCONFIRMATION = 0x00000001,
SHERB_NOPROGRESSUI = 0x00000002,
SHERB_NOSOUND = 0x00000004
};
public ref class Form1 : public System::Windows::Forms::Form{
[DllImport("Shell32.dll",CharSet=CharSet::Unicode)]
static System::UInt32 SHEmptyRecycleBin(IntPtr hwnd, String^ pszRootPath, RecycleFlags dwFlags);
private: void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
Thread^ th = gcnew System::Threading::Thread(gcnew ThreadStart(this, &Form1::doEmpty));
th->Start();
//this->doEmpty(); // this line works just fine
}
private: void doEmpty()
{
try{
SHEmptyRecycleBin(IntPtr::Zero, String::Empty, RecycleFlags::SHERB_NOCONFIRMATION);
}catch(Exception^ ex)
{Diagnostics::Debug::Write(ex->Message);}
}
};
}
whats the problem here...?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
难道是因为您创建的线程在默认安全上下文中执行,而不是在主线程的安全上下文中执行?
有关提示,请参阅有关 ExecutionContext 的文档。 您可以在线程上设置 ExecutionContext 并重试。
Could it be because threads you create execute in the default security context, not in the security context of the main thread?
See the doc on ExecutionContext for a hint. You can set the ExecutionContext on your thread and re-try.
您是否从线程中调用了 CoInitialize?
它返回什么错误代码?
Have you called CoInitialize from your thread?
What error code is it returning?
Shell 函数仅适用于 STA 线程,.NET 线程默认为 MTA。 您可以将线程设置为使用单单元线程:
Shell functions work with STA threads only, and .NET threads are MTA by default. You can set the thread to use single-apartment threading:
我不知道为什么会发生这种情况,但是您尝试过其他线程方法吗? 例如BackgroundWorker组件或ThreadPool.QueueUserWorkItem? 错误仍然发生吗?
I don't know WHY that's happening, but have you tried other threading methods? Such as a BackgroundWorker component or ThreadPool.QueueUserWorkItem? Does the error still happen?