title: Data export form occasionally freezes during/after file opens
body: <p>I have data logging software that freezes on some machines during/after the export process. During normal use the main form is logging data. If the user wants to export that data to a spread sheet they follow steps to open a new form. This form also holds focus until the form is closed. The new form has options to select which set of data to be exported. Upon selecting a data set the user hits the export button. The code is long but I'll try to summarize.</p>
  
  <pre><code>Private Sub Exoprt_btn_Click(ByVal...)
  
  Dim fs As FileStream = Nothing
  Dim fw As StreamWriter = Nothing
  Dim ...
  </code></pre>
  
  <p>After this SQL stuff happens to fill out the file. This seems to work correctly because the file always opens and is filled properly.</p>
  
  <p>At the end of the code we have</p>
  
  <pre><code>fw.Close()
  fs.Close()
  fw.Dispose()
  fs.Dispose()
  System.Diagnostics.Process.Start(FileName)
  </code></pre>
  
  <p>At this point, when the code works the spreadsheet opens and the user can save or close as they see fit. After dealing with the new file they can return to the pop-up window, select new data to export or close the window and work with the main form.</p>
  
  <p>But when the software freezes, upon returning to the data logging program the data export form is usually blank or sometimes objects like buttons and labels are partially loaded. The export window still holds focus but the main form is still updating as new data is logged. Trying to close the export window results in a windows not responding dialogue. Restarting the computer reveals that everything in the background was working as intended except the window's features could not be interacted with.</p>
  
  <p>I tried to identify the malicious code by writing in Sleep periods and a text message.</p>
  
  <pre><code>fw.Close()
  label1.text = "fw.Close()"
  System.Threading.Thread.Sleep(3000)
  fs.Close()
  label1.text = "fs.Close()"
  System.Threading.Thread.Sleep(3000)
  ...
  System.Diagnostics.Process.Start(Filename)
  label1.text = "Open File"
  System.Threading.Thread.Sleep(3000)
  </code></pre>
  
  <p>The result was that after closing Excel the user only saw "Open File" if the program didn't freeze and "Label1" if it did.</p>
  
  <p>I did some research and ran across some thoughts about threading where this set of code was not being interthreaded properly since the main window is threaded and doing its own thing. I am not sure why some computers run properly and some seem to lock up consistantly.</p>
  
  <p>More experiments:</p>
  
  <p>1) Just opening the Export window can cause the window to freeze while it is logging. Upon opening the export window the program populates a datagridview-like object from SQL.</p>
  
  <p>2) I can use another instance of the logging software to grab data while the original instance is logging. This still causes the main window of the primary instance to lock up.</p>
  
  <p>3) One computer that used to exhibit the symptoms seems to have been excorcised of its demons.</p>