calling BackGround Worker from different class

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,
I have to do a time consuming task using background worker, at the same time i also need to write some information to some text boxes
I have a class for the background worker
class BackGroundWork
{
public BackgroundWorker bw = new BackgroundWorker();
public Form1 objFrm;

public BackGroundWork()
{
InitializeBackGroundWorker();
}

private void InitializeBackGroundWorker()
{
bw.WorkerSupportsCancellation = true;
bw.WorkerReportsProgress = true;
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.Disposed += new EventHandler(bw_Disposed);
}

void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
Debug.Print("Work Completed by Worker", e.Result);
}

void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
Debug.Print("Writing from Progress " + e.ProgressPercentage.ToString());
}

void bw_DoWork(object sender, DoWorkEventArgs e)
{
AsyncDemo aDemo = new AsyncDemo(objFrm);
aDemo.PeformAsyncOp();
}

void bw_Disposed(object sender, EventArgs e)
{
Debug.Print("BackGroundWorker is Disposed" +e.ToString());
}
This is my code when the button is clicked
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();

BackGroundWork objbw = new BackGroundWork();
if (objbw.bw.IsBusy != true)
{
objbw.bw.RunWorkerAsync();
}
}
Then i have this class which does the time consuming process and at the same time write to the text boxesdelegate double DelPerformCalc(int value,out string text,out string text1);

class AsyncDemo
{
public Form1 objFrm;

public string myText;
public string myText1;

public AsyncDemo(Form1 frm)
{
this.objFrm = frm;
}

public double PerformCalculation(int i,out string mytext, out string mytext1)
{
double value = 0;
Random r = new Random();
for (double j = 0; j < 100000; j++)
{
value += j;

Debug.Print("The time now is " + DateTime.Now.Minute + ":" + DateTime.Now.Millisecond);
Debug.Print("Running on Thread " + Thread.CurrentThread.ManagedThreadId);
Debug.Print("The value is " + j.ToString());

writetoText("The time now is" + DateTime.Now.Minute + ":" + DateTime.Now.Millisecond + Environment.NewLine);
writetoText1("Running on Thread " + Thread.CurrentThread.ManagedThreadId + Environment.NewLine);

}
mytext = objFrm.TextBoxText;
mytext1 = objFrm.TextBox1Text;
myText = mytext;
myText1 = mytext1;
return value;
}

private string writetoText1(string text)
{
objFrm.TextBox1Text = objFrm.TextBox1Text + text;
return objFrm.TextBox1Text;
}

private string writetoText(string text1)
{
objFrm.TextBoxText = objFrm.TextBoxText + text1;
return objFrm.TextBoxText;
}

public void PeformAsyncOp()
{
DelPerformCalc delPerCalc = new DelPerformCalc(PerformCalculation);

string text = objFrm.TextBoxText;
string text1 = objFrm.TextBox1Text;

IAsyncResult result = delPerCalc.BeginInvoke(2, out text, out text1, null, null);
Thread.Sleep(1000);
result.AsyncWaitHandle.WaitOne();
double myReturnedvalue = delPerCalc.EndInvoke(out text,out text1,result);

Debug.Print("My calculate value is " + myReturnedvalue);
objFrm.TextBoxText = "My calculate value is " + myReturnedvalue + "n";

}

public string Write()
{
//objFrm.TextBoxText = "Thomson";
//return objFrm.TextBoxText;
return "T";
}

internal string Write1()
{
//objFrm.TextBox1Text = "Reuters";
//return objFrm.TextBox1Text;
return "R";
}
}
But i get this error my frm object is also null, can someone plz help me on this
litu Here

View the full article
 
Back
Top