![](/img/trans.png)
[英]how to configure timer and thread.sleep so that thread.sleep will wait until all the code is executed
[英]Using SendMessage() to minimize all windows, but need a more efficient way than Thread.Sleep() to wait until the legacy desktop is showing
在我的C#控制台應用程序中,我使用SendMessage()
來最小化所有窗口,從而有效地顯示Windows 8 Legacy Desktop。 這很好用,但是我必須先使用Thread.Sleep(1000)
才能等待Legacy Desktop實際顯示,然后再嘗試執行其他操作。
IntPtr lHwnd = FindWindow("Shell_TrayWnd", null);
SendMessage(lHwnd, WM_COMMAND, (IntPtr)MIN_ALL, IntPtr.Zero);
Thread.Sleep(1000);
我真的想用一種更有效的方式替換Thread.Sleep()
,以在繼續之前檢測舊版桌面是否正在顯示。
有任何想法嗎?
編輯:這是Interop包裝器和常量。 以防萬一。
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true)]
static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, IntPtr wParam, IntPtr lParam);
const int WM_COMMAND = 0x111;
const int MIN_ALL = 419;
const int MIN_ALL_UNDO = 416;
我不確定這對您是否會更好,但是也許值得一試...
(1)將對“ Shell32”的引用添加到您的項目中(通過添加引用-> COM-> Microsoft Shell控件和自動化)。
(2)將引用的“嵌入式互操作類型”設置為false
。
(3)使用以下代碼最小化所有窗口:
dynamic shell = new Shell32.ShellClass();
shell.MinimizeAll();
但是,我懷疑這只是執行SendMessage()的另一種方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.