Background Worker Crashes Due to too Much Processing? - c#

I've been struggling with a Background Worker for sometime, and I'm beginning to wonder if there are limits to what one can do with a bw. I'm trying to utilize a bw to handle a TCPIP exchange while updating the UI using its ProgressChanged Method. I know the UI update is ok, but my DoWork routine (shown below) sometimes causes the bw thread to disappear/stop working. Has anyone else had this problem?
private void TCPIP_DoWork(object sender, DoWorkEventArgs e)
{
int a = 0;
s.Send(System.Text.Encoding.ASCII.GetBytes("s"));
if (worker.CancellationPending == true)
{
s.Send(System.Text.Encoding.ASCII.GetBytes("t"));
}
else
{
try
{
a = s.Available;
s.Receive(bytes);
Thread.Sleep(25);
using (Stream fileStream = new FileStream(#sbpFile.Text,
FileMode.Append, FileAccess.Write, FileShare.None))
{
using (BinaryWriter bw = new BinaryWriter(fileStream))
{
if (a == 0)
Thread.Sleep(20);
else if (a < 1023)
{
bw.Write(bytes, 0, a);
Thread.Sleep(20);
}
else
{
bw.Write(bytes, 0, 1024);
Thread.Sleep(20);
}
}
}
}
catch(Exception e)
{
Console.WriteLine("{0} Exception.", e);
}
}
}
NOTE: The only reason those Thread.Sleep() operations are in there are because they seem to be the temporary fix for having the bw not trip over itself...

Try to check Error property on RunWorkerCompleted event handler. Maybe you get some exception, which in not handled by your code.
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
Console.WriteLine("{0} Exception.", e.Error);
// etc
}

Related

Reading data from serial Port is loosing packages

Hello i created a program which reads serial data from a scoreboard, then depending on the string the program separates the data into different boxes on the form and then to different txt files.The purpose of this, is to use those txt files for livestreaming purposes in basketball games.
It's the first time i work with serial data and i am not a very experienced programmer.
My problem, as the title of this post suggests is that every now and then without any reason i am loosing some packages. This is happening randomly , for example in 10 second period i could 1 package while the next one none or 4.
private void ReadData()
{
Thread MyThread = null;
{
try
{
ThreadStart ThreadMethod = new ThreadStart(ReadFromPort);
MyThread = new Thread(ThreadMethod);
}
catch (Exception e)
{
Console.WriteLine("Failed to create thread! " + e.Message);
return;
}
try
{
MyThread.Start();
}
catch (Exception e)
{
Console.WriteLine("The thread failed to start! " + e.Message);
}
}
}
//Recieves data and write them on textbox (optionally on a txt)
private void ReadFromPort()
{
while (Receiver == true)
{
try
{
int count = ComPort.BytesToRead;
System.Windows.Forms.Application.DoEvents();
byte[] data = new byte[count];
ComPort.Read(data, 0, data.Length);
currentMessage = Combine(currentMessage, data);
ReceivedData = (BitConverter.ToString(data));
if (count > 0)
{
if (chBoxUpdate.Checked)
{
DataType = count;
tempData = ReceivedData;
this.Invoke(new EventHandler(DisplayText));
if (chboxTxt.Checked)
{
this.Invoke(new EventHandler(ExportData));
}
}
else if (chBoxPrevious.Checked)
{
DataType = count;
tempData = ReceivedData;
this.Invoke(new EventHandler(ClearData));
this.Invoke(new EventHandler(DisplayText));
if (chboxTxt.Checked)
{
this.Invoke(new EventHandler(ExportData));
}
}
}
}
catch (Exception e)
{
}
}
}
//Displays Text
private void DisplayText(object sender, EventArgs e)
{
string temp;
Console.WriteLine(tempData+ " (" + tempData.Length.ToString() + ")");
try
{
if (tempData.Length == 38)// && ReceivedData.Substring(12, 5) == "03-02")
{
if (tempData.Substring(12, 5) == "03-02")
{
DataText.AppendText(tempData.Substring(24, 8));
DataText.AppendText("\n");
Blink.Text = "Reading...";
timer1.Start();
timer1.Enabled = true;
}
}
else
if (tempData.Length == 35)
{
if (tempData.Substring(12, 5) == "45-02")
{
AttackTime.AppendText(tempData.Substring(24, 5));
Blink.Text = "Reading...";
AttackTime.AppendText("\n");
timer1.Start();
timer1.Enabled = true;
}
}
else
if (tempData.Length == 29)
{
if (tempData.Substring(12, 5) == "03-36")
{
HomeScore.AppendText(tempData.Substring(21, 2));
Blink.Text = "Reading...";
HomeScore.AppendText("\n");
timer1.Start();
timer1.Enabled = true;
}
else
if (tempData.Substring(12, 5) == "03-46")
{
AwayScore.AppendText(tempData.Substring(21, 2));
Blink.Text = "Reading...";
AwayScore.AppendText("\n");
timer1.Start();
timer1.Enabled = true;
}
}
}
catch (ArgumentOutOfRangeException)
{
}
}
Keep in mind that tempData and ReceivedData are the same here and as the programs appears now there's not any particular reason to set tempData=ReceivedData.This is a part of a different,older code i used in the begining which i never changed but it doesn't effect the program.
At first i am using a thread to enable the ReadFromPort then if the program finds that there are available data in line it displays them with the DisplayText and it's using the ExportData to export the data to a txt.I think the problem is somewhere there but as i am not very experienced i can't tell where.
Are there any suggestions on how to improve my code? If you more details or information i can provide them.
You are using Invoke, this blocks the caller thread until the event has been processed, and since you are updating the UI this can take some time. This probably causes some buffer to overflow and data to be discarded. So you should do as little work as possible on the reading thread.
There are a few alternatives
Put the data on a concurrentQueue, and let the UI thread have a timer that periodically triggers a method that reads from the queue and updates the UI.
Put the data on a concurrentQueue, wrapped in a blocking collection, have another background thread iterate over the blocking collection, using GetConsumingEnumerable, and post the result to the main thread once done. This would be suitable if there is significant processing to be done.
Use BeginInvoke to post the received data to the main-thread, this does not wait for the call to complete, but probably have slightly higher overhead that the previous alternatives. I would recommend not accessing any UI properties from the background thread, since by default no property of a UI class is threadsafe. Even if you might get away with it if you are only reading, I would not take the chance.

