Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to execute 3 methods one after another, a message should be shown in label control (eg: checking method1..) at the same time when we execute each method. How to do this in WPF Window.

The UI thread must not wait for worker threads to complete, to show the window and the worker threads should execute one after another. For that purpose i have used join(), that made UI thread also waiting for worker threads to finish.
Posted
Updated 29-Jul-15 23:56pm
v4

You would need to do it in a separate thread. Now, the problem is how to reflect the results in a UI. You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke(),
Problem with Treeview Scanner And MD5.

See also more references on threading:
.NET event on main thread,
How to get a keydown event to operate on a different thread in vb.net,
Control events not firing after enable disable + multithreading,
.NET event on main thread[^].

—SA
 
Share this answer
 
Comments
[no name] 30-Jul-15 6:16am    
Hi i've updated the question.
Sergey Alexandrovich Kryukov 30-Jul-15 9:28am    
Just use my answer. And don't use Solution 1; it's bad, can bring a lot of problems.
—SA
Use dispatcher timer for every Function

C#
public class ViewModel{

    private static System.Timers.Timer aTimer;

    public ViewModel()
    {
        aTimer = new Timer();
        aTimer.Interval = 2000; // every two seconds

        // Hookup to the elapsed event
        aTimer.Elapsed += DoWork;

        // Have the timer fire repeated events (true is the default)
        aTimer.AutoReset = true;

        // Start the timer
        aTimer.Enabled = true;
    }

    public void DoWork(Object source, System.Timers.ElapsedEventArgs e) {
        //do work here
    }
}
 
Share this answer
 
Comments
[no name] 30-Jul-15 6:15am    
Hi i've updated the question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900