OpenFileDialog question (.Net C.F.)
-
Hi all, I have a little (and I hope simple) problem with using of OpenFileDialog in a .Net C.F. aplication: When I open OpenFileDialog1 to choose the file to open/read, it remains active/visible for all time that the choosen file is reading. And as to reading the file sometime need also 4 minutes, I would like that OpenFileDialog1 remain visible only for time needed to choose the file, and immediately after it disappears to show again the main form. But I don't know to do it (OpenFileDialog1 doesn't accepts .close). Here a piece of my code: Private Sub MenuItem2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MenuItem2.Click openFileDialog1.InitialDirectory = "\My Documents" openFileDialog1.Filter = "wpt files (*.wpt)|*.wpt|txt files (*.txt)|*.txt" openFileDialog1.FilterIndex = 1 If openFileDialog1.ShowDialog() = DialogResult.OK Then myStream = openFileDialog1.FileName NomeFile = myStream Me.Text = "Waypoints Manager - " + myStream Using sr As StreamReader = New StreamReader(myStream) CountLines = 0 Do line = sr.ReadLine() If line = Nothing Then Exit Do CountLines = CountLines + 1 Loop Until line Is Nothing sr.Close() End Using Can someone help me please? Thanks Ignazio
-
Hi all, I have a little (and I hope simple) problem with using of OpenFileDialog in a .Net C.F. aplication: When I open OpenFileDialog1 to choose the file to open/read, it remains active/visible for all time that the choosen file is reading. And as to reading the file sometime need also 4 minutes, I would like that OpenFileDialog1 remain visible only for time needed to choose the file, and immediately after it disappears to show again the main form. But I don't know to do it (OpenFileDialog1 doesn't accepts .close). Here a piece of my code: Private Sub MenuItem2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MenuItem2.Click openFileDialog1.InitialDirectory = "\My Documents" openFileDialog1.Filter = "wpt files (*.wpt)|*.wpt|txt files (*.txt)|*.txt" openFileDialog1.FilterIndex = 1 If openFileDialog1.ShowDialog() = DialogResult.OK Then myStream = openFileDialog1.FileName NomeFile = myStream Me.Text = "Waypoints Manager - " + myStream Using sr As StreamReader = New StreamReader(myStream) CountLines = 0 Do line = sr.ReadLine() If line = Nothing Then Exit Do CountLines = CountLines + 1 Loop Until line Is Nothing sr.Close() End Using Can someone help me please? Thanks Ignazio
Without looking at your code I can tell what's going on since it is such a frequently occurring question. The problem you are encountering is intrinsic to applications with single threads. The OpenFileDialog isn't still active. It is closed. However, your application hasn't redrawn it self so the last image that was on the screen remains visible until it does. Right now you are reading the file on your primary thread, and while that primary thread is busy reading the file it does not have the opportunity to redraw the main window. When the read operation completes then the application can use that thread to redraw the main interface. To prevent that behaviour you will need to read your file on a secondary thread so that the primary thread is still free to redraw the main interface. This concept can be applied to both desktop and mobile applications written with managed and native code. Please read the following for a more complete explanation of the problem you are encountering and the solution. Give Your .NET-based Application a Fast and Responsive UI with Multiple Threads
Joel Ivory Johnson
Meet my dev team: RDA Architecture Evangelist Team Blog
My site: J2i.net
modified on Sunday, December 14, 2008 4:51 PM
-
Without looking at your code I can tell what's going on since it is such a frequently occurring question. The problem you are encountering is intrinsic to applications with single threads. The OpenFileDialog isn't still active. It is closed. However, your application hasn't redrawn it self so the last image that was on the screen remains visible until it does. Right now you are reading the file on your primary thread, and while that primary thread is busy reading the file it does not have the opportunity to redraw the main window. When the read operation completes then the application can use that thread to redraw the main interface. To prevent that behaviour you will need to read your file on a secondary thread so that the primary thread is still free to redraw the main interface. This concept can be applied to both desktop and mobile applications written with managed and native code. Please read the following for a more complete explanation of the problem you are encountering and the solution. Give Your .NET-based Application a Fast and Responsive UI with Multiple Threads
Joel Ivory Johnson
Meet my dev team: RDA Architecture Evangelist Team Blog
My site: J2i.net
modified on Sunday, December 14, 2008 4:51 PM
Thank you very much Joel! Your posts are precise and reliable as usual. I will follow your advice. But While I was trying to resolve the problem, I made this simply modify to the code: I placed the OpenFileDialog1 code and the file reading code in 2 different Sub, and in the first line of reading Sub I added ME.Refresh() statement. It seems that this modify works and the OpenFileDialog1 disappears before the file is read (as I desired). Ignazio
modified on Monday, December 15, 2008 4:35 PM