UI freezes even process start with separate thread? - c#

In My application have time consuming process.There fore i try to do that operation in separate thread.Even i Stared it separate thread my Main UI still freezes during the time of long running process.But still i couldn't figure out the reason for that?Some thing wrong in my code?
My Event Hander Code
private void BtnloadClick(object sender, EventArgs e)
{
if (null != cmbSource.SelectedItem)
{
string selectedITem = ((FeedSource) cmbSource.SelectedItem).Url;
if (!string.IsNullOrEmpty(selectedITem))
{
Thread starter = new Thread(() => BindDataUI(selectedITem));
starter.IsBackground = true;
starter.Start();
}
}
private void BindDataUI(string url)
{
if (feedGridView1.InvokeRequired)
{
BeginInvoke(new Action(() => BindDataGrid(url)));
}
else
BindDataGrid(ss);
}
private void BindDataGrid(string selectedItem)
{
for (int i = 0; i < 10; i++)
{
//Time consuming Process
}
}

Your thread is completely useless :-)
In your thread you are executing BindDataUI which marshals the execution back to the UI thread using Invoke.
Your complete code is equivalent to this:
private void BtnloadClick(object sender, EventArgs e)
{
if (null != cmbSource.SelectedItem)
{
string selectedITem = ((FeedSource) cmbSource.SelectedItem).Url;
if (!string.IsNullOrEmpty(selectedITem))
{
BindDataGrid(selectedITem);
}
}
private void BindDataGrid(string selectedItem)
{
for (int i = 0; i < 10; i++)
{
//Time consuming Process
}
}
It would be better to only marshal these parts of BindDataGrid to the UI thread that really need to run on this thread because they need to update the UI.

Related

Background worker is not reporting progress Winforms

I have a background worker running, which is dynamically making form fields from an xml file. Depending on the size of the xml, it takes some time to load, so I am using a loading bar to report the progress to use so they won't exit out of the program. The program works as intended, it hides the loading panel and shows the form fields when the worker finishes, but while loading, the loading bar won't load. I received no errors.
This is where the report progress is being called:
if (!retrievePath.Equals(""))
{
// create the template with the data from the file
XDocument filledDoc = templateCreator.CreateTemplateWithGivenData2(retrievePath, fileName2);
tempDoc = filledDoc;
XElement root = tempDoc.Root;
// get child forms of return data state and sections
IDataInterface dataInterface = new DataInterfaceImplementation();
IEnumerable<XElement> sections = dataInterface.GetSections(filledDoc);
// Grab forms that aren't empty
IEnumerable<XElement> forms = XmlClass.GetMefForms(filledDoc).Where(u => u.Value != "").ToList();
IEnumerable<XElement> extra = dataInterface.GetSections(filledDoc).Where(u => u.Value != "").ToList();
// get the return header state
elemForms = dataMiddleman.GetSections(filledDoc);
foreach (XElement el in elemForms)
{
if (el.Name.LocalName.Equals("ReturnHeaderState"))
{
createForms(el, 3);
}
}
foreach (XElement el in forms)
{
i = i + 1;
i = (i / forms.Count()) * 100;
if (i == 100)
{
i = (i / (forms.Count() - 1)) * 100;
}
createForms(el, i);
}
private void createForms(XElement x, int i)
{
this.Invoke((MethodInvoker)delegate {
backgroundWorker1.ReportProgress(i);
var pLabel = new ParentLabel(x);
this.leftGroup.Controls.Add(pLabel);
var parentPanel = new CustomPanel(x);
parentPanel.SendToBack();
this.thebox.Controls.Add(parentPanel);
RecursiveTraverse(x, parentPanel);
pLabel.Click += (sender, e) => PLabel_Click(sender, e);
pPanels.Add(parentPanel);
});
}
This is my background worker code:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
loadingPanel.BringToFront();
populateNewFields();
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
loadingBar.Value = e.ProgressPercentage;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
loadingBar.Value = 100;
Thread.Sleep(100);
loadingPanel.SendToBack();
loadingBar.Value = 0;
}
Your question is about Background worker is not reporting progress Winforms and I hope it's ok if I use a Minimal Reproducible Example to demo how to successfully fire an event when progress occurs on the background thread (which is is one way to achieve the outcome you want) and reducing the complex Xml operations to a "time-consuming black box" to be dealt with as a separate issue.
This Form will provide a means to test the notification using the MockCreateForm method which mimics a form creation by blocking the background worker for 5 ms. I believe your design spec is to send a notification every 100 operations.
Generic event lacks the needed properties so inherit EventArgs to customize the info received (declaring it outside the MainForm class).
public delegate void ProgressEventHandler(ProgressEventArgs e);
public class ProgressEventArgs : EventArgs
{
public ProgressEventArgs(int count, int total)
{
Count = count;
Total = total;
}
public int Count { get; }
public int Total { get; }
}
When the button (actually a CheckBox where Appearance=Button) state is toggled, it calls this worker Task using a CancellationTokenSource and CancellationToken so it can be halted. Every 100 times, the Progress event is fired:
private void btnWorker_CheckedChanged(object sender, EventArgs e)
{
if(btnWorker.Checked)
{
_cts = new CancellationTokenSource();
Task.Run(() =>
{
var formCount = 10000;
for (int i = 0; i < formCount; i++)
{
if(_cts.IsCancellationRequested)
{
return;
}
// Notify every 100 times.
if((i % 100) == 0)
{
Progress?.Invoke(new ProgressEventArgs(count: i, total: formCount));
}
MockCreateForm();
}
Progress?.Invoke(new ProgressEventArgs(count: formCount, total: formCount));
}, _cts.Token);
}
else
{
_cts.Cancel();
labelStatus.Text = "Idle";
}
}
CancellationTokenSource _cts = null;
The only thing left is to consume the event in the MainForm. The only thing that needs to be marshalled back onto the UI thread is the brief moment that Label.Text is being updated.
public MainForm()
{
InitializeComponent();
Progress += (e) =>
{
Invoke((MethodInvoker)delegate
{
labelStatus.Text = $"{e.Count} of {e.Total}";
});
};
}
public event ProgressEventHandler Progress;
What you do on the background thread is up to you. Just put it here:
public void MockCreateForm()
{
Task.Delay(5).Wait();
}
I hope this gets you closer to what you are trying to achieve.

Why can I not recursively call a Worker_DoWork function?

I have a tcp server and client in c# using Sytem.Net.Sockets. My worker function is as following
private void Worker_DoWork(object sender, DoWorkEventArgs e)
{
byte[] buffer = new byte[1];
socket.Receive(buffer);
chatTextBox.Text = buffer[0].ToString();
}
I'm calling this after the server is created, as soon as it receives a message from the client it writes it on the screen and stops. My issue is I want it at the end to call itself again so it waits for another message to display. If I just add worker.RunWorkerAsync(); at the bottom it doesn't work, however if I just call it from another button it works and receives and writes the message.
The BackgroundWorker "wraps" around a Thread and helps you with all the plumbing. It is a horribly dated approach to Multitasking taht you should not use in producive code anymore. However it is also the best "Training Wheels" for Multitasking I know off.
Your fundamental approach is flawed. The BGW not allowing that is really just it helping you learn the things you need to learn. A list of mistakes in your code:
you are accessing a GUI element directly in DoWork. Do not do that. Only write the UI in ReportProgress and RunWorker completed Events. This is a general Multithreading rule, that is helped by teh BGW's design.
you are trying to restart the thread, before it has finished. If you wanted to restart it, RunWorkerCompleted would be the right place
however more sensible would be for the core of the BackgroundWorker to be a semi-infinite loop. Something that runs until canceled. Normally handing out inforamtion with reporting is not a good idea, but in this case it is the best idea I have.
The only other thing I can give you, is my old BGW example code:
#region Primenumbers
private void btnPrimStart_Click(object sender, EventArgs e)
{
if (!bgwPrim.IsBusy)
{
//Prepare ProgressBar and Textbox
int temp = (int)nudPrim.Value;
pgbPrim.Maximum = temp;
tbPrim.Text = "";
//Start processing
bgwPrim.RunWorkerAsync(temp);
}
}
private void btnPrimCancel_Click(object sender, EventArgs e)
{
if (bgwPrim.IsBusy)
{
bgwPrim.CancelAsync();
}
}
private void bgwPrim_DoWork(object sender, DoWorkEventArgs e)
{
int highestToCheck = (int)e.Argument;
//Get a reference to the BackgroundWorker running this code
//for Progress Updates and Cancelation checking
BackgroundWorker thisWorker = (BackgroundWorker)sender;
//Create the list that stores the results and is returned by DoWork
List<int> Primes = new List<int>();
//Check all uneven numbers between 1 and whatever the user choose as upper limit
for(int PrimeCandidate=1; PrimeCandidate < highestToCheck; PrimeCandidate+=2)
{
//Report progress
thisWorker.ReportProgress(PrimeCandidate);
bool isNoPrime = false;
//Check if the Cancelation was requested during the last loop
if (thisWorker.CancellationPending)
{
//Tell the Backgroundworker you are canceling and exit the for-loop
e.Cancel = true;
break;
}
//Determin if this is a Prime Number
for (int j = 3; j < PrimeCandidate && !isNoPrime; j += 2)
{
if (PrimeCandidate % j == 0)
isNoPrime = true;
}
if (!isNoPrime)
Primes.Add(PrimeCandidate);
}
//Tell the progress bar you are finished
thisWorker.ReportProgress(highestToCheck);
//Save Return Value
e.Result = Primes.ToArray();
}
private void bgwPrim_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
pgbPrim.Value = e.ProgressPercentage;
}
private void bgwPrim_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
pgbPrim.Value = pgbPrim.Maximum;
this.Refresh();
if (!e.Cancelled && e.Error == null)
{
//Show the Result
int[] Primes = (int[])e.Result;
StringBuilder sbOutput = new StringBuilder();
foreach (int Prim in Primes)
{
sbOutput.Append(Prim.ToString() + Environment.NewLine);
}
tbPrim.Text = sbOutput.ToString();
}
else
{
tbPrim.Text = "Operation canceled by user or Exception";
}
}
#endregion

