Open File Dialog Momentary Freeze

angbermu

New member
Joined
Jul 8, 2006
Messages
2
Programming Experience
Beginner
[Resolved] Open File Dialog Momentary Freeze

Hi to all:

I am working in a VB.net little project, this program take an String or a file to get the hash of it, a use Cryptography class from dotnet, everything looks going fine and I can get the hash from both inputs, but if i try to get the has from a big file (200+ MB) the open file dialog get freeze for a moment, this for already a 30 seconds, I just want to go back to the main windows and advise the user that the Application is calculating the hash, I tried to use DoEvents but I cannot figure out how do this. Can help Me?

Attached the project...

Thanks in advance

Angel
 

Attachments

  • md5.zip
    39.4 KB · Views: 18
Last edited:
Are you running in the IDE because things take longer this way.

Try it using the exe that automatically created each time you
run/start debug.

the exe is located in the bin directory of your project - but you
probably already know all this.
 
It's hashing of the 200+MB file that takes that long, it's got nothing to do with OpenFileDialog. You have to run the hashing in in a separate thread. Here is an example with delegate and callback, I've taken your openFiledial_FileOk and changed it, then added the necessary delegates and methods. There's not much comments and this is a bit advanced, but a lot of documentation exist both with you local help and online at MSDN and other resources. Working with asynchronous methods also allow you to first select a large file, dialog immediately returns, then select a small file. And guess what, the small file finishes first!
VB.NET:
[COLOR=blue]Private Sub[/COLOR] openFiledial_FileOk([COLOR=blue]ByVal[/COLOR] sender [COLOR=blue]As [/COLOR]System.Object, [COLOR=blue]ByVal[/COLOR] e [COLOR=blue]As[/COLOR] System.ComponentModel.CancelEventArgs) _
[COLOR=blue]Handles[/COLOR] openFiledial.FileOk
  [COLOR=blue]Dim[/COLOR] deleg [COLOR=blue]As New[/COLOR] dlgFilehasher([COLOR=blue]AddressOf[/COLOR] FileHasher)
  [COLOR=blue]Dim[/COLOR] callback [COLOR=blue]As New[/COLOR] AsyncCallback([COLOR=blue]AddressOf[/COLOR] ProcessComplete)
  deleg.BeginInvoke(openFiledial.FileName, cmbAlg.SelectedItem, callback, openFiledial.FileName)
[COLOR=blue]End Sub[/COLOR]
 
[COLOR=blue]Private Delegate Function[/COLOR] dlgFilehasher([COLOR=blue]ByVal[/COLOR] filename [COLOR=blue]As String[/COLOR], [COLOR=blue]ByVal[/COLOR] alg [COLOR=blue]As String[/COLOR]) [COLOR=blue]As String[/COLOR]
 
[COLOR=blue]Private Sub[/COLOR] ProcessComplete([COLOR=blue]ByVal[/COLOR] ar [COLOR=blue]As[/COLOR] System.IAsyncResult)
[COLOR=blue]  Dim[/COLOR] aResult [COLOR=blue]As[/COLOR] System.Runtime.Remoting.Messaging.AsyncResult = _
    [COLOR=blue]CType[/COLOR](ar, System.Runtime.Remoting.Messaging.AsyncResult)
[COLOR=darkgreen]  'get the delegate method[/COLOR]
  [COLOR=blue]Dim[/COLOR] temp [COLOR=blue]As[/COLOR] dlgFilehasher = [COLOR=blue]CType[/COLOR](aResult.AsyncDelegate, dlgFilehasher)
[COLOR=darkgreen]  'get the String result with EndInvoke[/COLOR]
[COLOR=black]  updateUI(temp.EndInvoke(ar))[/COLOR]
[COLOR=darkgreen]  'example of getting something from the AsyncState object.[/COLOR]
[COLOR=darkgreen]  'this object is not available to the actual process and can't be changed by that process. [/COLOR]
[COLOR=darkgreen]  'if you hash a lot this could be a way to know what calls just finished.[/COLOR]
  [COLOR=blue]Dim[/COLOR] pi [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR] = [COLOR=blue]CType[/COLOR](ar.AsyncState, [COLOR=blue]String[/COLOR])
  MsgBox(pi & " was the hashing process that just finished")
[COLOR=blue]End Sub[/COLOR]
 
[COLOR=blue]Private Delegate Sub[/COLOR] dlgUpdateUI([COLOR=blue]ByVal[/COLOR] text [COLOR=blue]As String[/COLOR])
 
[COLOR=blue]Sub[/COLOR] updateUI([COLOR=blue]ByVal[/COLOR] text [COLOR=blue]As String[/COLOR])
  [COLOR=blue]If[/COLOR] Me.InvokeRequired [COLOR=blue]Then[/COLOR]
    [COLOR=blue]Dim[/COLOR] d [COLOR=blue]As New[/COLOR] dlgUpdateUI([COLOR=blue]AddressOf[/COLOR] updateUI)
    Me.Invoke(d, [COLOR=blue]New[/COLOR] [COLOR=blue]Object[/COLOR]() {text})
  [COLOR=blue]Else[/COLOR]
    txtMD5.Text = text
  [COLOR=blue]End If[/COLOR]
[COLOR=blue]End Sub[/COLOR]
 
Another option instead of a plain delegate with callback is to make that module a class, let it accept parameters through properties, and add an event for callback functionality. Then you create an instance of that class for each call, set the instance properties, and call one of its methods through an instance of Threading.Thread. When you handle the 'finished' event which occurs in context of the thread that raised it, you must also here be equally careful to invoke any object belonging to the main thread like the updateUI example above.
 
Back
Top