Search code examples
c#hexfilesystemwatcher

Looking for preventive from "not responding" C#


Yesterday my teachers gives me a task to make something like database in .txt file which has to contains hexes and a C# application which takes all hexes from this database, also with its offSets. Then i gotta use it,the offset, to take the hex from file on this offset and compare the both haxes, does they are same. I am using fileSystemWatcher to "spy" chosen directory for new files and with one, two, three or little bit more files it works perfect but if i try to copy very "big" folder the application stops - "not responding". I have try to find from where the problem comes like i adding and deleting functions and found the "black sheep" -the function which have to take the file's hex which is comply on the given offset.

  public string filesHex(string path,int bytesToRead,string offsetLong)
  {
      byte[] byVal;

      try
      {
          using (Stream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
          {
              BinaryReader brFile = new BinaryReader(fileStream);
              offsetLong = offsetLong.Replace("x", string.Empty);
              long result = 0;
              long.TryParse(offsetLong, System.Globalization.NumberStyles.HexNumber, null, out result);
              fileStream.Position = result;
              byte[] offsetByte = brFile.ReadBytes(0);
              string offsetString = HexStr(offsetByte);
              //long offset = System.Convert.ToInt64(offsetString, 16);
              byVal = brFile.ReadBytes(bytesToRead);
          }
          string hex = HexStr(byVal).Substring(2);

          return hex;
      }

Solution

  • You could create a new Thread and run the filesHex method in it.

    You can change your string inside the thread code and get it's value after like this:

     public string hex="";
     public void filesHex(string path,int bytesToRead,string offsetLong)
     {
            byte[] byVal;
    
    
          using (Stream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
          {
              BinaryReader brFile = new BinaryReader(fileStream);
              offsetLong = offsetLong.Replace("x", string.Empty);
              long result = 0;
              long.TryParse(offsetLong, System.Globalization.NumberStyles.HexNumber, null, out result);
              fileStream.Position = result;
              byte[] offsetByte = brFile.ReadBytes(0);
              string offsetString = HexStr(offsetByte);
              //long offset = System.Convert.ToInt64(offsetString, 16);
              byVal = brFile.ReadBytes(bytesToRead);
          }
          hex = HexStr(byVal).Substring(2);
    
    
      }
    

    This would be your call:

     Thread thread = new Thread(() => filesHex("a",5,"A"));//example for parameters.
      thread.Start();
        string hexfinal=hex;//here you can acess the desired string.
    

    Now it would not freeze the main UI thread because you run your method on a sperate thread.

    Goodluck.