在WPF应用程序中遇到后台工作进程问题

时间:2017-05-17 02:09:13

标签: c# wpf

我发现了一些关于使用后台工作人员的其他文章,我将在下面链接。我使用代码示例并尝试执行此操作以运行3个不同的SQL查询。在下面发布的代码中,当我在RunBackGroundWorkerProcesses1内部中断时,它会停在那里并被调用,但是即使它在代码中,也从不调用worker_DoWork1的方法。我假设我误解了这一点,有人可以增加一些清晰度。

链接我用作参考: WPF Multithreading

代码:

public CallInformationMainScreen()
        {
            InitializeComponent();    

//This is where i call the background processes
            RunBackGroundWorkerProcesses1();
            RunBackGroundWorkerProcesses2();
            RunBackGroundWorkerProcesses3();    
        }
        #endregion

        #region Methods used to generate data for the UI
        public string DisplayTotalDailyCalls()
        {
            DailyCallsQuery db = new DailyCallsQuery();
            return db.GetNumber(SkillNumber);
        }
        public string DisplayTotalLastSevenCalls()
        {
            PrevSevenCallQuery db = new PrevSevenCallQuery();
            return db.GetNumber(SkillNumber);
        }
        public string DisplayDailyAbandonCalls()
        {
            DailyAbandonQuery db = new DailyAbandonQuery();
            return db.GetNumber(SkillNumber);
        }    


        #endregion

        #region Background worker processes        
        private void RunBackGroundWorkerProcesses1()
        {
            BackgroundWorker worker = new BackgroundWorker();            
            worker.DoWork += new DoWorkEventHandler(worker_DoWork1);
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);

            System.Timers.Timer t = new System.Timers.Timer(10000); //  10 second intervals
            t.Elapsed += (sender, e) =>
            {
                // Don't try to start the work if it's still busy with the previous run...
                if (!worker.IsBusy)
                    worker.RunWorkerAsync();
            };
        }
        private void RunBackGroundWorkerProcesses2()
        {
            BackgroundWorker worker = new BackgroundWorker();           
            worker.DoWork += new DoWorkEventHandler(worker_DoWork2);
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);

            System.Timers.Timer t = new System.Timers.Timer(10000); //  10 second intervals
            t.Elapsed += (sender, e) =>
            {
                // Don't try to start the work if it's still busy with the previous run...
                if (!worker.IsBusy)
                    worker.RunWorkerAsync();
            };
        }
        private void RunBackGroundWorkerProcesses3()
        {
            BackgroundWorker worker = new BackgroundWorker();            
            worker.DoWork += new DoWorkEventHandler(worker_DoWork3);
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);

            System.Timers.Timer t = new System.Timers.Timer(10000); //  10 second intervals
            t.Elapsed += (sender, e) =>
            {
                // Don't try to start the work if it's still busy with the previous run...
                if (!worker.IsBusy)
                    worker.RunWorkerAsync();
            };
        }

        private void worker_DoWork1(object sender, DoWorkEventArgs e)
        {
            // Whatever comes back from the lengthy process, we can put into e.Result            
            TotalDailyCalls = DisplayTotalDailyCalls();
            e.Result = TotalDailyCalls;
        }
        private void worker_DoWork2(object sender, DoWorkEventArgs e)
        {
            // Whatever comes back from the lengthy process, we can put into e.Result            
            TotalDailyLast7Days = DisplayTotalLastSevenCalls();
            e.Result = TotalDailyCalls;
        }
        private void worker_DoWork3(object sender, DoWorkEventArgs e)
        {
            // Whatever comes back from the lengthy process, we can put into e.Result
            TotalDailyAbandon = DisplayDailyAbandonCalls();
            e.Result = TotalDailyAbandon;
        }

        private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            // First, handle the case where an exception was thrown.
            if (e.Error != null)
            {
                // handle the System.Exception
                MessageBox.Show(e.Error.Message);
            }
            else if (e.Cancelled)
            {
                // now handle the case where the operation was cancelled... 
                ErrorHolder = "The operation was cancelled";
            }
            else
            {
                // Finally, handle the case where the operation succeeded
                ErrorHolder = e.Result.ToString();
            }
        }
        #endregion

3 个答案:

答案 0 :(得分:2)

你没有启动你的计时器。请参阅Timer.Start Method ()

    private void RunBackGroundWorkerProcesses1()
    {
        BackgroundWorker worker = new BackgroundWorker();            
        worker.DoWork += new DoWorkEventHandler(worker_DoWork1);
        worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);

        System.Timers.Timer t = new System.Timers.Timer(10000); //  10 second intervals
        t.Elapsed += (sender, e) =>
        {
            // Don't try to start the work if it's still busy with the previous run...
            if (!worker.IsBusy)
                worker.RunWorkerAsync();
        };
        t.Start(); // Start the timer
    }

答案 1 :(得分:1)

我发布此信息是为了说明一种更简单的方法。它并不意味着直接回答这个问题。

如果您是NuGet" System.Reactive"以及相关的WPF库,你可以这样做:

IDisposable subscription =
    new []
    {
        Observable.Interval(TimeSpan.FromSeconds(10.0)).Select(x => DisplayTotalDailyCalls()),
        Observable.Interval(TimeSpan.FromSeconds(10.0)).Select(x => DisplayTotalLastSevenCalls()),
        Observable.Interval(TimeSpan.FromSeconds(10.0)).Select(x => DisplayDailyAbandonCalls()),
    }
    .Merge()
    .ObserveOnDispatcher()
    .Subscribe(x => ErrorHolder = x, e => MessageBox.Show(e.Error.Message));

那就是它。任务完成。所有代码都是技术上的一行代码。

答案 2 :(得分:0)

只有在BackgroundWorker.RunWorkerAsync()事件被触发时才会调用

Timer.Elapsed。由于计时器设置为10秒间隔,BackgroundWorker将不会启动10秒。您可能应该在创建并初始化它之后调用BackgroundWorker.RunWorkerAsync(),以便它立即开始。