c# unexpected backgroundWorker behaviour

I have attached a backgroundWorker component to my main form which runs a parallel task of capturing the screen for an animated gif. The worker's function has a while loop that runs until I use CancelAsync() on the worker, at which point it exits the loop, does some other things like saving the gif file and so on and returns some results to the UI thread.
private bool capturing = false;
public MainForm()
{
InitializeComponent();
backgroundWorker.DoWork += backgroundWorker_DoWork;
backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted;
backgroundWorker.WorkerSupportsCancellation = true;
}
private void captureBtn_Click(object sender, EventArgs e)
{
Debug.WriteLine("Button clicked");
if (capturing) { return; }
if (!backgroundWorker.IsBusy)
{
backgroundWorker.RunWorkerAsync();
}
}
private void stopCaptureBtn_Click(object sender, EventArgs e)
{
if (backgroundWorker.IsBusy)
{
backgroundWorker.CancelAsync();
}
}
private void backgroundWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
capturing = true;
Debug.WriteLine("DoWork running");
while (!backgroundWorker.CancellationPending)
{
Debug.WriteLine("Capturing frame {0}", frames);
//do the capturing to memory stream
}
Debug.WriteLine("DoWork cancelled");
//do some other things like saving the gif etc
e.Result = someResult;
}
private void backgroundWorker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
Debug.WriteLine("RunWorkerCompleted running");
capturing = false;
//does something with the e.Result
}
My console output during a normal test is something like this:
Button clicked
DoWork running
Capturing frame 0
Capturing frame 1
Capturing frame 2
Capturing frame 3
Capturing frame 4
Capturing frame 5
Cancel button clicked
DoWork cancelled
The thread 0x2e4c has exited with code 0 (0x0).
DoWork running
DoWork cancelled
The thread 0x1010 has exited with code 0 (0x0).
RunWorkerCompleted running
The function seems to be running twice, I can see 2 separate threads exiting and also I don't seem to get any results from capturing. If I set a breakpoint inside the backgroundWorker_DoWork function and continue later, the first run does the capturing normally. What could be going on?
Its getting called twice because binding the event second time right after InitializeComponent().
Just comment those lines and it should work fine.
Here's the same example without the issue of running twice.
Sample Output
...
...
...
Capturing frame 2632
Capturing frame 2633
Capturing frame 2634
DoWork cancelled
RunWorkerCompleted running
public partial class Form1 : Form
{
private bool capturing = false;
public Form1()
{
InitializeComponent();
backgroundWorker1.WorkerSupportsCancellation = true;
// Don't need to re-bind
//backgroundWorker1.DoWork += backgroundWorker1_DoWork;
//backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;
//backgroundWorker1.WorkerSupportsCancellation = true;
}
private void captureBtn_Click(object sender, EventArgs e)
{
Debug.WriteLine("Button clicked");
if (capturing) { return; }
if (!backgroundWorker1.IsBusy)
{
backgroundWorker1.RunWorkerAsync();
}
}
private void stopCaptureBtn_Click(object sender, EventArgs e)
{
if (backgroundWorker1.IsBusy)
{
backgroundWorker1.CancelAsync();
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
capturing = true;
Debug.WriteLine("DoWork running");
int frames = 1;
while (!backgroundWorker1.CancellationPending)
{
Debug.WriteLine("Capturing frame {0}", frames);
//do the capturing to memory stream
frames++;
}
Debug.WriteLine("DoWork cancelled");
//do some other things like saving the gif etc
//e.Result = someResult;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
Debug.WriteLine("RunWorkerCompleted running");
capturing = false;
//does something with the e.Result
}
The thread 0x3108 has exited with code 0 (0x0) means there was no error.
While reading or writing the big data, you should divide it into parts. Otherwise, you cannot progress. Your current while loop is disabled when writing to memory stream.
so your backgroundWorker_DoWork method should be like this :
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
var worker = (BackgroundWorker)sender;
// this is an example file
string filePath = #"C:\file.gif";
// it determines how many bytes of data will be received from the stream each time.
byte[] buffer = new byte[4096];
int byteCount = 0;
// we are preparing to read stream.
using (FileStream fs = new FileStream(filePath, FileMode.Open, System.IO.FileAccess.Read))
using (MemoryStream ms = new MemoryStream())
{
// read the stream bytes and fill buffer
while ((byteCount = fs.Read(buffer, 0, buffer.Length)) > 0)
{
// if the task was canceled
if (worker.CancellationPending)
{
e.Cancel = true;
break;
}
// write buffer to memory stream
ms.Write(buffer, 0, byteCount);
}
}
}

BackgroundWorker not starting again when once finished

I have a WinForms application which uses a backgroundworker for downloading images from given urls. For the download I use a backgroundworker.
The application is running fine when started, and the download happens as planned, but when the worker is done and I click the downloadbutton again to start downloading from another url, the backgroundworker doesn't do anything.
I fixed that problem temporarily by calling application.restart() when the worker is done, which works but can't be here longer than it has to.
Worker-Code:
// initialization of worker is done in constructor of my class
downloadWorker.WorkerReportsProgress = true;
downloadWorker.WorkerSupportsCancellation = true;
downloadWorker.DoWork += new DoWorkEventHandler(worker_doWork);
downloadWorker.ProgressChanged += new ProgressChangedEventHandler(worker_progressChanged);
downloadWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_runWorkerCompleted);
// ...
private void worker_doWork(object sender, DoWorkEventArgs e)
{
WebClient downloadClient = new WebClient();
HttpWebRequest HttpReq = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response;
try
{
response = (HttpWebResponse)HttpReq.GetResponse();
}
catch (WebException ex)
{
response = (HttpWebResponse)ex.Response;
}
if (response.StatusCode == HttpStatusCode.NotFound)
MessageBox.Show("Website not found");
if (response.StatusCode == HttpStatusCode.OK)
{
for(int i=0; i<3;i++)
{
string image = getImageUrl(url,i);
downloadWorker.ReportProgress(i);
image = WebUtility.HtmlDecode(image);
string saveName = "img_"+i+".png";
try
{
downloadClient.DownloadFile(image, saveName);
}
catch (Exception ex)
{
MessageBox.Show(ex.StackTrace);
}
}
}
}
private void worker_progressChanged(object sender, ProgressChangedEventArgs e)
{
rtxtStatus.AppendText("Downloade Image" + e.ProgressPercentage + " of 3");
}
private void worker_runWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("Download completed");
}
edit:
if (e.Error != null)
{
MessageBox.Show(e.Error.ToString());
}
To avoid any misunderstandings: The backgroundWorker is definetely running at the second time, and it is not an error of the reportProgress-method, since I get the same thing when I dont report anything.
edit no. 2:
I found out where the error came from: at the second run, the for-loop is completely skipped. But that doesn't make any sense for me either... There can't be any other value still be in because I have a completely new instance of the class, can it? But anyway, if it just skipped the method the worker still should exit which it doesn't do. For testing, I added a MessageBox after the for-loop, which is not executed after the second run.

