backgroundworker

How To Start And Stop A Continuously Running Background Worker Using A Button

我与影子孤独终老i 提交于 2019-11-26 14:38:12
问题 Let's say I have a background worker like this: private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { while(true) { //Kill zombies } } How can I make this background worker start and stop using a button on a WinForm? 回答1: This is how to do it (link to answer below) 回答2: Maybe you can use a manualresetevent like this, I didn't debug this but worth a shot. If it works you won't be having the thread spin its wheels while it's waiting ManualResetEvent run = new

Sending Arguments To Background Worker?

痞子三分冷 提交于 2019-11-26 12:46:18
Let's say I want to sent an int parameter to a background worker, how can this be accomplished? private void worker_DoWork(object sender, DoWorkEventArgs e) { } I know when this is worker.RunWorkerAsync();, I don't understand how to define in worker_DoWork that it should take an int parameter. You start it like this: int value = 123; bgw1.RunWorkerAsync(argument: value); // the int will be boxed and then private void worker_DoWork(object sender, DoWorkEventArgs e) { int value = (int) e.Argument; // the 'argument' parameter resurfaces here ... // and to transport a result back to the main

How to make BackgroundWorker return an object

孤者浪人 提交于 2019-11-26 12:10:00
问题 I need to make RunWorkerAsync() return a List<FileInfo> . How can I return an object from a background worker? 回答1: In your DoWork event handler for the BackgroundWorker (which is where the background work takes place) there is an argument DoWorkEventArgs . This object has a public property object Result. When your worker has generated its result (in your case, a List<FileInfo> ), set e.Result to that, and return. Now that your BackgroundWorker has completed its task, it triggers the

How to wait for a BackgroundWorker to cancel?

我只是一个虾纸丫 提交于 2019-11-26 12:06:33
Consider a hypothetical method of an object that does stuff for you: public class DoesStuff { BackgroundWorker _worker = new BackgroundWorker(); ... public void CancelDoingStuff() { _worker.CancelAsync(); //todo: Figure out a way to wait for BackgroundWorker to be cancelled. } } How can one wait for a BackgroundWorker to be done? In the past people have tried: while (_worker.IsBusy) { Sleep(100); } But this deadlocks , because IsBusy is not cleared until after the RunWorkerCompleted event is handled, and that event can't get handled until the application goes idle. The application won't go

WPF BackgroundWorker vs. Dispatcher

百般思念 提交于 2019-11-26 12:00:57
问题 In my WPF application I need to do an async-operation then I need to update the GUI. And this thing I have to do many times in different moment with different oparations. I know two ways to do this: Dispatcher and BackgroundWorker. Because when I will chose it will be hard for me to go back, I ask you: what is better? What are the reasons for choosing one rather than the other? Thank you! Pileggi 回答1: The main difference between the Dispatcher and other threading methods is that the

InvalidOperationException - object is currently in use elsewhere

余生颓废 提交于 2019-11-26 11:21:04
问题 I\'ve gone through this SO question but it didn\'t help. The case here is different. I\'m using Backgroundworkers. 1st backgroundworker starts operating on the image input of user and inside firstbackgroundworker_runworkercompleted() I\'m using calling 3 other backgroundworkers algo1backgroundworker.RunWorkerAsync(); algo2backgroundworker.RunWorkerAsync(); algo3backgroundworker.RunWorkerAsync(); this is the code for each: algo1backgroundworker_DoWork() { Image img = this.picturebox.Image;

Can you link to a good example of using BackgroundWorker without placing it on a form as a component?

岁酱吖の 提交于 2019-11-26 11:16:22
问题 I can remember that many years ago (in 2005) I was using BackgroundWorker in my code without using a visual component for it, but I can\'t remember how (unfortunately I am very forgetful and forget everything very soon after I stop using it). Perhaps I was extending BackgroundWorker class. Can you link to a good example of using BackgroundWorker this way? 回答1: This article explains everything you need clearly. Here are the minimum steps in using BackgroundWorker: Instantiate BackgroundWorker

Proper way to Dispose of a BackGroundWorker

烈酒焚心 提交于 2019-11-26 09:46:30
问题 Would this be a proper way to dispose of a BackGroundWorker? I\'m not sure if it is necesary to remove the events before calling .Dispose(). Also is calling .Dispose() inside the RunWorkerCompleted delegate ok to do? public void RunProcessAsync(DateTime dumpDate) { BackgroundWorker worker = new BackgroundWorker(); worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted); worker.DoWork += new DoWorkEventHandler(worker_DoWork); worker.RunWorkerAsync(dumpDate);

Running a method in BackGroundWorker and Showing ProgressBar

非 Y 不嫁゛ 提交于 2019-11-26 09:10:27
问题 What I want is when some method is doing some task UI keeps itself active and I want to show the progress of the work in a progress-bar. I have a method, a BackGroundWorker and a Progressbar . I want to call the method when BackGroundWorker starts running and show the progress. The method contains a loop. So, it can report the progress. So, what can be done? private void Form1_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the \'dataSet1.TBLMARKET\' table. You

Task parallel library replacement for BackgroundWorker?

被刻印的时光 ゝ 提交于 2019-11-26 08:53:10
问题 Does the task parallel library have anything that would be considered a replacement or improvement over the BackgroundWorker class? I have a WinForms application with a wizard-style UI, and it does some long-running tasks. I want to be able to have a responsive UI with the standard progress bar and ability to cancel the operation. I\'ve done this before with BackgroundWorker, but I\'m wondering if there are some TPL patterns that can be used instead? 回答1: The Task class is an improvement over