@@ -1501,8 +1501,14 @@ public static void SetTestHook(string property, bool value)
1501
1501
/// </summary>
1502
1502
internal class ShellExecuteHelper
1503
1503
{
1504
- private static bool _succeeded ;
1505
- private static int _errorCode ;
1504
+ private NativeMethods . ShellExecuteInfo _executeInfo ;
1505
+ private int _errorCode ;
1506
+ private bool _succeeded ;
1507
+
1508
+ /// <summary>
1509
+ /// Constructor for ShellExecuteHelper
1510
+ /// </summary>
1511
+ private ShellExecuteHelper ( NativeMethods . ShellExecuteInfo executeInfo ) { _executeInfo = executeInfo ; }
1506
1512
1507
1513
8000
/// <summary>
1508
1514
/// Start a process using ShellExecuteEx with default settings about WindowStyle and Verb.
@@ -1549,28 +1555,16 @@ internal static Process Start(ProcessStartInfo startInfo, ProcessWindowStyle win
1549
1555
shellExecuteInfo . lpDirectory = Marshal . StringToHGlobalUni ( startInfo . WorkingDirectory ) ;
1550
1556
1551
1557
shellExecuteInfo . fMask |= NativeMethods . SEE_MASK_FLAG_DDEWAIT ;
1552
- if ( Thread . CurrentThread . GetApartmentState ( ) == System . Threading . ApartmentState . STA )
1558
+ ShellExecuteHelper helper = new ShellExecuteHelper ( shellExecuteInfo ) ;
1559
+ if ( ! helper . ExecuteOnSTAThread ( ) )
1553
1560
{
1554
- InvokeShellExecuteEx ( shellExecuteInfo ) ;
1555
- }
1556
- else
1557
- {
1558
- ParameterizedThreadStart threadStart = new ParameterizedThreadStart ( InvokeShellExecuteEx ) ;
1559
- Thread thread = new Thread ( threadStart ) ;
1560
- thread . SetApartmentState ( System . Threading . ApartmentState . STA ) ;
1561
- thread . Start ( shellExecuteInfo ) ;
1562
- thread . Join ( ) ;
1563
- }
1564
-
1565
- if ( ! _succeeded )
1566
- {
1567
- if ( _errorCode == NativeMethods . ERROR_BAD_EXE_FORMAT || _errorCode == NativeMethods . ERROR_EXE_MACHINE_TYPE_MISMATCH )
1561
+ if ( helper . ErrorCode == NativeMethods . ERROR_BAD_EXE_FORMAT || helper . ErrorCode == NativeMethods . ERROR_EXE_MACHINE_TYPE_MISMATCH )
1568
1562
{
1569
- throw new Win32Exception ( _errorCode , "InvalidApplication" ) ;
1563
+ throw new Win32Exception ( helper . ErrorCode , "InvalidApplication" ) ;
1570
1564
}
1571
1565
else
1572
1566
{
1573
- throw new Win32Exception ( _errorCode ) ;
1567
+ throw new Win32Exception ( helper . ErrorCode ) ;
1574
1568
}
1575
1569
}
1576
1570
}
@@ -1597,16 +1591,13 @@ internal static Process Start(ProcessStartInfo startInfo, ProcessWindowStyle win
1597
1591
return processToReturn ;
1598
1592
}
1599
1593
1600
- private static void InvokeShellExecuteEx ( object param )
1601
- {
1602
8000
- NativeMethods . ShellExecuteInfo shellExecuteInfo = ( NativeMethods . ShellExecuteInfo ) param ;
1603
- _succeeded = true ;
1604
- if ( ! NativeMethods . ShellExecuteEx ( shellExecuteInfo ) )
1594
+ private void ShellExecuteFunction ( ) {
1595
+ if ( ! NativeMethods . ShellExecuteEx ( _executeInfo ) )
1605
1596
{
1606
1597
_errorCode = Marshal . GetLastWin32Error ( ) ;
1607
1598
if ( _errorCode == 0 )
1608
1599
{
1609
- switch ( ( long ) shellExecuteInfo . hInstApp )
1600
+ switch ( ( long ) _executeInfo . hInstApp )
1610
1601
{
1611
1602
case NativeMethods . SE_ERR_FNF : _errorCode = NativeMethods . ERROR_FILE_NOT_FOUND ; break ;
1612
1603
case NativeMethods . SE_ERR_PNF : _errorCode = NativeMethods . ERROR_PATH_NOT_FOUND ; break ;
@@ -1618,13 +1609,38 @@ private static void InvokeShellExecuteEx(object param)
1618
1609
case NativeMethods . SE_ERR_SHARE : _errorCode = NativeMethods . ERROR_SHARING_VIOLATION ; break ;
1619
1610
case NativeMethods . SE_ERR_NOASSOC : _errorCode = NativeMethods . ERROR_NO_ASSOCIATION ; break ;
1620
1611
case NativeMethods . SE_ERR_DLLNOTFOUND : _errorCode = NativeMethods . ERROR_DLL_NOT_FOUND ; break ;
1621
- default : _errorCode = ( int ) shellExecuteInfo . hInstApp ; break ;
1612
+ default : _errorCode = ( int ) _executeInfo . hInstApp ; break ;
1622
1613
}
1623
1614
}
1624
1615
_succeeded = false ;
1625
1616
}
1626
1617
}
1627
1618
1619
+ private bool ExecuteOnSTAThread ( )
1620
+ {
1621
+ if ( Thread . CurrentThread . GetApartmentState ( ) != System . Threading . ApartmentState . STA )
1622
+ {
1623
+ ThreadStart threadStart = new ThreadStart ( this . ShellExecuteFunction ) ;
1624
+ Thread thread = new Thread ( threadStart ) ;
1625
+ thread . SetApartmentState ( System . Threading . ApartmentState . STA ) ;
1626
+ thread . Start ( ) ;
1627
+ thread . Join ( ) ;
1628
+ }
1629
+ else
1630
+ {
1631
+ ShellExecuteFunction ( ) ;
1632
+ }
1633
+ return _succeeded ;
1634
+ }
1635
+
1636
+ private int ErrorCode
1637
+ {
1638
+ get
1639
+ {
1640
+ return _errorCode ;
1641
+ }
1642
+ }
1643
+
1628
1644
private static int GetProcessIdFromHandle ( SafeProcessHandle processHandle )
1629
1645
{
1630
1646
NativeMethods . NtProcessBasicInfo info = new NativeMethods . NtProcessBasicInfo ( ) ;
0 commit comments