@@ -35,13 +35,36 @@ public static void JumpRegEdit(string regPath, string valueName = null, bool mor
3535 //注册表编辑器窗口多开
3636 process = Process . Start ( "regedit.exe" , "-m" ) ;
3737 process . WaitForInputIdle ( ) ;
38- hMain = process . MainWindowHandle ;
38+
39+ // 等待主窗口句柄可用,最多等待5秒
40+ int retries = 50 ;
41+ while ( retries -- > 0 )
42+ {
43+ process . Refresh ( ) ;
44+ hMain = process . MainWindowHandle ;
45+ if ( hMain != IntPtr . Zero ) break ;
46+ Thread . Sleep ( 100 ) ;
47+ }
48+
49+ if ( hMain == IntPtr . Zero ) return ;
3950 }
4051
4152 ShowWindowAsync ( hMain , SW_SHOWNORMAL ) ;
4253 SetForegroundWindow ( hMain ) ;
43- var hTree = FindWindowEx ( hMain , IntPtr . Zero , "SysTreeView32" , null ) ;
44- var hList = FindWindowEx ( hMain , IntPtr . Zero , "SysListView32" , null ) ;
54+
55+ // 等待树视图和列表视图控件就绪,最多等待5秒
56+ IntPtr hTree = IntPtr . Zero ;
57+ IntPtr hList = IntPtr . Zero ;
58+ int retries2 = 50 ;
59+ while ( retries2 -- > 0 )
60+ {
61+ hTree = FindWindowEx ( hMain , IntPtr . Zero , "SysTreeView32" , null ) ;
62+ hList = FindWindowEx ( hMain , IntPtr . Zero , "SysListView32" , null ) ;
63+ if ( hTree != IntPtr . Zero && hList != IntPtr . Zero ) break ;
64+ Thread . Sleep ( 100 ) ;
65+ }
66+
67+ if ( hTree == IntPtr . Zero || hList == IntPtr . Zero ) return ;
4568
4669 SetForegroundWindow ( hTree ) ;
4770 SetFocus ( hTree ) ;
0 commit comments