codingdir logo sitemap sitemap |
Home
PHP
C#
C++
ANDROID
JAVA
JAVASCRIPT
PYTHON

The BackgroundWorker RunWorkerAsync method does not trigger DoWork();


By : , Category : c#

BackgroundWorker.IsBusy will be true after its background thread been started, so it will always be true where you are testing it inside bgw_DoWork(). Therefore, monitor.StartFolderMonitor(txtFolderPath.Text) will never be called.

What are you trying to test for? I think you can just remove the if (!worker.IsBusy) check.

ReLated :

Your code looks fine there is no change in code..try to figure out following things

  1. Check your click event is working ...button click is working
  2. put break point in click event and debug it if click event is working

I suggest you can look this article once : Multi-threading with the BackgroundWorker which is doing same thing as you are doing.. Might be your find something missing.

From a design standpoint, I would recommend keeping the constructor as free from doing any actual work as is possible. The reason for that is because work in the constructor can lead to unexpected behavior and general buggy-ness. It can also make your code less flexible and much harder to unit-test. But those suggestions apply to all work done in the constructor. They don't apply specifically to just background workers. And just like any maxim, there are sometimes exceptions-to-the-rule.

From a technical stand point, as long as you are calling it after the designer-inserted InitializeComponent method is called, then you should not have any negative ramifications.

I think you are looking for ReportProgress i made up a quick example (console application)

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            BackgroundWorker bw = new BackgroundWorker();
            bw.DoWork += new DoWorkEventHandler(bw_DoWork);
            bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
            bw.WorkerReportsProgress = true;
            bw.RunWorkerAsync();

            Console.ReadKey();
        }

        static void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            Console.WriteLine("Percentage is:{0}%", e.ProgressPercentage);
        }

        static void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            var self = (sender as BackgroundWorker);
            self.ReportProgress(10);
            Thread.Sleep(1000);
            self.ReportProgress(20);
            Thread.Sleep(1000);
            self.ReportProgress(30);
        }

    }
}

hope that covers your question.

The DoWork event runs in a background thread. It cannot interact with UI elements. You should be creating some result; something that is not itself a UI element, setting it to the BGW's Result property, and then updating the UI based on that result in the RunWorkerCompleted even handler, which can access the result set in DoWork.

Comments


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © codingdir.com All Rights Reserved .