C# – 循环foreach直到true
C#的新手.但由于工作环境,我必须“随时学习”.
过去两天我的代码一直在苦苦挣扎,我在这里消耗了尽可能多的问题和MSDN上的文章,但我认为他们让我更加困惑. 我使用我的代码启动应用程序A.应用程序A启动应用程序B(我无法启动应用程序B,我超越了它). 到目前为止,我只能用Thread.Sleep(xxx)完成这个;在你看到下面的代码之前. 我想要做的是循环下面的代码,直到它是真的.
IntPtr hWnd = IntPtr.Zero; foreach (Process procList in Process.GetProcess()) { if (procList.MainWindowTitle.Contains("SAP Logon")) { hWnd = procList.MainWindowHandle; } } ShowWindow(hWnd,0); 该代码仅在我之前使用以下内容时才有效: Thread.Sleep(10000); 在整个代码块之前.它工作的唯一原因是b / c它允许有足够的时间传递窗口打开并包含我正在寻找的标题. 我试过while循环.
我觉得以下其中一个应该可行,但它没有,或者我完全搞砸了. while (!procList.MainWindowTitle.Contains("SAP Logon")) { } // ! at the beginning OR while (procList.MainWindowTitle.Contains("SAP Logon") == null) { } // equaling null OR while (procList.MainWindowTitle.Contains("SAP Logon") < 0) { } // etc.,etc.,while (procList.MainWindowTitle.DOESNOTContain("SAP Logon")) { } // I know this is wrong but it almost seems like what I need... 有人有什么建议吗?我的大脑是炒鸡蛋,这是我完成这个应用程序所需的最后一点. 非常感谢你! 解决方法
您使用while循环的想法应该有效.你可以尝试这样的事情:
IntPtr hWnd = IntPtr.Zero; bool isFound = false; while(!isFound) { foreach (Process procList in Process.GetProcess()) { if (procList.MainWindowTitle.Contains("SAP Logon")) { isFound = true; hWnd = procList.MainWindowHandle; } } Thread.Sleep(100); // You may or may not want this } ShowWindow(hWnd,0); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |