Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
How to call backgroundworker from backgroundworker in c#? Also want to know about how to handle multiple thread in c#?
Posted
Comments
Pheonyx 25-Jul-13 5:45am    
Why do you want to call a background worker from a background worker? Please explain the context.
As for "handle multiple thread in c#" there are hundreds of articles/examples if you use google that show different approaches to this.
Makarand Borawake 25-Jul-13 6:37am    
I want to show copying process on progress bar and also show progress for it. That's why I use background worker. Need to call a background worker from a background worker because now I want to start actually copying process from that background worker and also want to pass source and destination path to 2nd background worker. Need help

The BackgroundWorker class can make threading simple to use because it marshals the ProgressChanged and RunWorkerCompleted events to the UI thread. BUT waiting to catch you out is the poorly documented requirement that the RunWorkerAsync method must be called from the UI thread.

If RunWorkerAsync is called from a non UI thread then the events will not be marshalled and will be raised on ThreadPool threads. In this situation accessing a Control directly will cause a cross threading exception and if ProgressChanged events are raised at a high rate they may arrive out of order.

So yes, you can start one BackgroundWorker from another but will probably wish that you hadn't.

Alan.
 
Share this answer
 
Comments
MarceloSDiniz 24-Jan-21 21:09pm    
Indeed, very poorly documented. Saved me a lot of work with your direction. Thank you very much...
Nothing special here. You call a BackgroundWorker from another BackgroundWorker the exact same way you would call it from the main thread: via the RunWorkerAsync()[^] method.

Or use the RunWorkerAsync(object)[^] overload.

The BackgroundWorker then fires its DoWork[^] event. Subscribe a method to it in which you do the actual work. The method has to conform to a DoWorkEventHandler[^], which means its return type is void[^] and it needs two parameters:

The first is sender of type object[^], which actually is a reference to the calling BackgroundWorker itself.

Second is a DoWorkEventArgs[^], which encapsulates Argument[^], again of type object. That's a reference to the exact same argument you gave the RunWorkerAsync(object) overload.
 
Share this answer
 
v2
Comments
Makarand Borawake 25-Jul-13 6:30am    
ok..Thanks!!
If now I to pass 2 variable for inner backgroundworker, is it possoble from RunWorkerAsync() or is there any way for passing variable.
lukeer 25-Jul-13 8:03am    
I updated my solution.
A BackgroundWorker thread is just another thread: so anything you can do in a "normal" thread you can do in a BackgroundWorker (except access UI components, of course) - and that includes starting new threads.
The only complexity you might find is keeping track of which thread code is in: but that is pretty easy to handle since the BackgroundWorker instance is passed through to the DoWork event handler as the sender parameter, and just needs casting to a BackgroundWorker.

Be aware though that not all data items are thread safe: if you are multiplying threads you may need to start using locks and semaphores to prevent data corruption.
 
Share this answer
 
See this link

http://winstonmadiano.wordpress.com/2013/06/04/vb-net-multi-threading-using-background-worker-and-delegate/[^]

C# Code

C#
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
using System.Linq;
using System.Xml.Linq;
public partial class frmBackGroundWorker
{
	private delegate void progressDelegate(string progress);

	private void BackgroundWorker1_DoWork(System.Object sender, System.ComponentModel.DoWorkEventArgs e)
	{
		for (int i = 0; i <= 50000; i++) {
			progressDelegate m_UpdateText = null;
			m_UpdateText = new progressDelegate(progress1);
			this.Invoke(m_UpdateText, i.ToString());
		}
	}

	private void BtnStartCounting_Click(System.Object sender, System.EventArgs e)
	{
		ProgressBar1.Visible = true;
		btnSayHello.Enabled = true;
		BackgroundWorker1.RunWorkerAsync();

	}

	private void progress1(string progress)
	{
		Label1.Text = progress;
		//Me.Text = progress
		ProgressBar1.Minimum = 0;
		ProgressBar1.Maximum = 50000;
		ProgressBar1.Value = Convert.ToInt32(progress);
	}

	private void BackgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
	{
		Interaction.MsgBox("Done proccessing");
		btnSayHello.Enabled = false;
		lblHelloOutput.Text = "";
		ProgressBar1.Visible = false;
	}

	private void btnSayHello_Click(System.Object sender, System.EventArgs e)
	{
		lblHelloOutput.Text = "Hello" + Environment.UserName + ", I can perform other task while the thread is running.";
	}
	public frmBackGroundWorker()
	{
		InitializeComponent();
	}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900