【发布时间】:2013-12-05 09:58:10
【问题描述】:
C# 新手。但由于工作环境的原因,我必须“在飞行中学习”。
过去 2 天一直在为我的代码苦苦挣扎,我在这里尽可能多地提出问题和 MSDN 上的文章,但我认为它们让我更加困惑。
我使用我的代码启动应用 A。应用 A 启动应用 B(我无法启动应用 B,我已超出此范围)。
我想用我的代码做的是当应用 B 的 MainWindowTitle 可用时,隐藏窗口。
到目前为止,我只能通过Thread.Sleep(xxx) 完成此操作;在您在下面看到的代码之前。
我想避免使用计时器。
我要做的是循环下面的代码,直到它为真。
当应用 A 启动应用 B 时,MainWindowTitle 需要几秒钟才能变为可用。但是代码运行的太快了,还没有到,代码就完成了。
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 循环。
- 在“foreach”之外
- 在“如果”之外
- 围绕“foreach”(锁定系统真的很快...)哈哈!
- 围绕“如果”
我觉得以下其中一项应该可以工作,但它没有,或者我完全搞砸了。
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...
有人有什么建议吗?我的大脑是炒鸡蛋,这是我完成这个应用程序所需的最后一点。
如果我唯一的选择是Thread.Sleep(),那就这样吧,但我宁愿不使用它。
最后一件事:我必须以 .net 2.0 为目标。
谢谢!
【问题讨论】:
-
使用 while 循环。
-
你的标题与问题无关。
标签: c# loops foreach while-loop msdn