ShellExecuteHelper executeHelper = new ShellExecuteHelper(&shellExecuteInfo);
if (!executeHelper.ShellExecuteOnSTAThread())
{
... // 忽略其他代码
}
... // 忽略其他代码
}
... // 忽略其他代码
}
复制代码
可以看到在 StartWithShellExecuteEx 里使用的是 ShellExecuteHelper 辅助方法来实现,通过 ShellExecuteOnSTAThread 也能猜到,这是在 STA 线程执行的。这是因为启动线程如果是用来调用文件打开,一些 COM 是需要 STA 线程的。然而如果当前的线程不是 STA 的线程,那需要如何执行
接下来继续看 ShellExecuteOnSTAThread 的实现
internal unsafe class ShellExecuteHelper
{
... // 忽略其他代码
public bool ShellExecuteOnSTAThread()
{
// ShellExecute() requires STA in order to work correctly.
if (Thread.CurrentThread.GetApartmentState() != ApartmentState.STA)
{
ThreadStart threadStart = new ThreadStart(ShellExecuteFunction);