PDA

View Full Version : سوال: مشکل در استفاده از وقفه



Alireza751
جمعه 26 دی 1393, 21:31 عصر
سلام
من در برنامه C#‎‎ با استفاده از وقفه نوشتم موقع اجرا خطای زیر رو
ross-thread operation not valid: Control 'txtElevator' accessed from a thread other than the thread it was created on
برای خط کد های 26-49-72
نمیدونم چطوری باید مشکل رو رفع کنم کسی میتونه کمکم کنه؟

private void btnStart_Click(object sender, EventArgs e) {
//Passenger(0);
PassengerThread = new Thread[i];
for (int j = 0; j < i; j++)
{
PassengerThread[j] = new Thread(Passenger);
PassengerThread[j].Start(j);
}
for (int j = 0; j < i; j++)
{
PassengerThread[j].Join();
}
Thread.Sleep(500);
THEEND();
MessageBox.Show("ممنون از اینکه تا پایان کار با ما همراه بودید", "پایان");
}
//-----------------------------------------------------------------------------------
//تابع حرکت اسانسور رو به پایین
public void Down()
{
pictureBox2.WaitOnLoad = true;
pictureBox2.Image = Image.FromFile(txtDown.Text);

while (txtElevator.Location.Y != y)
{
txtElevator.Location = new Point(X, Y);
if (txtElevator.Location.Y == lbl1.Location.Y)
{
jahat = false;
Up();
return;
}
Y++;
Thread.Sleep(5);
}
if (txtElevator.Location.Y == lbl1.Location.Y)
{
jahat = false;
}
pictureBox2.Image = Image.FromFile(txtStop.Text);
}
//-----------------------------------------------------------------------------------
//تابع حرکت اسانسور رو به بالا
public void Up()
{
pictureBox2.Image = Image.FromFile(txtUp.Text);
while (txtElevator.Location.Y != y)
{
txtElevator.Location = new Point(X, Y);
if (txtElevator.Location.Y == lbl10.Location.Y)
{
jahat = true;
Down();
return;
}
Y--;
Thread.Sleep(5);
}
if (txtElevator.Location.Y == lbl10.Location.Y)
{
jahat = true;
}
pictureBox2.Image =Image.FromFile(txtStop.Text);
}
//-----------------------------------------------------------------------------------
//تابع برگشت به طبثه اول در انتها کار
public void THEEND()
{
pictureBox2.Image = Image.FromFile(txtDown.Text);
while (txtElevator.Location.Y != lbl1.Location.Y)
{
txtElevator.Location = new Point(X, Y);
Y++;
Thread.Sleep(5);
}
pictureBox2.Image = Image.FromFile(txtStop.Text);


}
//-----------------------------------------------------------------------------------
//تابع سوار شدن مسافر ها
public void vorod1()
{
if (txtElevator.Text == "")
{ txtElevator.Text = "* "; }
else if (txtElevator.Text == "* ")
{ txtElevator.Text = "* * "; }
else if (txtElevator.Text == "* * ")
{ txtElevator.Text = "* * * "; }
else if (txtElevator.Text == "* * * ")
{ txtElevator.Text = "* * * * "; }
else if (txtElevator.Text == "* * * * ")
{ txtElevator.Text = "* * * * *"; }
}
//-----------------------------------------------------------------------------------
//تابع خارج شدن مسافر ها
public void khoroj1()
{
if (txtElevator.Text == "* ")
{ txtElevator.Text = ""; }
else if (txtElevator.Text == "* * ")
{ txtElevator.Text = "* "; }
else if (txtElevator.Text == "* * * ")
{ txtElevator.Text = "* * "; }
else if (txtElevator.Text == "* * * ")
{ txtElevator.Text = "* * * "; }
else if (txtElevator.Text == "* * * * *")
{ txtElevator.Text = "* * * * "; }
}
//-----------------------------------------------------------------------------------
//تابع مسافر ها
private void Passenger(object Number)
{
//AsansorWait.WaitOne();
//vSemaphor.WaitOne();//یک سمافر که یک فرایند وارد ناحیه شود
int number = Convert.ToInt32(Number);
v += "lbl" + Convert.ToString(vorod[number]);
foreach (var item in Controls.OfType<Label>())
{
if (item.Name == v)
{
y = item.Location.Y;
}
}
if (jahat == true)
{
Down();
}
else
{
Up();
}
vorod1();
v = "";
Thread.Sleep(500);
//vSemaphor.Release();//پایان ناحیه بحرانی
//endSemaphor.WaitOne();//شروع ناحیه بحرانی جدید
end += "lbl" + Convert.ToString(khoroj[number]);
foreach (var item in Controls.OfType<Label>())
{
if (item.Name == end)
{
y = item.Location.Y;
}
}


if (jahat == true)
{
Down();
}
else
{
Up();
}
khoroj1();
end = "";
Thread.Sleep(500);
//endSemaphor.Release();//پایان ناحیه بحرانی
//AsansorWait.Release();
}

محمد رضا فاتحی
شنبه 27 دی 1393, 07:33 صبح
ساده ترین راه اینه ولی اینکه به مشکلی بر نخوره نمی دونم خودم از این کد استفاده می کنم مشکلی هم نداشتم تا حالا
تو لود فرمت اینو بنویس
CheckForIllegalCrossThreadCalls = false;

SabaSabouhi
شنبه 27 دی 1393, 08:23 صبح
سلام
دوستمون rezafatehy که فقط مشکل رو دور زدن، وقتی مشکلی رو دور می‌زنید، انتظار داشته باشین که یه جای دیگه مشکل بزرگتری براتون ایجاد کنه.
این مساله راه حل صحیح داره،
اخیراً تو یکی از تاپیک‌های این تالار مفصل راجع بهش صحبت شده و من توضیح دادم راه حل رو.
فکر کنم تو یه تاپیکی بود با موضوع چند رشته‌ای در Console Application.
قبل از پست زدن یه جستجو کنید.

صبا صبوحی