Results 1 to 6 of 6

Thread: FindWindow

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2008
    Posts
    200

    FindWindow

    Dear All,

    I am using VB Express 2008, on Windows XP Pro. I have written a programme which tests another programme which compares prices and terms from several vendors. This Bid Tabulation programme, compares prices and terms of up to 10 Bidders and 500 line items in mixed currencies.

    The testing programme, initially, would not work and I discovered that I needed to introduce a delay in order for it to run the Bid Tabulation programme. So my initial code, using the delay, was:

    Code:
    Process.Start("E:\NewFiles\BidTab\BidTab\bin\Release\BidTab.exe")
              
              TimeDelay()
              
              ParentHWND = FindWindow("WindowsForms10.Window.8.app.0.378734a", "Bid Tab Main Programme")
              If ParentHWND <> IntPtr.Zero Then
                   ChildHWND = FindWindowEx(ParentHWND, IntPtr.Zero, "WindowsForms10.BUTTON.app.0.378734a", "Enter")
                   For x = 1 To 3
                        ChildHWND = FindWindowEx(ParentHWND, ChildHWND, "WindowsForms10.BUTTON.app.0.378734a", "Enter")
                   Next
                   PostMessage(ChildHWND, BM_CLICK, 0, 0)
              End If
    And the button was clicked, and the Bid Tabulation programme started and ran successfully, as long as the number of vendors was 5 and the number of line items was 5. when I tried to increase the number of vendors to 10, the programme failed.

    I then discovered that at the point of failure, when the particular ParentHWND=0, if I introduced:

    Code:
    For i = 1 To 10
    TimeDelay()
    Next
    It worked again. However, when I tried to run 10 vendors and 30 line items, once more it failed. I could increase the numbers slightly by modifying the time delays, but even when I went to:

    Code:
    For i = 1 To 40
    TimeDelay()
    Next
    I still could not get the programme to successfully run.

    TimeDelay() is a simple:

    Code:
    Private Sub TimeDelay()
    
              For x = 1 To 10000
                   For y = 1 To 10000
    
                   Next
              Next
    
    End Sub
    Does anyone know what I am doing wrong, or whether there is another way to assure that ParentHWND will not equal zero without using a time delay?

    Regards,

    john

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: FindWindow

    In order to get the window handle, it must have been created. If you call FindWindow before that window handle has been created then it will return zero.

    First up, if you want to a time delay, you don't use a "busy waiting" loop like that. That loop is going to soak up most of the processor cycles available so you are actually making the system slower, so it takes longer to start the other application. If you want a time delay then you actually stop processing for a period of time, which you do by calling Thread.Sleep. That will tell the system not to process your thread at all for a particular number of milliseconds.

    That said, that's not a good idea either. You don't know how long it will take the system to run that app so you will have to make the user wait longer than is actually necessary. You also run the risk of missing the window on some systems if your period is too short.

    What you should be doing as a first option is using the WaitForInputIdle method of the Process object you're creating. It won't work for all applications but those with forms are likely to be OK. That will stall your app until the new process reports that it can receive input.

    At that point you should be guaranteed of the main window handle being available. You don't need to use the FindWindow API to get it either, because the Process object has a MainWindowHandle property. That assumes that it is the process' main window you want the handle of. You'll still need to use FindWindowEx for the child controls too.

    With all that in mind, your code should resemble this:
    vb.net Code:
    1. Using p = Process.Start("notepad")
    2.     p.WaitForInputIdle()
    3.  
    4.     Dim h = p.MainWindowHandle
    5.  
    6.     MessageBox.Show(h.ToString())
    7.  
    8.     '...
    9. End Using
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: FindWindow

    Can't you just use the MainWindowHandle of that process?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 2008
    Posts
    200

    Re: FindWindow

    jmcilhinney,

    you appear to answer many of my questions; thank you.

    i will try your suggestion, as there are about 30 times that it has wait for ParentHWND not to equal zero.


    dee-u,

    thank you, but due to the number of ParentHWND used, i believe that jmcilhinney's answer is what i need.

    thank you both,

    regards,

    john

  5. #5
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: FindWindow

    Ooopps, too late. =)
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jul 2008
    Posts
    200

    Re: FindWindow

    Dear jmcilhinney,

    Please confirm that I am using this correctly, because it still indicates “0”.

    Code:
    Dim prcProcess As Process = Process.Start("E:\jmtb\Programmes\VB2008\NewFiles\BidTab\BidTab\bin\Release\BidTab.exe")
    
              Using (prcProcess)
                   prcProcess.WaitForInputIdle()
                   Dim h = prcProcess.MainWindowHandle
                   MessageBox.Show(h.ToString())
              End Using
    but if i insert a delay again it works.

    Regards,

    john

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width