I am writing a simple picture blender in Windows Forms. Although it works rather fine I have a problem - while application is in progress - I can't move the main Winodow Form. The application allows two threads to work in background. When only one is busy then if I try to drag the window it responds with a delay (as if it needed time to get a focus back). When two Background Builders do their task I can't move main form at all. After they finish I can move window again. I thought that passing 'this' as a argument to another thread might be the issue, but I make copies of fields I need and just in case I've added 'this.Activate()' after calling a separate thread. This makes no difference anyway.
This is how I call workers:
private void PerformBlending()
{
if (!bw2.IsBusy)
{
bw2.RunWorkerAsync(this);
this.Activate();
}
else
{
bw3.RunWorkerAsync(this);
this.Activate();
}
}
private void bw2_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker bw = sender as BackgroundWorker;
Form1 mainForm = e.Argument as Form1;
Bitmap leftBmp = new Bitmap(mainForm.buttonPic1.BackgroundImage);
Bitmap rightBmp = new Bitmap(mainForm.buttonPic2.BackgroundImage);
Bitmap resultBmp;
int leftX = leftBmp.Width; int leftY = leftBmp.Height;
int rightX = rightBmp.Width; int rightY = rightBmp.Height;
int x = leftX < rightX ? leftX : rightX;
int y = leftY < rightY ? leftY : rightY;
resultBmp = new Bitmap(x, y);
double alfa = mainForm.trackBarValue;
for (int i = 0; i < x; ++i)
//Parallel.For(0, x, i =>
{
for (int j = 0; j < y; ++j)
{
bw.ReportProgress((int)(((double)i / (double)y + (double)1 / (double)x) * 100));
int leftR = (i < leftX && j < leftY) ? (int)(alfa * leftBmp.GetPixel(i, j).R) : 0;
int leftG = (i < leftX && j < leftY) ? (int)(alfa * leftBmp.GetPixel(i, j).G) : 0;
int leftB = (i < leftX && j < leftY) ? (int)(alfa * leftBmp.GetPixel(i, j).B) : 0;
int rightR = (i < rightX && j < rightY) ? (int)((1 - alfa) * rightBmp.GetPixel(i, j).R) : 0;
int rightG = (i < rightX && j < rightY) ? (int)((1 - alfa) * rightBmp.GetPixel(i, j).G) : 0;
int rightB = (i < rightX && j < rightY) ? (int)((1 - alfa) * rightBmp.GetPixel(i, j).B) : 0;
int r = leftR + rightR;
int g = leftG + rightG;
int b = leftB + rightB;
resultBmp.SetPixel(i, j, Color.FromArgb(r, g, b));
}
}//);
e.Result = resultBmp;
}