0

I have a .cab extraction utility. On my main window, I want to show the name of .cab being extracted, which file is being extracted right now, and the percentage of extraction done.

I have written properties for each field i.e. file name, percentage, etc... which are on my ViewModel.

All is working fine but it is not reflected on UI

MainVindowViewModel:

    public string FileExtract
    {
        get
        {
            return _fileExtract;
        }
        set
        {
            _fileExtract = value;
            NotifyPropertyChanged("FileExtract");
        }
    }

    public int Percent
    {
        get
        {
            return _percent;
        }
        set
        {
            _percent = value;
            NotifyPropertyChanged("Percent");
        }
    }     

Method for extraction

private void ExtractCab(string outputDirectory)
{
    m_CabinetFile.FileExtractBefore += new EventHandler(CabinetFile_FileExtractBefore);
    m_CabinetFile.FileExtractComplete += new EventHandler(CabinetFile_FileExtractComplete);
}

above two events triggers before and after file is extracted respectively.

With the following methods I get all info that I required when cab is getting extracted, but it is not reflected on UI

    private void CabinetFile_FileExtractBefore(object sender, System.EventArgs e)
    {
        TFile file = (TFile)sender;

        FileExtract = file.FullName;                    
    }

    private void CabinetFile_FileExtractComplete(object sender, System.EventArgs e)
    {
        Count++;
        Percent = Convert.ToInt32(((decimal)Count / (decimal)m_CabinetFile.FileCount) * 100);
    }

FileExtract and Percent properties are bound to the XAML UI, which is getting updated in code but not in UI. UI is stuck until complete cab has been extracted.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
DSS
  • 49
  • 5
  • My advice is to have a look at `async\await` functionality or at the `BackgroundWorker` control. `BackGroundWorker` is easier to understand, but `async\await` is, in my opinion, a better solution nowadays, once you understand how it works. – AndreySarafanov Jul 25 '14 at 09:09
  • @Sheridan I think this is bug request and not 'how to' post. – Matas Vaitkevicius Jul 25 '14 at 10:07
  • @LIUFA, this is a question and answer site, not a bug requesting, or reporting site. The question author has quite clearly had problems with their `BackgroundWorker`, so the linked question will help them to fix that. – Sheridan Jul 25 '14 at 10:30

1 Answers1

1

Always use BackgroundWorker to do intensive computations in WPF. Main thread is responsible for UI rendering and if busy with extracting, it cannot respond to other requests.

BackgroudWorker class provides also callbacks to report current progress, which you can use to inform user in UI.

Jan Kukacka
  • 1,196
  • 1
  • 14
  • 29