backgroundworker

Background Worker How To

回眸只為那壹抹淺笑 提交于 2019-12-01 09:04:39
问题 I'm trying to implement background worker into my program, so that it wont freeze when i run the program and starts retrieving the data I need. I'm not quite sure how background worker works. 回答1: Background workers are threads that run in the background and do work without interrupting/blocking your main thread. You can read more here. In quick terms: In DoWork do your blocking operation. Whenever you can, report how far through you are with the operation using (sender as BackgroundWorker)

BackgroundWorker still needs to call Invoke?

[亡魂溺海] 提交于 2019-12-01 08:27:38
问题 In the last question Display progress bar while doing some work in C#?, people has recommend use of BackgroundWorker . I thought in BackgroundWorker DoWork method you can update the GUI directly, but why this function call need to be called using Invoke . toolTip.SetToolTip(button, toolTipText); 回答1: The RunWorkerCompleted callback is marshalled onto the UI thread; DoWork is not. You should use the ReportProgress method to update the UI during DoWork processing. See: How to: Run an Operation

Please help me make this code thread safe

走远了吗. 提交于 2019-12-01 08:22:43
问题 I've got a bit of a problem with making my data loading and filtering thread safe. The following code on my control's base class which handles all the data population through a BackgroundWorker. This tends to throw the error on "this.DataWorker.RunWorkerAsync()" saying that the BackgroundWorker is busy. /// <summary> /// Handles the population of the form data. /// </summary> /// <param name="reload">Whether to pull data back from the WebService.</param> public void Populate(bool reload) { if

Textbox text from background worker?

淺唱寂寞╮ 提交于 2019-12-01 08:17:22
I've been trying to figure out how to get my textbox's text or other property from within a background worker. Does anybody know how to do this? I cannot pass it as a param because it needs to be real-time. Thanks for the help! I think you need to just invoke the property (pseudo-code): private void bgw1_DoWork(object sender, DoWorkEventArgs e) { // looping through stuff { this.Invoke(new MethodInvoker(delegate { Text = textBox1.Text; })); } } Use the ReportProgress method and event of the Background worker. That will switch to the correct thread for you. Or if needed in WPF: private void

How to create an auto-upload Android picture app?

寵の児 提交于 2019-12-01 07:42:31
问题 I am trying to create an app that automatically uploads a picture to my server . The idea is that a user creates a picture with the native/normal camera and my app gets a notification (catches the event) and uploads the picture (in the background). I found a solution for Windows Phone (see here), but not for Android. How can I do this? - Is this technically even possible (with the given APIs) or is it a special feature just for contracted services ( Facebook or Dropbox do that )? Thank you!

Passing a method to a backgroundworker dowork

北城余情 提交于 2019-12-01 06:47:17
问题 In the code below, is there a way to instead of always subscribing the updateWorker_DoWork method, pass it a method like this public void GetUpdates(SomeObject blah) { //... updateWorker.DoWork += new DoWorkEventHandler(blah); //... } public void GetUpdates() { //Set up worker updateWorker.WorkerReportsProgress = true; updateWorker.WorkerSupportsCancellation = true; updateWorker.DoWork += new DoWorkEventHandler(updateWorker_DoWork); updateWorker.RunWorkerCompleted += new

Textbox text from background worker?

做~自己de王妃 提交于 2019-12-01 05:35:37
问题 I've been trying to figure out how to get my textbox's text or other property from within a background worker. Does anybody know how to do this? I cannot pass it as a param because it needs to be real-time. Thanks for the help! 回答1: I think you need to just invoke the property (pseudo-code): private void bgw1_DoWork(object sender, DoWorkEventArgs e) { // looping through stuff { this.Invoke(new MethodInvoker(delegate { Text = textBox1.Text; })); } } 回答2: Use the ReportProgress method and event

C# backgroundWorker reports string?

社会主义新天地 提交于 2019-12-01 03:18:35
How can I report a string (like "now searching file. . .", "found selection. . .") back to my windows.form from a backgroundWorker as well as a percentage. Additionally, I have a large class that contains the method I want to run in the backgroundWorker_Work. I can call it by Class_method(); but i am then unable to report my percentage done or anything from the called class, only from the backgroundWorker_Work method. Thanks! I'm assuming WCF also have the method public void ReportProgress(int percentProgress, Object userState); So just use the userState to report the string. private void

How to yield return inside anonymous methods?

时间秒杀一切 提交于 2019-12-01 02:05:31
Basically I have an anonymous method that I use for my BackgroundWorker : worker.DoWork += ( sender, e ) => { foreach ( var effect in GlobalGraph.Effects ) { // Returns EffectResult yield return image.Apply (effect); } }; When I do this the compiler tells me: "The yield statement cannot be used inside an anonymous method or lambda expression" So in this case, what's the most elegant way to do this? Btw this DoWork method is inside a static method, in case that matters for the solution. Unfortunately you can't. The compiler does not allow you to combine the two "magic" pieces of code. Both

C# backgroundWorker reports string?

可紊 提交于 2019-11-30 23:44:40
问题 How can I report a string (like "now searching file. . .", "found selection. . .") back to my windows.form from a backgroundWorker as well as a percentage. Additionally, I have a large class that contains the method I want to run in the backgroundWorker_Work. I can call it by Class_method(); but i am then unable to report my percentage done or anything from the called class, only from the backgroundWorker_Work method. Thanks! 回答1: I'm assuming WCF also have the method public void