BeginInvoke cause application hang in BackgroundWorker

I was trying to solve the problem in this Question but I ended up having another problem
in short words that question was asking how to load a huge file into textBox chunk by chunk,
so in back ground worker Do_work event I did this:
using (FileStream fs = new FileStream(#"myFilePath.txt", FileMode.Open, FileAccess.Read))
{
int bufferSize = 50;
byte[] c = null;
while (fs.Length - fs.Position > 0)
{
c = new byte[bufferSize];
fs.Read(c , 0,c.Length);
richTextBox1.AppendText(new string(UnicodeEncoding.ASCII.GetChars(c)));
}
}
that didn't work because a backgroundWorker can't affect UI elements and I need to use BeginInvoke to do it.
so I changed the code:
delegate void AddTextInvoker();
public void AddText()
{
using (FileStream fs = new FileStream(#"myFilePath.txt", FileMode.Open, FileAccess.Read))
{
int bufferSize = 50;
byte[] c = null;
while (fs.Length - fs.Position > 0)
{
c = new byte[bufferSize];
fs.Read(c , 0,c.Length);
richTextBox1.AppendText(new string(UnicodeEncoding.ASCII.GetChars(c)));
}
}
}
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
this.BeginInvoke(new AddTextInvoker(AddText));
}
there are two problems with this code.
1- it's taking longer and longer time to append the text (I think because of string immutability replacing the text over time will take longer)
2- on every addition the richTextBox will scroll down to the end which causing application hang.
the question is what can I do to stop the scrolling and application hang?
and what can I do to enhance string concatenation here?
Edit: after some testing and using Matt's answer I got this:
public void AddText()
{
using (FileStream fs = new FileStream(#"myFilePath.txt", FileMode.Open, FileAccess.Read))
{
int bufferSize = 50;
byte[] c = null;
while (fs.Length - fs.Position > 0)
{
c = new byte[bufferSize];
fs.Read(c , 0,c.Length);
string newText = new string(UnicodeEncoding.ASCII.GetChars(c));
this.BeginInvoke((Action)(() => richTextBox1.AppendText(newText)));
Thread.Sleep(5000); // here
}
}
}
when the loading pauses I can read and write without problems or hanging, once the text exceeded the the richTextBox size the loading will scroll down and will prevent me from continue.
One problem I see is that your background worker is, well, not doing any work in the background. It's all running on the UI thread. This may be why the UI thread is non-responsive.
I would refine your DoWork handler like so:
public void AddText()
{
using (FileStream fs = new FileStream(#"myFilePath.txt",
FileMode.Open, FileAccess.Read))
{
int bufferSize = 50;
byte[] c = null;
while (fs.Length - fs.Position > 0)
{
c = new byte[bufferSize];
fs.Read(c , 0,c.Length);
string newText = new string(UnicodeEncoding.ASCII.GetChars(c));
this.BeginInvoke((Action)(() => richTextBox1.AppendText(newText));
}
}
}
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
AddText();
}
What I've done is localized the use of BeginInvoke to the single UI call made in the handler. That way, all of the other work is done in the background thread. Maybe that will help with the UI thread becoming non-responsive.
Just call Application.DoEvents. That's the simplest thing, no need to worry about manually creating or synchronizing threads or background workers, yet your app stays responsive.
Also, try using File.ReadLines, which is a lazy-loaded enumerable, rather than manually using a FileStream. This, for example, works for me, and gives you everything you need in a loop and two lines of code.
private void button1_Click(object sender, EventArgs e)
{
foreach (var line in File.ReadLines(#"C:\Users\Dax\AppData\Local\Temp\dd_VSMsiLog0D85.txt", Encoding.ASCII))
{
richTextBox1.AppendText(line + "\r\n");
Application.DoEvents();
}
}
Alternately you can specify your chunk size and load it by that. This will run a bit faster, but take a bit longer (less than a second though) to read the full file at first.
private void button1_Click(object sender, EventArgs e)
{
var text = File.ReadAllText(#"C:\Users\Dax\AppData\Local\Temp\dd_VSMsiLog0D85.txt", Encoding.ASCII);
const int chunkSize = 1000000;
for (var i = 0; i < text.Length / chunkSize; ++i)
{
richTextBox1.AppendText(text.Substring(chunkSize * i, chunkSize));
Application.DoEvents();
}
}
Try this third option and see if your hang is caused by the file or by the loop:
void button1_Click(object sender, EventArgs e)
{
while(true)
{
richTextBox1.AppendText("a");
Application.DoEvents();
}
}
Ok that's not exactly the best solution but it do what I want, instead of using AppendText which will surly scroll down I used +=, and I still got the hang so needed the Sleep(100)
public void AddText()
{
using (FileStream fs = new FileStream(#"myFilePath.txt", FileMode.Open, FileAccess.Read))
{
int bufferSize = 50;
byte[] c = null;
while (fs.Length - fs.Position > 0)
{
c = new byte[bufferSize];
fs.Read(c , 0,c.Length);
string newText = new string(UnicodeEncoding.ASCII.GetChars(c));
this.BeginInvoke((Action)(() => richTextBox1.Text += newText));
Thread.Sleep(100);
}
}
}
this will actually not allow me to scroll down until loading is done,
I couldn't come up with a better idea.
Edit: And a work around to be able to read the text before loading is done is to set scrollBasrs on richTextBox to None and set the form autoscroll to true and in TextChanged event:
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
using (Graphics g = CreateGraphics())
{
SizeF size = g.MeasureString(richTextBox1.Text, richTextBox1.Font);
richTextBox1.Width = (int)Math.Ceiling(size.Width) >
richTextBox1.Width ? (int)Math.Ceiling(size.Width) : richTextBox1.Width;
richTextBox1.Height = (int)Math.Ceiling(size.Height) >
richTextBox1.Height ? (int)Math.Ceiling(size.Height) : richTextBox1.Height;
}
}
this way I will be able to scroll down while loading. I hope someone find a better solution.
Try this:
private void button1_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
using (FileStream fs = new FileStream(
#"myFilePath.txt", FileMode.Open, FileAccess.Read))
{
int bufferSize = 50;
byte[] c = null;
while (fs.Length - fs.Position > 0)
{
c = new byte[bufferSize];
fs.Read(c, 0, c.Length);
Invoke(new Action(() =>
richTextBox1.AppendText(
new string(UnicodeEncoding.ASCII.GetChars(c)))));
}
}
}
The problem was the BeginInvoke, that call the AppendText async, see
http://msdn.microsoft.com/en-us/library/0b1bf3y3.aspxhttp://msdn.microsoft.com/en-us/library/0b1bf3y3.aspx
and
http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx
Keep in mind that the Invoke method can throw exception, eg: if you close the form while loading text, so put it in try catch block and handle it.
BackgroundWorker CAN affect elements on the UI thread, since its events 'ProgressChanged' and 'RunWorkerCompleted' are executed on the calling thread. The following example increments an integer variable up to 10 on a separate thread, and communicates each increment back to the UI thread.
BackgroundWorker _worker;
private void button1_Click(object sender, EventArgs e)
{
_worker.RunWorkerAsync();
}
private void Form1_Load(object sender, EventArgs e)
{
_worker = new BackgroundWorker();
_worker.WorkerReportsProgress = true;
_worker.DoWork += new DoWorkEventHandler(worker_DoWork);
_worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = (BackgroundWorker)sender;
for (int i = 0; i < 10; i++)
{
// pass value in parameter userState (2nd parameter), since it can hold objects
worker.ReportProgress(0, i); // calls ProgressChanged on main thread
}
}
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// get value, passed in DoWork, back from UserState
richTextBox1.AppendText(e.UserState.ToString());
}

Multi threading in WPF using C# (with background worker)

I have written code to save an image which is generated by the application. The size of the image is around 32-35 MB. While saving the image to a BMB file, it is taking a long time, around 3-5 secs. For this purpose, I have used a background worker but when running the background worker, it shows an error like..."can't access the object as it is created on different thread".
Following is the code:
private void btnSaveDesign_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.SaveFileDialog sfd = new Microsoft.Win32.SaveFileDialog();
sfd.Title = "Save design as...";
sfd.Filter = "BMP|*.bmp";
if (sfd.ShowDialog() == true)
{
ww = new winWait();
ww.Show();
System.ComponentModel.BackgroundWorker bw = new System.ComponentModel.BackgroundWorker();
bw.DoWork += new System.ComponentModel.DoWorkEventHandler(bw_DoWork);
bw.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
fName = sfd.FileName;
cache = new CachedBitmap((BitmapSource)imgOut.Source, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
bw.RunWorkerAsync();
}
}
void bw_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
ww.Close();
}
void bw_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
BmpBitmapEncoder encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(cache)); //here... it says cant access...
using (FileStream file = File.OpenWrite(fName))
{
encoder.Save(file);
}
}
I have declared "cache" as a global object. (A similar trick worked when I was programming in Windows Forms with VB.NET.)
ww is the wait window that I want to be displayed while the precess is being executed.
How to do this? Is there any other simple method for multi threading in WPF?
When WPF objects are created they are assigned to a Dispatcher object. This disallows any threads other than the creating thread to access the object. This can be circumvented by freezing the object by calling the freeze method. You would need to call Freeze on your bitmapsource object. Once you have frozen your object it becomes uneditable
Your problem comes about because you are accessing an object which is not created by the background worker thread. Normally this would happen if you access a UI control which is created in the main thread and accessed from different thread.
Use the code below.
Dispatcher.Invoke
(
new Action(
delegate()
{
BmpBitmapEncoder encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(cache));
using (FileStream file = File.OpenWrite(fName))
{
encoder.Save(file);
}
}
)
);
I think you have to pass cache as a parameter to the new thread:
bw.RunWorkerAsync(cache);
and get it from the DoWork method:
var cache=(CacheType) e.Argument;
.NET framework provides a simple way to get started in threading with
the BackgroundWorker component. This wraps much of the complexity and
makes spawning a background thread relatively safe. In addition, it
allows you to communicate between your background thread and your UI
thread without doing any special coding. You can use this component
with WinForms and WPF applications. The BackgroundWorker offers
several features which include spawning a background thread, the
ability to cancel the background process before it has completed, and
the chance to report the progress back to your UI.
public BackgroudWorker()
{
InitializeComponent();
backgroundWorker = ((BackgroundWorker)this.FindResource("backgroundWorker"));
}
private int DoSlowProcess(int iterations, BackgroundWorker worker, DoWorkEventArgs e)
{
int result = 0;
for (int i = 0; i <= iterations; i++)
{
if (worker != null)
{
if (worker.CancellationPending)
{
e.Cancel = true;
return result;
}
if (worker.WorkerReportsProgress)
{
int percentComplete =
(int)((float)i / (float)iterations * 100);
worker.ReportProgress(percentComplete);
}
}
Thread.Sleep(100);
result = i;
}
return result;
}
private void startButton_Click(object sender, RoutedEventArgs e)
{
int iterations = 0;
if (int.TryParse(inputBox.Text, out iterations))
{
backgroundWorker.RunWorkerAsync(iterations);
startButton.IsEnabled = false;
cancelButton.IsEnabled = true;
outputBox.Text = "";
}
}
private void cancelButton_Click(object sender, RoutedEventArgs e)
{
// TODO: Implement Cancel process
this.backgroundWorker.CancelAsync();
}
private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
// e.Result = DoSlowProcess((int)e.Argument);
var bgw = sender as BackgroundWorker;
e.Result = DoSlowProcess((int)e.Argument, bgw, e);
}
private void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
workerProgress.Value = e.ProgressPercentage;
}
private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
MessageBox.Show(e.Error.Message);
}
else if (e.Cancelled)
{
outputBox.Text = "Canceled";
workerProgress.Value = 0;
}
else
{
outputBox.Text = e.Result.ToString();
workerProgress.Value = 0;
}
startButton.IsEnabled = true;
cancelButton.IsEnabled = false;
}

Categories

Resources