ReportProgress doesn't call progressChanged with tasks in c#

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
int currentProgress=-1;
while (currentProgress<length)
{
currentProgress=Worker.progress;
backgroundWorker1.ReportProgress(currentProgress);
Thread.Sleep(500);
length = Worker.UrlList.Count;
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
int ix = e.ProgressPercentage;
progressBar1.Value = ix;
lblText.Text =ix+" %";
}
I wrote a program to download page sources by reading a file have about 1000 URLs. so I used Tasks to download pages async. here Worker.progress is the currently executed URL amount. though the debuger hits the backgroundWorker1.ReportProgress(currentProgress); it never enter to the backgroundWorker1_ProgressChanged.
private void StartButton_Click(object sender, EventArgs e)
{
t.makeUrlList(inputFile);
backgroundWorker1 = new BackgroundWorker();
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.DoWork += backgroundWorker1_DoWork;
backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged;
backgroundWorker1.RunWorkerAsync();
t.RunTasks();
Application.Exit();
}
background worker initializes when start button clicks...
here is where my tasks are created....
public void RunTasks()
{
if (numOfTasks > UrlList.Count)
numOfTasks=UrlList.Count-1;
Task[] t = new Task[numOfTasks];
int j = 0;
while ( j < UrlList.Count-1)
{
for (int i = 0; (i < t.Count())&&(j<UrlList.Count-1); i++)
{
try
{
if (t[i].IsCompleted || t[i].IsCanceled || t[i].IsFaulted)
{
t[i] = Task.Run(() => FindWIN(j));
j++;
progress = j;
}
}
catch (NullReferenceException ex)
{
t[i] = Task.Run(() => FindWIN(j));
j++;
progress = j;
}
}
}
}
If you want to BackgroundWorker supports updating progress information, the value of WorkerReportsProgress should be set to true . If this property is true , the user code can call ReportProgress for initiating event ProgressChanged .
Background worker initialization:-
backgroundWorker1 = new BackgroundWorker();
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.DoWork+=backgroundWorker1_DoWork;
backgroundWorker1.ProgressChanged+=backgroundWorker1_ProgressChanged;
backgroundWorker1.RunWorkerAsync();
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
int currentProgress = -1;
decimal length=1000;
while (currentProgress < length)
{
currentProgress = Worker.progress;
backgroundWorker1.ReportProgress(currentProgress);
Thread.Sleep(500);
length = Worker.UrlList.Count;
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) {
int ix = e.ProgressPercentage;
progressBar1.Value = ix;
lblText.Text = ix + " %";
}
See the demo code below. This is mostly untested, and certainly isn't 'production standard', but it should give you a good start!
It uses a ConcurrentQueue to hold the list of URLs to be processed. This is threadsafe, and makes life a lot easier.
It has a configurable number of urls and tasks. It's best not to make 1000 tasks, but instead have a queue of work items, and a smaller pool of Tasks which 'pull items' off the queue until it's empty. This means you can performance test different numbers of Tasks and find the best value for your problem.
It uses Invoke when updating the progress bar - this avoids the cross-thread exception.
No BackgroundWorker - just TaskFactory and Task
public partial class Form1 : Form
{
private const int UrlCount = 1000;
private const int taskCount = 10;
private ConcurrentQueue<string> urlList;
private List<Task> taskList;
public Form1()
{
InitializeComponent();
}
private void ResetQueue()
{
// fake up a number of strings to process
urlList = new ConcurrentQueue<string>(Enumerable.Range(0, UrlCount)
.Select(i => "http://www." + Guid.NewGuid().ToString() + ".com"));
}
private void button1_Click(object sender, EventArgs e)
{
ResetQueue();
var taskFactory = new TaskFactory();
// start a bunch of tasks
taskList = Enumerable.Range(0, taskCount).Select(i => taskFactory.StartNew(() => ProcessUrl()))
.ToList();
}
void ProcessUrl()
{
string current;
// keep grabbing items till the queue is empty
while (urlList.TryDequeue(out current))
{
// run your code
FindWIN(current);
// invoke here to avoid cross thread issues
Invoke((Action)(() => UpdateProgress()));
}
}
void FindWIN(string url)
{
// your code here
// as a demo, sleep a sort-of-random time between 0 and 100 ms
Thread.Sleep(Math.Abs(url.GetHashCode()) % 100);
}
void UpdateProgress()
{
// work out what percentage of the queue is processed
progressBar1.Value = (int)(100 - ((double)urlList.Count * 100.0 / UrlCount));
}
}
You should set WorkerReportsProgress property of your worker to true on initialization stage.

