backgroundworker

Designing an Interface for BackgroundWorker

[亡魂溺海] 提交于 2019-11-30 22:33:41
In my windows forms applications I have a class that extends a backgroundworker, let's call it ExtendedBGW1.cs in my form class I declare it like a member variable so I have scope for the entire class like so: public partial class Main : Form { ExtendedBGW1 ebgw1; } Later on in the forms constructor I do this public Main() { InitializeComponent(); ebgw1 = new ExtendedBGW1(); InitializeBackgoundWorker(); } My InitializeBackgroundWoker() method looks like this private void InitializeBackgoundWorker() { ebgw1.DoWork += new DoWorkEventHandler(ebgw1.worker_DoWork); ebgw1.RunWorkerCompleted += new

Send a backgroundworker to sleep while checking for cancellation

大憨熊 提交于 2019-11-30 21:37:14
I have a background worker which updates the GUI on a regular basis via ReportProgress. The update occurs at regular intervals, every 5 seconds for example, or it could be 20 seconds. In order to perform the update at set times I send the worker process to sleep for the duration and when it wakes it updates the GUI with new information. The worker supports cancellation, and outside of sleeping it cancels correctly. I want to be able to invoke the cancellation during the wait period, however sending the thread to sleep makes this impossible. I assume I'll have to invoke a loop and check for the

reusing the backgroundworker more than once

こ雲淡風輕ζ 提交于 2019-11-30 21:15:32
i am using the background worker to do an expensive operation: backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork); backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged); backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted); backgroundWorker1.RunWorkerAsync(inputs); At the end i have this: void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { Messagebox.Show("Done with expensive operation 1"}; } I now have another expensive

Getting data off the Clipboard inside a BackgroundWorker

倾然丶 夕夏残阳落幕 提交于 2019-11-30 18:17:27
问题 I have a background worker and in the DoWork method I have the following: var clipboardData = Application.Current.Dispatcher.Invoke(new Action(() => { Clipboard.GetData(DataFormats.Serializable); })); Why does this always return null even though I know there is data on the clipboard in the correct format? 回答1: Try putting the call into an STA thread: object data = null; Thread t = new Thread(() => { data = Clipboard.GetData(DataFormats.Serializable); }); t.SetApartmentState(ApartmentState.STA

Designing an Interface for BackgroundWorker

送分小仙女□ 提交于 2019-11-30 18:02:19
问题 In my windows forms applications I have a class that extends a backgroundworker, let's call it ExtendedBGW1.cs in my form class I declare it like a member variable so I have scope for the entire class like so: public partial class Main : Form { ExtendedBGW1 ebgw1; } Later on in the forms constructor I do this public Main() { InitializeComponent(); ebgw1 = new ExtendedBGW1(); InitializeBackgoundWorker(); } My InitializeBackgroundWoker() method looks like this private void

Does closing the application stops all active BackgroundWorkers?

杀马特。学长 韩版系。学妹 提交于 2019-11-30 16:50:02
问题 Simple question, to repeat the title: Does closing the WinForms application stops all active BackgroundWorkers? 回答1: Yes, it does. BackgroundWorker.RunWorkerAsync simply calls BeginInvoke on a internal delegate, which in turn queues the request to the ThreadPool . Since all ThreadPool threads are background, yes, it will end when the application ends. However, keep in mind that: By "closing the WinForms application" I am presuming closing the main Form instance (that's generally the one

C# Can I add values to a listbox with a backgroundwork thread?

可紊 提交于 2019-11-30 14:29:25
I want my background worker to add items to a list box, it appears to do so when debugging but the listbox doesn't show the values. I suspect this is something to do with adding items whilst inside the background worker thread, do I need to add these to an array and then populate the list box from the array during backgroundWorker1_RunWorkerCompleted ? Thanks for the help. You can use Invoke like this: private void AddToListBox(object oo) { Invoke(new MethodInvoker( delegate { listBox.Items.Add(oo); } )); } You can, but you must advise your Backgroundworker to report state, and send the input

Async/await for long-running API methods with progress/cancelation

走远了吗. 提交于 2019-11-30 14:29:09
Edit I suppose the proper way of forcing await to invoke the worker asynchronously is with a Task.Run, like this: await Task.Run(() => builder.Build(dlg.FileName, cts.Token, new Progress(ReportProgress))); Got some light from http://blogs.msdn.com/b/pfxteam/archive/2012/04/12/10293335.aspx . this should be easy but I'm new to async/await so bear with me. I am building a class library exposing an API with some long-running operations. In the past, I used a BackgroundWorker to deal with progress reporting and cancelation, like in this simplified code fragment: public void DoSomething(object

WinForms UI responsiveness when dealing with “heavy” data

为君一笑 提交于 2019-11-30 14:15:31
问题 I'm modifying a windows form to allow data to be loaded in the background while the UI remains responsive. The data takes noticeable time to both retrieve and bind. Ideally, I would do both in the background, but there is some ambiguity about what kind of UI updates I should be doing in the background (as in outside the main thread). A solid example that shows data retrieval and data binding in the background would be very helpful. 回答1: The retrieval can, and should, be pushed off to a

How to test a ViewModel that loads with a BackgroundWorker?

余生颓废 提交于 2019-11-30 13:49:49
问题 One of the nice things about MVVM is the testability of the ViewModel. In my particular case, I have a VM that loads some data when a command is called, and its corresponding test: public class MyViewModel { public DelegateCommand LoadDataCommand { get; set; } private List<Data> myData; public List<Data> MyData { get { return myData; } set { myData = value; RaisePropertyChanged(() => MyData); } } public MyViewModel() { LoadDataCommand = new DelegateCommand(OnLoadData); } private void