private void ProcessNumbersBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) { // Get the incoming AddParam object. AddParams args = (AddParams)e.Argument; // Artifical lag. System.Threading.Thread.Sleep(5000); // Return the value. e.Result = args.a + args.b; }
private void btnProcessData_Click(object sender, EventArgs e) { try { // First get the user data (as numerical). int numbOne = int.Parse(txtFirstNumber.Text); int numbTwo = int.Parse(this.txtSecondNumber.Text); AddParams args = new AddParams(numbOne, numbTwo); // Now spin up the new method and pass args variable. ProcessNumbersBackgroundWorker.RunWorkerAsync(args); } catch (Exception ex) { MessageBox.Show(ex.Message); } }