Async Behaviour

I have the following code to update the progress bar in async fashion and i notice
its async behaviour through the call to MessageBox.In this case it works perfectly
but when i give a sleep of 1s(1000) the MessageBox doesnot pops up and the the complete progress bar fills at once.
Kindly tell why this is happening.
private void button1_Click(object sender, EventArgs e)
{
Update_Async async = new Update_Async(Update_Async_method);
progressBar1.BeginInvoke(async,10);
MessageBox.Show("Updation In Progress");
}
public void Update_Async_method(int a)
{
this.progressBar1.Maximum = a;
for (int i = 1; i <= a; i++)
{
progressBar1.Value = a;
Thread.Sleep(10);
//Thread.Sleep(1000);
}
}
Try Update_Async.BeginInvoke(async, 10) instead if you want the delegate to run asynchrnously but, you'll have to cross thread checking on the update to the progress bar.
In response to your comment, very similar to what you are doing already,
void UpdatingFunction(int value)
{
if (this.progressBar.InvokeRequired)
{
this.progressBar.BeginInvoke(UpdatingFunction, value);
return;
}
// Invoke not required, work on progressbar.
}
This also explains what the Invoke methods on controls are for.
Delegate.BeginInvoke will run a method in a thread once and then dispose it. It is a poor choice if you want to repeatedly do some work in a thread and return intermediate results. If that is what you want, you should use BackgroundWorker. Highly abbreviated snippet:
BackgroundWorker bw;
YourFormConstructor()
{
...
bw = new BackgroundWorker();
bw.WorkerReportsProgress = true;
bw.DoWork += BackgroundCalculations;
bw.ProgressChanged += ShowBackgroundProgress;
}
private void button1_Click(object sender, EventArgs e)
{
bw.RunWorkerAsync(10);
}
void ShowBackgroundProgress(object sender, ProgressChangedEventArgs e)
{
this.progressBar.Value = e.ProgressPercentage;
}
static void BackgroundCalculations(object sender, DoWorkEventArgs e)
{
BackgroundWorker bw = sender as BackgroundWorker;
int max = (int)e.Argument;
for (int i = 0; i < max; i++)
{
bw.ReportProgress(i * 100 / max);
Thread.Sleep(10);
}
bw.ReportProgress(100);
}
}

