locked
Making a background process RRS feed

  • Question

  • User-605707711 posted

     Hello, I'm quite new to software developing (I'm used to web dev) 

    I tried to make process that monitores file for changes , makes its md5 hash and outputs.

     

    Dim f As FileStream = New FileStream(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite, 8192)
    Dim md5 As MD5CryptoServiceProvider = New MD5CryptoServiceProvider
    md5.ComputeHash(f)
    Dim hash As Byte() = md5.Hash
    Dim buff As StringBuilder = New StringBuilder
    Dim hashByte As Byte
    For Each hashByte In hash
    buff.Append(String.Format("{0:X1}", hashByte))
    Next
    Label1.Text = buff.ToString()
    System.Threading.Thread.Sleep(4000)
    f.Close()

     


    This works perfectly.

    but when i want to read changes all the time

    Dim f As FileStream = New FileStream(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite, 8192)
    Dim md5 As MD5CryptoServiceProvider = New MD5CryptoServiceProvider
    Do
    md5.ComputeHash(f)
    Dim hash As Byte() = md5.Hash
    Dim buff As StringBuilder = New StringBuilder
    Dim hashByte As Byte
    For Each hashByte In hash
    buff.Append(String.Format("{0:X1}", hashByte))
    Next
    Label1.Text = buff.ToString()
    System.Threading.Thread.Sleep(4000)
    Loop
    f.Close()
     

     


    It doesn't output at all, it seams to freeze or smt.

     I know Do Loop isn't right way to go with this problem, but i'm not a proffesional so I dont know any other way to make this work in background all the time.

    Thank you ! 

    Sunday, October 11, 2009 9:26 AM

Answers

  • User-263151482 posted

    Hi!

    You should consider using the FileSystemWatcher -class

    You can easily can easily set the filepath and the notification event like changed, created, deleted and renamed.

    Make sure the set the EnableRaisingEvents property to true, to start listening.

    There are examples behind the link to MSDN.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, October 11, 2009 9:39 AM

All replies

  • User-263151482 posted

    Hi!

    You should consider using the FileSystemWatcher -class

    You can easily can easily set the filepath and the notification event like changed, created, deleted and renamed.

    Make sure the set the EnableRaisingEvents property to true, to start listening.

    There are examples behind the link to MSDN.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, October 11, 2009 9:39 AM
  • User-605707711 posted

    Thanks a lot, this seems to be much more efficient and better for my needs.

    I'll try to make this work.

     

    Sunday, October 11, 2009 9:44 AM