Geliştirmeye çalıştığım form 6 resim kutusundan oluşan bir diziye ve 6 kalıptan oluşan bir görüntü dizisine sahip. Tıklandığında, her görüntüyü bir an için gösteren zarları "döndüren" 6 iş parçacığı oluşturması gereken bir düğmem var. Sorun şu ki, zar tıklatıldıktan sonra buton tıklatılarak bir yöntemi çağırmam gerekiyor. Zarın dönmesini sağlayabilirim, ancak mesaj kutusu hemen görüntülenir. Birkaç farklı yol denedim ve çeşitli hatalar aldım. Aşağıdaki çalışmayan sürümde, program donuyor. Bir tonluk kaynakları kontrol ettim ama Delegeler gibi bazı kavramları kavramak ve tüm bunları iyi bir şekilde çağırmak. Herhangi bir yardım harika olurdu! Burada görüntüleri ayarlama sizin çağırma ve resim kutuları Güncellemenize arasında bir kilitlenme alıyoruzMesaj dizilerim ile ilgili sorunlar
namespace testDice
{
public partial class Form1 : Form
{
private Image[] imgAr;
private PictureBox[] picBoxAr;
private Random r;
private Thread[] tArray;
private ThreadStart tStart;
private delegate void setTheImages();
public Form1()
{
InitializeComponent();
setImageArray();
setPicBoxAr();
}
private void setImageArray()
{
imgAr = new Image[6];
imgAr[0] = testDice.Properties.Resources.die6;
imgAr[1] = testDice.Properties.Resources.die1;
imgAr[2] = testDice.Properties.Resources.die2;
imgAr[3] = testDice.Properties.Resources.die3;
imgAr[4] = testDice.Properties.Resources.die4;
imgAr[5] = testDice.Properties.Resources.die5;
}
private void setPicBoxAr()
{
picBoxAr = new PictureBox[6];
picBoxAr[0] = pictureBox1;
picBoxAr[1] = pictureBox2;
picBoxAr[2] = pictureBox3;
picBoxAr[3] = pictureBox4;
picBoxAr[4] = pictureBox5;
picBoxAr[5] = pictureBox6;
}
private void button1_Click(object sender, EventArgs e)
{
roll();
//wait for threads to finish and update images--doesn't work
for (int n = 0; n < 6; n++)
{
while (tArray[n].IsAlive)
{
for (int i = 0; i < 6; i++)
{
this.picBoxAr[i].Update();
}
}
}
MessageBox.Show("Each die has its own thread");
}
private void roll()
{
this.tStart = new ThreadStart(RunAllDiceThreads);
this.tArray = new Thread[6];
for (int i = 0; i < 6; i++)
{
this.tArray[i] = new Thread(tStart);
this.tArray[i].Start();
}
}
private void RunAllDiceThreads()
{
int n = 0;
while (n < 50)
{
setImg();
Thread.Sleep(50);
n++;
}
for (int i = 0; i < 6; i++)
{
if (tArray[i] != null)
{
tArray[i].Abort();
tArray[i] = null;
}
}
}// end RunAllDiceThreads
private void setImg()
{
r = new Random();
for (int i = 0; i < 6; i++)
{
if (this.picBoxAr[i].InvokeRequired)
{
setTheImages s = new setTheImages(setImg);
// parameter mismatch error here
//this.Invoke(s, new object[] { imgAr[r.Next(6)] });
//Freezes here!!
this.Invoke(s);
}
else
{
this.picBoxAr[i].Image = imgAr[r.Next(6)];
}
}
}//end setImg
}// end class Form1
}//end namespace testDice
1. Tüm program büyük bir iş parçacığı güvenlik ihlali gibi görünüyor. 2. Bunun için hiç iş parçacığına ihtiyacınız olmadığını düşünüyorum. Bunun yerine, bir zamanlayıcı kullanın ve işaretlendiğinde tüm zarları güncelleyin. – svick
Ödev, thread – Mox
kullanmaktır. TPL'yi kullanmayı düşünmelisiniz. – davenewza