Call methods in WPF window from main thread

Anders

Use case
I'm developing a small application in C# that is called by another application to retrieve data from the Internet. It runs as a process on its own, but almost all of the interaction with it, is managed by the calling application. Therefor it does not have a GUI. However I'd like to add a progress bar using WPF that is shown during certain data retrievals that could take up to a minute. It's fairly easy to make an estimate of how much work is done and how much is left and therefor I find a progress bar suitable.

Research done
I have a fair understanding of threading after reading large parts of Albahari's pdf on threading (http://www.albahari.info/threading/threading.pdf). I have also read through a lot of posts on SO and MSDN in this matter. Most posts suggest the use of a background worker for the time consuming data retrieval while keeping the GUI in the main thread and therefor suggest solutions using a background worker. That feels awkward in this scenario though, where the main task is data retrieval and not GUI interaction.

I've spend a bunch of hours trying to make sense of different tutorials and forum posts while trying to conform them to my problem, but I have not succeeded and now I'm pretty much back to square one. Basically I'd like to end up with the following two classes outlined below:

ProgressBarWindow

public partial class ProgressBarWindow : Window
{
    public ProgressBarWindow()
    {
        InitializeComponent();
    }

    public void setValue(int value) 
    {
        // This function should be available from the main thread
    }
}

Querier

Public class Querier
{



    public List<Item> getItems()
    {
        // call ProgressBarWindow.setValue(0);
        ...
        // call ProgressBarWindow.setValue(100);
        // call ProgressBarWindow.Close();
    }
}

It's my understanding that UI must run under single threads and therefor my ProgressBarWindow object could not be instantiated in a new thread while at the same time be available to the main thread (kind of).

Dispatcher.BeginInvoke appears to be my savior here but so far I haven't been able to figure out what should go into the Querier class and what to go in the ProgressBarWindow class. How can I make the two threads interact with the same instance of ProgressBarWindow?

Please ask if you need more details and I will try to clarify.

Servy

You can use the Progress class to update the UI with the current progress of a long running operation.

First create an instance of Progress in your UI:

Progress<int> progress = new Progress<int>(currentProgress =>
{
    progressBar.Value = currentProgress;
    //todo do other stuff
});

Then pass it to the long running process:

public List<Item> getItems(IProgress<int> progress)
{
    progress.Report(0);
    //todo do something
    progress.Report(100);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Call main thread object methods from second thread design pattern

From Dev

Call methods from the main thread - UnityEngine C#

From Dev

call function of main thread from secondary thread

From Dev

Call function in main thread from another thread?

From Dev

Execute Python function in Main thread from call in Dummy thread

From Dev

Exception in thread "main" when calling methods from switch in menu class

From Dev

WPF Binding a Main Window Control from a UserControl View Model

From Dev

Raise a event on the User control from the Main Window in WPF

From Dev

call from java Main to methods in another java file

From Dev

Call methods of Firebase Messaging Service from main activity

From Dev

You should not call getWriteableDatabase() from the application main thread

From Dev

You should not call getWriteableDatabase() from the application main thread

From Dev

Show MessageBox from thread as it were from the main thread in C#/WPF

From Dev

Can't call a method from another window in C# WPF

From Dev

WPF C# call window function from App

From Dev

How to call a Usercontrol from another in same window in WPF ?

From Dev

WPF C# call window function from App

From Dev

Call method on render page content from parent window WPF

From Dev

WPF main window does not return

From Dev

Java: How to open the execution of new Thread in separate command window from Main Thread?

From Dev

Displaying window from ui-thread sometimes blocks the main ui-thread

From Dev

Launching a WPF Window in a Separate Thread

From Dev

Launching a WPF Window in a Separate Thread

From Dev

How to call getLooper() on main Thread?

From Dev

WPF: Cannot set Visibility or call Show, after a Window has closed. Trying to open a main window after Splash

From Dev

how to call getWritableDatabase() from a different thread or intent service, App crashing on calling it in the main thread

From Dev

Open window designed in WPF from external exe and receive return values from methods

From Dev

Java: Multithreading accessing main thread variables/methods

From Dev

Call function from Main()

Related Related

  1. 1

    Call main thread object methods from second thread design pattern

  2. 2

    Call methods from the main thread - UnityEngine C#

  3. 3

    call function of main thread from secondary thread

  4. 4

    Call function in main thread from another thread?

  5. 5

    Execute Python function in Main thread from call in Dummy thread

  6. 6

    Exception in thread "main" when calling methods from switch in menu class

  7. 7

    WPF Binding a Main Window Control from a UserControl View Model

  8. 8

    Raise a event on the User control from the Main Window in WPF

  9. 9

    call from java Main to methods in another java file

  10. 10

    Call methods of Firebase Messaging Service from main activity

  11. 11

    You should not call getWriteableDatabase() from the application main thread

  12. 12

    You should not call getWriteableDatabase() from the application main thread

  13. 13

    Show MessageBox from thread as it were from the main thread in C#/WPF

  14. 14

    Can't call a method from another window in C# WPF

  15. 15

    WPF C# call window function from App

  16. 16

    How to call a Usercontrol from another in same window in WPF ?

  17. 17

    WPF C# call window function from App

  18. 18

    Call method on render page content from parent window WPF

  19. 19

    WPF main window does not return

  20. 20

    Java: How to open the execution of new Thread in separate command window from Main Thread?

  21. 21

    Displaying window from ui-thread sometimes blocks the main ui-thread

  22. 22

    Launching a WPF Window in a Separate Thread

  23. 23

    Launching a WPF Window in a Separate Thread

  24. 24

    How to call getLooper() on main Thread?

  25. 25

    WPF: Cannot set Visibility or call Show, after a Window has closed. Trying to open a main window after Splash

  26. 26

    how to call getWritableDatabase() from a different thread or intent service, App crashing on calling it in the main thread

  27. 27

    Open window designed in WPF from external exe and receive return values from methods

  28. 28

    Java: Multithreading accessing main thread variables/methods

  29. 29

    Call function from Main()

HotTag

Archive