mardi 5 mai 2015

Background worker RunWorkCompleted Event

I have following wpf program, what i want to do is btnAnalyzer_click method to wait till both of the DoWork & RunWorkerCompleted methods finish. So i used a AutoResetEvent, but now bwAnalyze_click method(#4 line) run after the DoWork and then WorkerCompleted method(line order - #1 #2 #4 & #3). But i want them to execute in the order #1 #2 #3 & #4. Any solutions or suggestions?

public partial class MainWindow : Window
{
    private readonly BackgroundWorker bwAnalyzer = new BackgroundWorker();
    private AutoResetEvent autoReset;//to signal the end of the BackgroudnWork

    public MainWindow(bool bStartLCSMood, UPDExtractorFile updInfo)
    {
        InitializeComponent();
        autoReset = new AutoResetEvent(false);
        bwAnalyzer.DoWork += new DoWorkEventHandler(DoWork);
        bwAnalyzer.RunWorkerCompleted += new RunWorkerCompletedEventHandler(WorkerCompleted);
    }

    void WorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        Console.WriteLine("Completed"); #3
        autoReset.Set();
    }

    void DoWork(object sender, DoWorkEventArgs e)
    {
        Console.WriteLine("load"); #2

    }

    private void btnAnalyze_Click(object sender, RoutedEventArgs e)
    {
        bwAnalyzer.RunWorkerAsync(); #1
        autoReset.WaitOne();//when commented working properly
        Console.WriteLine("click"); #4
    }
}

Aucun commentaire:

Enregistrer un commentaire