PDA

View Full Version : سوال: به غیر از ترد روش دیگه ای برای جلوگیری هنگ کردن برنامه



Sir-Programmer
یک شنبه 19 آذر 1391, 18:03 عصر
سلام
دوستان من میخوام وقتی رو کلید ارسال کلیک میکنم برنامه هنگ نکنه روش دیگه ای جز ترد نیست آخه از ترد استفاده کردم ولی درست نشد یه ارور دیگه ایجاد شد مثلاً میخوام وقتی از کد زیر استفاده می کنم برنامه هنگ نکنه
[

try
{
Ping ping = new Ping();
PingReply pingStatus = ping.Send("google.com");
if (pingStatus.Status == IPStatus.Success)
{
MailMessage msg = new MailMessage(new MailAddress(TxtUser.Text), new MailAddress(textBox1.Text)); // Create a MailMessage object with a from and to address
msg.Subject = textBox2.Text; // Add your subject
msg.SubjectEncoding = System.Text.Encoding.UTF8;
msg.Body = richTextBox1.Text; // Add the body of your message
msg.BodyEncoding = System.Text.Encoding.UTF8;
msg.IsBodyHtml = false; // Does the body contain html

SmtpClient client = new SmtpClient(SmtpServer.Text, 587); // Create an instance of SmtpClient with your smtp host and port
client.Credentials = new NetworkCredential(TxtUser.Text, TxtPw.Text); // Assign your username and password to connect to gmail
client.EnableSsl = false; // Enable SSL

if (TxtUser.Text.EndsWith("@yahoo.com"))
{
client.EnableSsl = false;
}
try
{

client.Send(msg); // Try to send your message;
MessageBox.Show("Message Sent.");
}
catch (SmtpException ex)
{
MessageBox.Show(ex.Message);
}
}
}
catch
{
MessageBox.Show("You are not connected to the internet.", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

مهرداد صفا
یک شنبه 19 آذر 1391, 18:24 عصر
سلام
درون کد متود Application.DoEvents را اضافه کنید و دوباره امتحان کنید

Sir-Programmer
یک شنبه 19 آذر 1391, 18:26 عصر
این روش هم تست کردم نمیشه :(

morteza271
یک شنبه 19 آذر 1391, 18:54 عصر
میشه بگین از تردها چطوری استفاده کردین که بازم درست نشده؟؟؟!!!
اگه امکانش هست کدتون رو با ترد اینجا بذارین تا بهتون بگم چطوری عمل کنید تا هنگ نکنه!

Sir-Programmer
یک شنبه 19 آذر 1391, 21:46 عصر
سلام
بیا دوست عزیز اینم کد ترد

private void sendmail()
{
try
{
Ping ping = new Ping();
PingReply pingStatus = ping.Send("google.com");
if (pingStatus.Status == IPStatus.Success)
{
MailMessage msg = new MailMessage(new MailAddress(TxtUser.Text), new MailAddress(textBox1.Text)); // Create a MailMessage object with a from and to address
msg.Subject = textBox2.Text; // Add your subject
msg.SubjectEncoding = System.Text.Encoding.UTF8;
msg.Body = richTextBox1.Text; // Add the body of your message
msg.BodyEncoding = System.Text.Encoding.UTF8;
msg.IsBodyHtml = false; // Does the body contain html

SmtpClient client = new SmtpClient(SmtpServer.Text, 587); // Create an instance of SmtpClient with your smtp host and port
client.Credentials = new NetworkCredential(TxtUser.Text, TxtPw.Text); // Assign your username and password to connect to gmail
client.EnableSsl = false; // Enable SSL

if (TxtUser.Text.EndsWith("@yahoo.com"))
{
client.EnableSsl = false;
}
try
{

client.Send(msg); // Try to send your message;
MessageBox.Show("Message Sent.");
}
catch (SmtpException ex)
{
MessageBox.Show(ex.Message);
}
}
}
catch
{
MessageBox.Show("You are not connected to the internet.", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

کد دکمه


private void button2_Click(object sender, EventArgs e)
{
Thread thsend = new Thread(new ThreadStart(sendmail));
thsend.Start();
}

میدونی چیه اجرا میشه ولی بجای اینکه ایملو ارسال کنه این مسج رو نمایش میده


MessageBox.Show("You are not connected to the internet.", "", MessageBoxButtons.OK, MessageBoxIcon.Error);

اگه میشه کمک کن مرسی

Sir-Programmer
یک شنبه 19 آذر 1391, 22:43 عصر
کسی نیست ؟؟؟؟؟

morteza271
یک شنبه 19 آذر 1391, 23:04 عصر
سلام
بیا دوست عزیز اینم کد ترد

private void sendmail()
{
try
{
Ping ping = new Ping();
PingReply pingStatus = ping.Send("google.com");
if (pingStatus.Status == IPStatus.Success)
{
MailMessage msg = new MailMessage(new MailAddress(TxtUser.Text), new MailAddress(textBox1.Text)); // Create a MailMessage object with a from and to address
msg.Subject = textBox2.Text; // Add your subject
msg.SubjectEncoding = System.Text.Encoding.UTF8;
msg.Body = richTextBox1.Text; // Add the body of your message
msg.BodyEncoding = System.Text.Encoding.UTF8;
msg.IsBodyHtml = false; // Does the body contain html

SmtpClient client = new SmtpClient(SmtpServer.Text, 587); // Create an instance of SmtpClient with your smtp host and port
client.Credentials = new NetworkCredential(TxtUser.Text, TxtPw.Text); // Assign your username and password to connect to gmail
client.EnableSsl = false; // Enable SSL

if (TxtUser.Text.EndsWith("@yahoo.com"))
{
client.EnableSsl = false;
}
try
{

client.Send(msg); // Try to send your message;
MessageBox.Show("Message Sent.");
}
catch (SmtpException ex)
{
MessageBox.Show(ex.Message);
}
}
}
catch
{
MessageBox.Show("You are not connected to the internet.", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

کد دکمه


private void button2_Click(object sender, EventArgs e)
{
Thread thsend = new Thread(new ThreadStart(sendmail));
thsend.Start();
}

میدونی چیه اجرا میشه ولی بجای اینکه ایملو ارسال کنه این مسج رو نمایش میده


MessageBox.Show("You are not connected to the internet.", "", MessageBoxButtons.OK, MessageBoxIcon.Error);

اگه میشه کمک کن مرسی

خوب دیگه پس ترد مشکلی نداره و با ترد اجرا میشه.
حالا اینکه اون خطا رو میده باید ببینید در کدوم خط از کدتون خط ره میده که میره به قسمت catch و اون پیام رو نشون میده.
برنامتون رو خط به خط اجرا کنید و ببینید در اجرای کدوم خط میپره به catch...

samadblaj
دوشنبه 20 آذر 1391, 01:12 صبح
سلام عزیزان اگه بخوام تئی یک thread از همچین کدی استفاه کنم باید به چه شکل باشه ؟

این کد خطا داره و تابع void مقدار بر نمیگردونه...

private void button1_Click(object sender, EventArgs e)
{
Thread t = new Thread(new ThreadStart(sendtext));
t.Start();
}
private void sendtext()
{
textBox1.Text = "Hi";
}

mehrbani
دوشنبه 20 آذر 1391, 02:21 صبح
به جای کار کردن مستقیم با Thread ها از Background Worker ها استفاده کنین.

mehrbani
دوشنبه 20 آذر 1391, 02:54 صبح
سلام عزیزان اگه بخوام تئی یک thread از همچین کدی استفاه کنم باید به چه شکل باشه ؟

این کد خطا داره و تابع void مقدار بر نمیگردونه...

private void button1_Click(object sender, EventArgs e)
{
Thread t = new Thread(new ThreadStart(sendtext));
t.Start();
}
private void sendtext()
{
textBox1.Text = "Hi";
}
شما در یک Thread دیگه نمی تونین به UI دسترسی داشته باشین.از Background Worker ها استفاده کنین

samadblaj
دوشنبه 20 آذر 1391, 12:08 عصر
سلام مبتدی هستم لطف کنید با یه مثال بیشتر توضیح دهید...

Sir-Programmer
چهارشنبه 22 آذر 1391, 13:03 عصر
برای متوقف کردن ترد باید چیکار کنم ؟ لطفاً جواب بدید