-1

I'm currently making an export app that would run a query in the db and store them in excel.

So my code will change the text of status and collapsed the visibility of the button when pressed.

Here is my code block:

        statusTextBlock.Text = "Export is running...";
        ExportDryRunBtn.Visibility = Visibility.Collapsed;
        GPDatabase.ExportDryRunItemsToExcel(progressBar);
        ExportDryRunBtn.Visibility = Visibility.Visible;
        statusTextBlock.Text = "";

My issue is that when I press export, the status won't change and the visibility will not collapsed. The application will not be responsive until it finishes the export then the other control changes will show up. I have a similar application that I worked on Windows Form with the same code structure and it works as intended.

Any reason why I can't do the same thing with WPF?

  • 1
    I'm not a WPF expert, but in a traditional Windows app, changes to appearance aren't reflected on the screen until the handler completes and hands back control to the message loop (the dispatcher in WPF, IIRC). You'll need to get out of that handler for this to work. In a Windows app, you could do that by posting a message to yourself and handling it – Flydog57 Mar 09 '22 at 03:29
  • Interesting, that I didn't know. I wonder how should I properly handle control changes in WPF – Franz Justin Buenaventura Mar 09 '22 at 03:31

1 Answers1

0

Your export function is probably taking a long time to complete, therefore you need to make it async and await it so you don't block the UI thread in WPF.

Visual Studio
  • 184
  • 1
  • 10
  • Didn't work, I converted my database export method to add async and await but it still blocking the UI Thread I think – Franz Justin Buenaventura Mar 09 '22 at 03:36
  • @FranzJustinBuenaventura The method that calls your database export method must be async as well. I'd recommend taking a look at [this post to get an idea of how to interact with controls without blocking the UI thread](https://stackoverflow.com/a/27089652/10470363) – Visual Studio Mar 09 '22 at 03:41
  • Hmm you are right, will need to rethink my code design to cater with the thread requirement for WPF – Franz Justin Buenaventura Mar 09 '22 at 04:26