About BackgroundWorker Process
When we have a function that takes a long time and it freezes the screen, the user gets nervous. They simply don't like to lose the control. It would give a friendly impression when they can still click on the form while the application is busy with getting the information.
For example, when you start implementing asynchronous methods, you will wonder which technique is best suitable for your application. Would you use a Timer class or a Thread class or a Background Worker? This post uses a BackgroundWorker class to show how easy it is, and where we need to pay attention when using it.
- There are three event handler methods are available to call the method asynchronously.
1. DoWork Event Handler
The DoWork method is like any other event handler.
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// Very time-consuming process or method.
}
2.RunWorkerCompleted Method
This event handler is used to display the status of the worker process after completion. So we can use for to display “Success” or “Failed” of the Process.
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (Convert.ToBoolean(e.Result) == true)
MessageBox.Show("Process List Loaded Successfully!");
else
MessageBox.Show("Process List Load Failed!");
}
3.Progress Changed Method
This is used to display the status of the process. For example we will have a Progress bar and each and every step increase the Progress status.
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e){ }Some of other important Keys are:
1.e.Argument – Used to get the parameter reference received by RunWorkerAsync.
2.e.Result - Check to see what the BackgroundWorker processing did
3.backgroundWorker1.RunWorkerAsync(object)
Called to start a process on the worker thread.
4.DoWorkEventArgs e :
Contains e.Argument and e.Result, so it is used to access those properties.
How to Accessing the Win Forms Controls form the Background worker:
- We can access through the anonymous methods of C#. The bellow example shows how to access the win forms combo box control through the background worker process.
- In this example retrieve the current process name list available of your system.
- So first add the namespace to Process, Thread class
using System.Diagnostics;
using System.Threading;
- To call background worker’s RunWorkerAsync() method
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
- Next Background worker’s DoWorker Method
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// To call method directly
// bflg = LoadCombo();
// To call Using delegates
ToLoadCombo Tocall = new ToLoadCombo(LoadCombo);
e.Result = Tocall();
}
- Then Next Step we need to include LoadCombo()
//Public Method For Accessing Form Control (i.e CmbProcess)
public bool LoadCombo()
{
//Here is a simple way to update any UI controls from worker threads
// using anonymous methods and local variable capture.
try
{
new Thread(delegate() // anonymous methods
{
this.Invoke((ThreadStart)delegate() // anonymous methods Call
{
foreach (Process item in Process.GetProcesses())
{
cmbProcess.Items.Add(item.ProcessName.ToString());
}
cmbProcess.SelectedIndex = 0;
});
}).Start();
}
catch
{
return false;
}
return true;
}
- At Last we need to add RunWorkerCompleted Event Handler
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (Convert.ToBoolean(e.Result) == true)
MessageBox.Show("Process List Loaded Successfully!");
else
MessageBox.Show("Process List Load Failed!");
}
Using the above code we will develop faster and freezes the screen Application.



7 comments:
Hello Siva,
Just the information I was looking for. Thanks for the detailed instructions. I haven’t used it yet but I guess the time has come.
We are using Blue Prism V5. I don’t see the XML VBO in the VBO folders.
Blue prism - XML VBO?
We need to parse an XML file and retrieve all attributes and values. Can someone please help with this?
Probably we might need to code in VB. Not sure.
By the way do you have any YouTube videos, would love to watch it. I would like to connect you on LinkedIn, great to have experts like you in my connection (In case, if you don’t have any issues).
Please keep providing such valuable information.
Ciao,
Afreen
Hi Siva,
This is one awesome blog. Much thanks again. Fantastic.
I hope you can help, we have had a few instances where we have had iexplore program open in the background and the v worked is unable to close or access it therefore it is affecting our automation as it cannot open a new instance with it seeing one already open.
Our IT team seems to think it must be being open by a user with higher access rights than the v worker but this is not the case as no one accesses the machine.
How Iexplorer can implement?
Has anyone had this happen to them?
Thank you,
Jannu
Hi Bro,
Thank you. Absolutely perfect! I was SOOOOO excited after seeing the blog and these instructions were absolutely wonderful! You’re a life saver!
I have excel file that has 50 records. I would like to take only 10 records and save to collection stage and then looping for remaining records. Could you please help me to resolved the issue?
Looping the collection of 10 records in Excel sheet using the Blue Prism?
Anyways great write up, your efforts are much appreciated.
Best Regards,
Hello,
Thanks so much! This is a great time saver. You explained it very clearly:)
Can anyone please help me out how to use list region.
How to use List Region in Region Mode in blueprism?
Excellent tutorials - very easy to understand with all the details. I hope you will continue to provide more such tutorials.
Cheers,
Ajeeth Kapoor
Hi Siva,
Muchas Gracias Mi Amigo! You make learning so effortless. Anyone can follow you and I would not mind following you to the moon coz I know you are like my north star.
I think the worst I've done is come up and hit the bottom of the cabinet door and/or shelf above me, when I'm getting to the WIFI router and modem that we keep in the living room built-ins.
Plus, there's the walking into corners of walls (they jump out at me, I swear!), tripping over nothing, knocking devices off my desk when I'm reaching for them, tripping over cords and pulling the devices off the desk,
Once again thanks for your tutorial.
Thanks a heaps,
Lee
Hi There,
Thanks for the timely post, at least for me. I have encore time wondered about the transition, but somehow did not dare, because the information I came across very controversial.
But today, after reading your article I’m very confident to take up any challenges.
I need to change the storage location for Archive and log files from default location to my desired location, please suggest me how to do so.
Note: Appreciate if this solution can have meant for User machine but not for server machine.
Archive and log file storage need to change?
Anyways great write up, your efforts are much appreciated.
Cheers,
Kavya
Hello There,
Thank You so much for this blog. It helped me lot. I am a Technical Recruiter by profession and first time working on this technology was bit tough for me, this article really helped me a lot to understand the details to get started with.
I am using decision stage to check whether the age of a person is greater than 18.
Free Blue prism course in detroit I am using this expression DateDiff (0, [Coll1.Date of Birth], Today ()) > 18. Date of Birth is in date format. When I evaluate it using evaluate expression it shows the correct result for different cases. But while running my whole flow it is giving always result as True. It never fails. My data also contains the date of birth where age is less than 18. I am not able to understand what mistake I am doing.
Decision stage not working?
Please help me out.
By the way do you have any YouTube videos, would love to watch it. I would like to connect you on LinkedIn, great to have experts like you in my connection (In case, if you don’t have any issues).
Please keep providing such valuable information.
Thanks,
Preethi.
Post a Comment