У меня есть 2 изображения в Picturebox. Когда я проверяю флажок, вентиляторы начинают вращаться. Я создаю две разные темы (но они выполняют одну и ту же работу). Если я проверяю только один из них, он работает без проблем, но если я проверяю их обоих одновременно, они замедляют друг друга. Я хочу, чтобы Темы не влияли друг на друга. Как я могу справиться с этой ситуацией?
Thread rotateImageThread = new Thread(new ParameterizedThreadStart(RotateImageThreadFunction));
Thread radialFanTurningThread = new Thread(new ParameterizedThreadStart(RadialFanTurningThreadFunction));
static ManualResetEvent pauseResumeThreadForFreezerFan = new ManualResetEvent(true);
static ManualResetEvent pauseResumeThreadForRadialFan = new ManualResetEvent(true);
static bool rotateImageRunFlag = false;
static bool radialFanRunFlag = false;
bool startThreadAtStartFlag = true;
bool startThreadAtStartForRadialFanFlag = true;
private void CB_freezer_fan_CheckedChanged(object sender, EventArgs e)
{
if (cb_freezer_fan.Checked == true)
{
StartThreadAtStart();
pb_FreezerFan.Visible = false;
pb_FreezerFanRunning.Visible = true;
setRotateImageRunFlag(true);
pauseResumeThreadForFreezerFan.Set();
}
else
{
pb_FreezerFan.Visible = true;
pb_FreezerFanRunning.Visible = false;
setRotateImageRunFlag(false);
pauseResumeThreadForFreezerFan.Reset();
}
}
private static void RotateImageThreadFunction(object objectToInside)
{
PictureBox pictureBox = (PictureBox)objectToInside;
int rotateAngle = 0;
while(true)
{
pauseResumeThreadForFreezerFan.WaitOne(Timeout.Infinite);
if (getRotateImageRunFlag() == true)
{
rotateAngle = pictureBox.Visible == false ? 0 : rotateAngle;
pictureBox.Image = RotateImage(global::SimulationInterface.Properties.Resources.FreezerFanRunning1, rotateAngle);
rotateAngle += 5;
Thread.Sleep(10);
rotateAngle = (rotateAngle == 360) ? 0 : rotateAngle;
}
}
}
public static Image RotateImage(Image img, float rotationAngle)
{
//create an empty Bitmap image
Bitmap bmp = new Bitmap(img.Width, img.Height);
//turn the Bitmap into a Graphics object
Graphics gfx = Graphics.FromImage(bmp);
//now we set the rotation point to the center of our image
gfx.TranslateTransform((float)bmp.Width / 2, (float)bmp.Height / 2);
//now rotate the image
gfx.RotateTransform(rotationAngle);
gfx.TranslateTransform(-(float)bmp.Width / 2, -(float)bmp.Height / 2);
//set the InterpolationMode to HighQualityBicubic so to ensure a high
//quality image once it is transformed to the specified size
//gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
//now draw our new image onto the graphics object
gfx.DrawImage(img, new Point(0, 0));
//dispose of our Graphics object
gfx.Dispose();
//return the image
return bmp;
}
public void StartThreadAtStart()
{
if (startThreadAtStartFlag == true)
{
rotateImageThread.Start(pb_FreezerFanRunning);
rotateImageThread.Priority = ThreadPriority.Lowest;
startThreadAtStartFlag = false;
}
}
private static void RadialFanTurningThreadFunction(object objectToInside)
{
PictureBox pictureBox = (PictureBox)objectToInside;
int rotateAngle = 0;
while (true)
{
pauseResumeThreadForRadialFan.WaitOne(Timeout.Infinite);
if (getRadialFanRunFlag() == true)
{
pictureBox.Image = RotateImageForRadialFan(global::SimulationInterface.Properties.Resources.ventilating_fan, -rotateAngle);
rotateAngle += 5;
Thread.Sleep(10);
rotateAngle = (rotateAngle == 360) ? 0 : rotateAngle;
}
System.Diagnostics.Trace.WriteLine(rotateAngle);
}
}
private void cb_RadialFan_CheckedChanged(object sender, EventArgs e)
{
ChangePictureboxImageWithCheckbox(cb_RadialFan, pb_HvacFan, pb_HvacFanRunning);
if (cb_RadialFan.Checked == true)
{
StartThreadAtStartForRadialFan();
setRadialFanRunFlag(true);
pauseResumeThreadForRadialFan.Set();
}
else
{
setRadialFanRunFlag(false);
pauseResumeThreadForRadialFan.Reset();
}
}
public static Image RotateImageForRadialFan(Image img, float rotationAngle)
{
//create an empty Bitmap image
Bitmap bmp = new Bitmap(img.Width, img.Height);
//turn the Bitmap into a Graphics object
Graphics gfx = Graphics.FromImage(bmp);
//now we set the rotation point to the center of our image
gfx.TranslateTransform((float)bmp.Width / 2, (float)bmp.Height / 2);
//now rotate the image
gfx.RotateTransform(rotationAngle);
gfx.TranslateTransform(-(float)bmp.Width / 2, -(float)bmp.Height / 2);
//set the InterpolationMode to HighQualityBicubic so to ensure a high
//quality image once it is transformed to the specified size
//gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
//now draw our new image onto the graphics object
gfx.DrawImage(img, new Point(0, 0));
//dispose of our Graphics object
gfx.Dispose();
//return the image
return bmp;
}
public void StartThreadAtStartForRadialFan()
{
if (startThreadAtStartForRadialFanFlag == true)
{
radialFanTurningThread.Start(pb_HvacFanInner);
radialFanTurningThread.Priority = ThreadPriority.Lowest;
startThreadAtStartForRadialFanFlag = false;
}
}
public static bool getRotateImageRunFlag()
{
return rotateImageRunFlag;
}
public static void setRotateImageRunFlag(bool state)
{
rotateImageRunFlag = state;
}
public static bool getRadialFanRunFlag()
{
return radialFanRunFlag;
}
public static void setRadialFanRunFlag(bool state)
{
radialFanRunFlag = state;
}