加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C# – 循环foreach直到true

发布时间:2020-12-16 01:58:34 所属栏目:百科 来源:网络整理
导读:C#的新手.但由于工作环境,我必须“随时学习”. 过去两天我的代码一直在苦苦挣扎,我在这里消耗了尽可能多的问题和MSDN上的文章,但我认为他们让我更加困惑. 我使用我的代码启动应用程序A.应用程序A启动应用程序B(我无法启动应用程序B,我超越了它). 我想用我的
C#的新手.但由于工作环境,我必须“随时学习”.

过去两天我的代码一直在苦苦挣扎,我在这里消耗了尽可能多的问题和MSDN上的文章,但我认为他们让我更加困惑.

我使用我的代码启动应用程序A.应用程序A启动应用程序B(我无法启动应用程序B,我超越了它).
我想用我的代码做的是当应用程序B的MainWindowTitle可用时,隐藏窗口.

到目前为止,我只能用Thread.Sleep(xxx)完成这个;在你看到下面的代码之前.
我想避免使用计时器.

我想要做的是循环下面的代码,直到它是真的.

When app A launches app B,it takes a few seconds for the MainWindowTitle to become available. But the code runs so fast that it’s not available yet and the code is done.

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循环.

  • Outside the ‘foreach’
  • Outside the ‘if’
  • Around the ‘foreach’ (that locked up the system really quickly…) hah!
  • Around the ‘if’

我觉得以下其中一个应该可行,但它没有,或者我完全搞砸了.

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...

有人有什么建议吗?我的大脑是炒鸡蛋,这是我完成这个应用程序所需的最后一点.
如果我唯一的选择是IS Thread.Sleep(),那就这样吧,但我宁愿不使用它.
最后一件事:我必须以.net 2.0为目标.

非常感谢你!

解决方法

您使用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);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读