backgroundworker

How to wait correctly until BackgroundWorker completes?

六月ゝ 毕业季﹏ 提交于 2019-11-27 11:13:22
Observe the following piece of code: var handler = GetTheRightHandler(); var bw = new BackgroundWorker(); bw.RunWorkerCompleted += OnAsyncOperationCompleted; bw.DoWork += OnDoWorkLoadChildren; bw.RunWorkerAsync(handler); Now suppose I want to wait until bw finishes working. What is the right way to do so? My solution is this: bool finished = false; var handler = GetTheRightHandler(); var bw = new BackgroundWorker(); bw.RunWorkerCompleted += (sender, args) => { OnAsyncOperationCompleted(sender, args); finished = true; }); bw.DoWork += OnDoWorkLoadChildren; bw.RunWorkerAsync(handler); int

Windows Service that runs Periodically

…衆ロ難τιáo~ 提交于 2019-11-27 10:22:08
问题 I'm writing a windows service that once started will run every X hours. The process it completes is fairly intensive, so I want to use a background worker. I'm using a Settings file to store both the hours between runs and the last time the service ran. I'm not exactly sure the best way to do this - that is, I want the service to idle using as few resources as possible, and when it runs, it needs to run in the background worker, report what it did, and then go back into idle mode. I've

polling with delayed_job

无人久伴 提交于 2019-11-27 09:21:45
问题 I have a process which takes generally a few seconds to complete so I'm trying to use delayed_job to handle it asynchronously. The job itself works fine, my question is how to go about polling the job to find out if it's done. I can get an id from delayed_job by simply assigning it to a variable: job = Available.delay.dosomething(:var => 1234) +------+----------+----------+------------+------------+-------------+-----------+-----------+-----------+------------+-------------+ | id | priority |

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

白昼怎懂夜的黑 提交于 2019-11-27 09:18:38
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? Fredrik Mörk This is how to do it (link to answer below) 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 ManualResetEvent(true); private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { while(run.WaitOne

BackgroundWorker completes before DoWork

丶灬走出姿态 提交于 2019-11-27 08:37:36
问题 I'm using a background worker to handle the loading of a file to stop my ui from freezing however it seems that the RunWorkerCompleted is finishing before my DoWork event has completed (Causes errors when exiting dialog)... is there anything I'm doing wrong? Am I better off doing this over a task? public static <T> LoadDesign(string xmlPath) { PleaseWait pw = new PleaseWait(xmlPath); pw.ShowDialog(); return pw.design; } private PleaseWait(string xmlFile) { InitializeComponent(); bw = new

Reusing a BackgroundWorker, cancel and wait for it

筅森魡賤 提交于 2019-11-27 07:55:55
问题 Suppose you have a search textbox and have a search algorithm attached to the TextChanged event, that runs with a BackgroundWorker. If there comes a new character in the textbox, i need to cancel the previous search and run it again. I tried using events in between the main thread and the bgw, from this previous question, but I still get the error "currently busy and cannot run multiple tasks concurrently" BackgroundWorker bgw_Search = new BackgroundWorker(); bgw_Search.DoWork += new

How to use BackgroundWorker in C#

风格不统一 提交于 2019-11-27 07:21:22
问题 How to use BackgroundWorker in C#? Actually i'm performing an operation of filling a PDF-Form from method called fill() . It takes more time to show up the result into pdfviewer, so I decided to show up a 'processing image' using a backgroundworker, and tried using it but failing to achieve it here is my code snippet : private void bgwLoadFile_DoWork(object sender, DoWorkEventArgs e) { this.Invoke((MethodInvoker)delegate() { ???? }); } private void bgwLoadFile_RunWorkerCompleted(object sender

How to make BackgroundWorker return an object

六眼飞鱼酱① 提交于 2019-11-27 06:47:45
I need to make RunWorkerAsync() return a List<FileInfo> . How can I return an object from a background worker? 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 RunWorkerCompleted event, which has a RunWorkerCompletedEventArgs object as an argument. RunWorkerCompletedEventArgs

WPF BackgroundWorker vs. Dispatcher

我是研究僧i 提交于 2019-11-27 06:30:30
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 The main difference between the Dispatcher and other threading methods is that the Dispatcher is not actually multi-threaded. The Dispatcher governs the controls, which need a single thread to

Displaying a progressbar while executing an SQL Query

被刻印的时光 ゝ 提交于 2019-11-27 05:56:23
问题 I want to inform the user while data is being read from an SQL database and I decided to create a form with a progressbar but it doesn't work - maybe because a thread is needed. I want to create the form programmatically ProgressBar pb = new ProgressBar(); pb.MarqueeAnimationSpeed = 30; pb.Style = ProgressBarStyle.Marquee; pb.Dock = DockStyle.Fill; progressForm.ClientSize = new Size(200, 50); progressForm.FormBorderStyle = FormBorderStyle.FixedDialog; progressForm.StartPosition =