c# BackgroundWorker DoWork method calling another class and ProgressReport

I am running a BackgroundWorker thread to do a time consuming task. The time consuming task is in another class. I need to pass the progress being made on this separate class running on BackgroundWorker back to the Main Form1 class. I am not sure how to approach this. Please provide suggestions. Thank you in advance.
**// Main Form1 UI Class**
public void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
{
//e.Argument always contains whatever was sent to the background worker
// in RunWorkerAsync. We can simply cast it to its original type.
DataSet ds = e.Argument as DataSet;
this.createje.ProcessData(this.ds);
}
private void backgroundWorker2_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.progressBar1.Minimum = 0;
this.progressBar1.Maximum = CreateJE.max;
this.progressBar1.Value = e.Recno;
}
**//Other Class called CreateJE**
public void ProcessData(DataSet ds)
{
//Do time consuming task...
for (int i = 1; i <= 10; i++)
{
if (worker.CancellationPending == true)
{
e.Cancel = true;
break;
}
else
{
// Perform a time consuming operation and report progress.
System.Threading.Thread.Sleep(500);
**//How do I report progress back to the Main UI?**
//worker.ReportProgress(i * 10);
}
}
}
The cleanest, and most extendable, solution is probably to have your ProcessData() method raise an event, which the BackgroundWorker is listening for. This way ProcessData() doesn't depend on having a BackgroundWorker as a caller. (You would also need to make a way of canceling out of ProcessData()). You can even re-use the ProgressChangedEventArgs if you want. For example (not tested but you get the idea?):
partial class Form1 {
public void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e) {
//e.Argument always contains whatever was sent to the background worker
// in RunWorkerAsync. We can simply cast it to its original type.
DataSet ds = (DataSet)e.Argument;
var bgw = (BackgroundWorker)sender;
var eh = new ProgressChangedEventHandler((o,a) => bgw.ReportProgress(a.ProgressPercentage));
createje.ProgressChanged += eh;
this.createje.ProcessData(this.ds));
createje.ProgressChanged -= eh; //necessary to stop listening
}
private void backgroundWorker2_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.progressBar1.Minimum = 0;
this.progressBar1.Maximum = CreateJE.max;
this.progressBar1.Value = e.ProgressPercentage;
}
}
partial class CreateJE {
public event ProgressChangedEventHandler ProgressChanged;
protected virtual void OnProgressChanged(ProgressChangedEventArgs e)
{
var hand = ProgressChanged;
if(hand != null) hand(this, e);
}
public void ProcessData(DataSet ds)
{
for(int i = 1; i <= 10; i++)
{
// Perform a time consuming operation and report progress.
System.Threading.Thread.Sleep(500);
var e = new ProgressChangedEventArgs(i * 10, null);
}
}
}
The quick and dirty way is to just pass the BackgroundWorker as a parameter to ProcessData(). This is IMHO rather ugly, though, ties you down to using only BackgroundWorkers, and also forces you to define the BackgroundWorker in one place (the main form class) and the returned values of ReportProgress in another (the CreateJE class).
You could also use a timer and report back progress every X ms, querying the CreateJE object for its progress. This seems in-line with the rest of your code. The hangup with this is it would make your CreateJE class not multi-thread-friendly.
The quickest and simplest option would be to declare a delegatein class CreateJE that will report proggress and then hook this to the ReportProgress method of BackgroundWorker.
class CreateJE
{
public Action<int> ReportProgressDelegate{get;set;}
public void ProcessData(DataSet ds)
{
for(int i = 0; i < 10; i++)
{
Thread.Sleep(500);
ReportProgress(i*10);
}
}
private void ReportProgress(int percent)
{
if(ReportProgressDelegate != null)
ReportProgressDelegate(percent);
}
}
In your form, initialize ReportProgressDelegate property of your instance (I assume this.createje refers to a field of the form so OnLoad seems like a good place to do the initialization):
protected override void OnLoad(EventArgs e)
{
this.creatje.ReportProgressDelegate = worker.ReportProgress;
}
After that, you can use the event handlers you already have (backgroundWorker2_DoWork and backgroundWorker2_DoWork).
PS: You should use the same approach with worker.CancellationPending property.

Categories

Resources