- Add using
using System.ComponentModel;
- Declare Background Worker:
private readonly BackgroundWorker worker = new BackgroundWorker();
- Subscribe to events:
worker.DoWork += worker_DoWork; worker.RunWorkerCompleted += worker_RunWorkerCompleted;
- Implement two methods:
private void worker_DoWork(object sender, DoWorkEventArgs e) { // run all background tasks here } private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { //update ui once worker complete his work }
- Run worker async whenever your need.
worker.RunWorkerAsync();
- Track progress (optional, but often useful)a) subscribe to
ProgressChanged
event and useReportProgress(Int32)
inDoWork
b) setworker.WorkerReportsProgress = true;
(credits to @zagy)
Related Posts:
- The calling thread cannot access this object because a different thread owns it
- WFP Grid: MouseRightButtonDown firing occuring BEFORE SelectionChanged on grid. Causing syncing issue
- XAML Binding Groups
- Metadata file ‘.dll’ could not be found
- How to resolve this System.IO.FileNotFoundException
- How to resolve this System.IO.FileNotFoundException
- MSHTML DLL on Windows 10
- MSHTML DLL on Windows 10
- Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on
- How to add comments into a Xaml file in WPF?
- The calling thread must be STA, because many UI components require this in WPF
- WSACancelBlockingCall exception
- Task vs Thread differences
- How can I wait for a thread to finish with .NET?
- How can I get the active screen dimensions?
- Page vs Window in WPF?
- How do I exit a WPF application programmatically?
- How to avoid a System.Runtime.InteropServices.COMException?
- Using FolderBrowserDialog in WPF application
- ImageMagick creating blank transparent square(s) according to width
- Why has the int32 type a maximum value of 2³¹ − 1? [duplicate]
- What is a PDB file?
- C# error: “An object reference is required for the non-static field, method, or property”
- Error : Could not load file or assembly or one of it’s dependencies
- Algorithms in O(n^2) vs O(n) [duplicate]
- The remote certificate is invalid according to the validation procedure [duplicate]
- Export Pervasive to MySQL (C#)
- WCF vs ASP.NET Web API
- Why use the params keyword?
- What does plus equals(+=) operator means here?
- Compiler Error Message: The compiler failed with error code -2146232576
- Validation failed for one or more entities. See ‘EntityValidationErrors’ property for more details
- What is the best way to iterate over a dictionary?
- A generic error occurred in GDI+, JPEG Image to MemoryStream
- “Could not find a part of the path” error message
- WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for jquery
- What does the %= operator do? [duplicate]
- What does vary:accept-encoding mean?
- what is this oleaut32.dll?
- Why not use Html.EditorForModel()
- Where can I find System.Web.Helpers, System.Web.WebPages, and System.Web.Razor?
- How to Use TextureBrush for painting an Image
- Can’t use System.Windows.Forms
- How to use the “Using” statement in ASP.net razor webpages?
- Where can I find the assembly System.Web.Extensions dll?
- Regex for numbers only
- There is no argument given that corresponds to the required formal parameter – .NET Error
- How do I remedy “The breakpoint will not currently be hit. No symbols have been loaded for this document.” warning?
- Is there an exponent operator in C#?
- How can I export data to an Excel file
- Could not establish trust relationship for SSL/TLS secure channel — SOAP
- How to convert an Stream into a byte[] in C#?
- Merge two (or more) lists into one, in C# .NET
- Why should I use IHttpActionResult instead of HttpResponseMessage?
- Convert integer to binary in C#
- HTML.ActionLink method
- Thread-safe List
property - Styles.Render in MVC4
- Exception from HRESULT: 0x800A03EC Error
- num1 >= 0x80 – What is 0x80?
- There is already an open DataReader associated with this Command which must be closed first
- Merging two arrays in .NET
- C# syntax to initialize custom class/objects through constructor params in array?
- Cannot implicitly convert type ‘int’ to ‘string’ C# class
- Unable to connect to any of the specified mysql hosts. C# MySQL
- String was not recognized as a valid DateTime ” format dd/MM/yyyy”
- Checking if a string array contains a value, and if so, getting its position
- Substring index and length must refer to a location within the string
- Difference between Method and Function?
- Case insensitive ‘Contains(string)’
- How to catch all exceptions in c# using try and catch?
- Getting “Handshake failed…unexpected packet format” when using WebClient.UploadFile() with “https” when the server has a valid SSL certificate
- Receiving access denied error from Visual Studio when trying to change target framework
- How to force garbage collector to run?
- CS1617: Invalid option ‘6’ for /langversion; must be ISO-1, ISO-2, 3, 4, 5 or Default
- Sort list by field (C#)
- How to post data to specific URL using WebClient in C#
- How would you count occurrences of a string (actually a char) within a string?
- Value does not fall within the expected range
- The type or namespace name could not be found
- How to execute a stored procedure within C# program
- The remote server returned an error: (405) Method Not Allowed. WCF REST Service
- TTS Voices for Windows7 64 bits
- How can I delete a file that is in use by another process?
- Convert double to float by cast or Convert.ToSingle()?
- Call a stored procedure with parameter in c#
- Type Checking: typeof, GetType, or is?
- C# Macro definitions in Preprocessor
- Adding images to buttons from Imagelist on a toolstrip?
- Multiple cases in switch statement
- System.ArgumentNullException: Value cannot be null. Parameter name: source
- Define: What is a HashSet?
- Convert string to decimal, keeping fractions
- Format of the initialization string does not conform to specification starting at index 0
- Sequence contains more than one element
- Drawing circles with System.Drawing
- Parser Error Message: Could not load type ‘sometype’
- Func vs. Action vs. Predicate [duplicate]
- the source file is different from when the module was built
- How to set session timeout in web.config