title: When the file is saved repeatedly using pywin32, the handle of the save window cannot be found after the second or third time
body: <p>When I run the program, I can work well for the first time, but I can 't find the handle of the window for the second or third time I save the file<br />
  I use spy + + to find, only refreshing the process table can find the handle of the new save window<br />
  How does pywin32 need to refresh the process  I can 't find the relevant article<br />
  <strong>main</strong></p>
  <pre><code>sonList = [[&quot;MDIClient&quot;, 0], [&quot;xxgeom5100mdidoc9012&quot;, 0]]
  mainWin = mainRun()
  
  &quot;&quot;&quot;
  Menu -&gt; Edit -&gt; File
  Find the exgeom window
  &quot;&quot;&quot;
  xgeomWin = 0
  checkMenu(mainWin, u&quot;Edit&quot;, [0, 2], level=1)
  xgeomWin = checkWin(xgeomWin, winName=u&quot;xgeom 11.54&quot;)
  win32gui.ShowWindow(xgeomWin, win32con.SW_MAXIMIZE)
  
  # Get the title of .son window
  sonWin = findSubHandle(xgeomWin, sonList)
  sonWin = checkWin(sonWin, winClass=&quot;xxgeom5100mdidoc9012&quot;)
  sonTitle = win32gui.GetWindowText(sonWin)
  win32gui.ShowWindow(sonWin, win32con.SW_MAXIMIZE)
  
  # Change file name
  sonTitle = selfIncreasing(sonTitle)
  # **Problem code**
  saveWin, saveBTN = checkMenu(xgeomWin, &quot;Save As...&quot;, [1])
  editWin = findSubHandle(saveWin, saveList)
  editWin = checkEdit(saveWin, editWin, saveList)
  while getEdit(editWin) != sonTitle:
      print(&quot;Edit Not Changed.&quot;)
      setText(editWin, sonTitle)
      win32api.Sleep(1000)
  
  while win32gui.FindWindow(None,&quot;Save As:&quot;):
      print(&quot;Saving...&quot;)
      shortcutKey(18, 83)
      win32api.Sleep(1000)
  
  </code></pre>
  <p><strong>method</strong></p>
  <pre><code>
  def findMenu(handle, index=0):
      assert type(index) == int and index &gt;= 0
      return win32gui.GetSubMenu(handle, index)
  
  def checkMenu(hwnd, menuName, index, level=0):
      # Problem code
      menu = win32gui.GetMenu(hwnd)
      subMenu = findMenu(menu, index[0])
      if level != 0:
          subMenu = findMenu(subMenu, index[1])
      win, BTN = menuCommand(hwnd, subMenu, menuName)
  
      return win, BTN
  
  def menuCommand(Mhandle, handle, command):
      assert type(command) == str
      command_dict = {
          &quot;Edit&quot;: [2, u&quot;xgeom 11.54&quot;],
          &quot;Pointer&quot;: [0, None],
          &quot;Reshape&quot;:[1,None],
          &quot;Analyze&quot;:[1, None],
          &quot;New Graph&quot;: [1, u&quot;emgraph 11.54&quot;],
          &quot;Save Graph As...&quot;:[4, u&quot;Enter filename for saved graph file:&quot;],
          &quot;Save As...&quot;:[5, u&quot;Save As:&quot;]
  
      }
      cmd_ID = win32gui.GetMenuItemID(handle, command_dict[command][0])
      win32gui.PostMessage(Mhandle, win32con.WM_COMMAND, cmd_ID, 0)
      if command != &quot;Pointer&quot; or &quot;&quot;:
          index = 1
          # Problem code
          while win32gui.FindWindow(None, command_dict[command][1]) == 0:
              print(
                  &quot;Failed to find handle to the &quot; + command +
                  &quot;.  Number of attempts: &quot;, index)
              index += 1
              win32api.Sleep(1000)
  
          dig_handle = win32gui.FindWindow(None, command_dict[command][1])
          confBTN_handle = win32gui.FindWindowEx(dig_handle, 0, &quot;Button&quot;, None)
          return dig_handle, confBTN_handle
  </code></pre>
  <p><strong>source code</strong>
  <a href="https://github.com/HakFoo/auto_sonnet" rel="nofollow noreferrer">enter link description here</a></p>