Re: Threading problem



Ah, a response!!! I'm just learning about using multithreading in C#.
Actually fairly new to C#. I can assure you that this code is performing
with no apparent problems. The only problem that I know of is that
the thread runs twice before it stops. I'm not sure what you are referring
to as regards "touching a windows resource" but I attempted to use
methods I found in the MSDN documentation for updating the UI
controls.

If you are knowledgeable in this area I would much appreciate a closer
look at the code. I know it is a bit much but I tried to minimize as best
I could without missing anything relevant. I'm not even sure if my
approach to this is sensible.

Regards
Chris Saunders

"Alvin Bruney" <www.lulu.com/owc> wrote in message
news:%23tQ6zg2hGHA.4276@xxxxxxxxxxxxxxxxxxxxxxx
Chris,
What I don't get is why this call isn't failing. Backgroundworker appears
to be touching a windows resource (a quick eyeball of the code)

--

________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
-------------------------------------------------------

"Chris Saunders" <evas@xxxxxxxxxxxxxxxxx> wrote in message
news:eIsHH5nhGHA.3572@xxxxxxxxxxxxxxxxxxxxxxx
I'm using System.ComponentModel.BackgroundWorker to do some drawing in my
application.
The thread seems to run fine but it runs twice and I can't see why.
I'm going to show my code with everything that I think is not relevant
removed.
Perhaps the drawBitmap function can be ignored, I just included it
because it is the
actual work the tread is performing. (It's just a test case.)
Can anyone help?

namespace Mandolin
{
public partial class MainForm : Form
{

public MainForm()
{
backgroundWorkerDraw.DoWork += new
DoWorkEventHandler(backgroundWorkerDraw_DoWork);
backgroundWorkerDraw.ProgressChanged += new
ProgressChangedEventHandler(backgroundWorkerDraw_ProgressChanged);
backgroundWorkerDraw.RunWorkerCompleted += new
RunWorkerCompletedEventHandler(backgroundWorkerDraw_RunWorkerCompleted);
}

private void goToolStripMenuItem_Click(object sender, EventArgs e)
{
if (backgroundWorkerDraw.IsBusy == false)
{
backgroundWorkerDraw.RunWorkerAsync();
}
}

private void drawBitmap(BackgroundWorker worker, DoWorkEventArgs
e)
{
if (worker.CancellationPending)
e.Cancel = true;
else
{
for (int r = startRow; r < bitmapHeight; r++)
{
if (worker.CancellationPending)
e.Cancel = true;
else
{
for (int c = 0; c < bitmapWidth; c++)
{
mutex.WaitOne();
bitmap.SetPixel(c, r, Color.FromArgb(r * c));
mutex.ReleaseMutex();
if (e.Cancel)
c = bitmapWidth;
}
}
worker.ReportProgress((int)(((double)r /
(double)bitmapHeight) * 100));
if (e.Cancel)
{
mutex.WaitOne();
if (pauseButtonPushed == true)
startRow = r;
if (stopButtonPushed == true)
startRow = 0;
r = bitmapHeight;
mutex.ReleaseMutex();
}
}
}
}

private void backgroundWorkerDraw_DoWork(object sender,
DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
drawBitmap(worker, e);
}

private void backgroundWorkerDraw_ProgressChanged(object sender,
ProgressChangedEventArgs e)
{
statusBar.Text = e.ProgressPercentage.ToString() + " percent
complete.";
Graphics g = CreateGraphics();
mutex.WaitOne();
g.DrawImage(bitmap, rect);
mutex.ReleaseMutex();
}

private void backgroundWorkerDraw_RunWorkerCompleted(object
sender, RunWorkerCompletedEventArgs e)
{
toolStripButtonPlay.Enabled = true;
toolStripButtonPause.Enabled = false;
toolStripButtonStop.Enabled = false;
goToolStripMenuItem.Enabled = true;
pauseToolStripMenuItem.Enabled = false;
stopToolStripMenuItem.Enabled = false;
statusBar.Text = statusBarReady;
}
}
}

Regards
Chris Saunders







.


Loading