Пользовательский интерфейс зависает на тяжелом расчете - PullRequest
4 голосов
/ 04 декабря 2011

Я загружаю огромные файлы в память, но по этому расчету мое приложение зависает.

Есть идеи, в чем проблема с моим кодом?

public void Drop(DragEventArgs args)
{
  BackgroundWorker worker = new BackgroundWorker();
  string fileName = IsSingleTextFile(args);
  if (fileName == null) return;
  worker.DoWork += (o, ea) =>
  {
    try
    {
      StreamReader fileToLoad = new StreamReader(fileName);
      string filecontent = fileToLoad.ReadToEnd();
      fileToLoad.Close();
      // providing the information to the UI thread

      Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background,
        new Action(() => SfmLogFile = filecontent));
    }
    catch (Exception)
    {
      throw;
    }
  };

  worker.RunWorkerCompleted += (o, ea) =>
  {
    args.Handled = true;
    IsBusy = false;
  };

  // Mark the event as handled, so TextBox's native Drop handler is not called.

  IsBusy = true;
  worker.RunWorkerAsync();

}

Ответы [ 2 ]

2 голосов
/ 04 декабря 2011

Я бы преобразовал ваш образец в что-то вроде этого:

public void Drop(DragEventArgs args)
{
  string fileName = IsSingleTextFile(args);
  if (fileName == null) return;
  // It is better to create worker after check for file name.
  BackgroundWorker worker = new BackgroundWorker();
  worker.DoWork += (o, ea) =>
  {
    try
    {
      string filecontent = ReadAllText(fileName);    
      ea.Result = fileContent;
    }
    catch (Exception)
    {
      throw;
    }
  };

  worker.RunWorkerCompleted += (o, ea) =>
  {
    var fileContent = ea.Result as string;
    Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background,
        new Action(() => SfmLogFile = filecontent));

    // if IsBusy propery is not on the UI thread, then you may leave it here
    // otherwise it should be set using the dispatcher too.
    IsBusy = false;
  };

  IsBusy = true;
  worker.RunWorkerAsync();
  // Mark the event as handled, so TextBox's native Drop handler is not called.    
  args.Handled = true;
}
1 голос
/ 04 декабря 2011

Я не уверен, является ли это причиной вашей проблемы, но вы устанавливаете args.Handled в обратном вызове для фонового работника, поэтому он будет вызываться после возврата обработчика события удаления. Он не будет иметь желаемого эффекта, так как он установлен слишком поздно, и может что-то испортить при обработке события.

...