【问题标题】:Looking for preventive from "not responding" C#寻找“不响应”C#的预防措施
【发布时间】:2015-09-11 11:19:39
【问题描述】:

昨天我的老师给了我一个任务,在 .txt 文件中制作类似数据库的东西,该文件必须包含十六进制和一个 C# 应用程序,该应用程序从该数据库中获取所有十六进制,以及它的偏移量。然后我必须使用它,偏移量,从这个偏移量的文件中获取十六进制并比较两个轴,它们是否相同。 我正在使用fileSystemWatcher 来“监视”为新文件选择的目录以及一个、两个、三个或更多文件,它工作得很好,但是如果我尝试复制非常“大”的文件夹,应用程序会停止 - “没有响应”。 我试图找到问题出在哪里,比如我添加和删除函数并找到“害群之马” - 该函数必须采用文件的十六进制,它符合给定的偏移量。

  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;
      }

【问题讨论】:

标签: c# hex filesystemwatcher


【解决方案1】:

您可以创建一个新线程并在其中运行filesHex 方法。

您可以在线程代码中更改您的字符串,然后像这样获取它的值:

 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);


  }

这将是您的电话:

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

现在它不会冻结主 UI 线程,因为您在单独的线程上运行您的方法。

祝你好运。

【讨论】:

  • 可能需要进一步的重组 - filesHex 接受参数并返回一个值,该值被此代码丢弃。
  • 好的,但是要在线程中使用它不能包含任何参数
  • @KenTavur 哦。请看我更新的答案。编辑它。,
  • 让我试试,我会给出反馈
  • @KenTavur 好吧..回来吧。祝你好运。
猜你喜欢
  • 2021-05-01
  • 2019-01-04
  • 2015-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多