الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

مساعدة عاجلة رجاء: مشكلة في Thread

مغلق
بدأه jrahma في 25 أغسطس 2006 · 1 رد · 403 مشاهدة · في Microsoft Visual C#.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

استخدمت هذا الكود محاولا استخدام Thread ولكني أحصل على الخطأ التالي:

Cross-thread operation not valid: Control 'lstHosts' accessed from a thread other than the thread it was created on.

وهذا هو الكود:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Net;
using System.Net.NetworkInformation;
using System.Threading;

namespace HostHealth
{
	public partial class frmMain : Form
	{
		private Thread ping_thread = null;

		int listview_item;

		public frmMain()
		{
			InitializeComponent();
		}

		private void ping_to_host()
		{
			for (listview_item = 0; listview_item <= lstHosts.Items.Count - 1; listview_item++)
			{
				Ping ping_sender = new Ping();
				PingOptions ping_options = new PingOptions();
				ping_options.DontFragment = true;

				PingReply ping_reply = ping_sender.Send(lstHosts.Items[listview_item].SubItems[1].Text);

				if (ping_reply.Status == IPStatus.Success)
				{
					lstHosts.Items[listview_item].SubItems[0].Text = "Success";
					lstHosts.Items[listview_item].SubItems[2].Text = ping_reply.RoundtripTime.ToString();
					lstHosts.Items[listview_item].SubItems[3].Text = ping_reply.Options.Ttl.ToString();
					lstHosts.Items[listview_item].BackColor = Color.Black;
				}
				else
				{
					lstHosts.Items[listview_item].SubItems[0].Text = ping_reply.Status.ToString();
					lstHosts.Items[listview_item].BackColor = Color.Red;
				}

				toolStart.Text = "Idle - " + DateTime.Now;
				ping_thread.Abort();
			}
		}

		private void frmMain_Shown(object sender, EventArgs e)
		{
			lstHosts.Columns.Add("Status");
			lstHosts.Columns.Add("Where");
			lstHosts.Columns.Add("time");
			lstHosts.Columns.Add("ttl");

			lstHosts.Columns[0].Text = "Status";
			lstHosts.Columns[1].Text = "Where";
			lstHosts.Columns[2].Text = "time";
			lstHosts.Columns[3].Text = "ttl";

			lstHosts.Columns[0].Width = 200;
			lstHosts.Columns[1].Width = 200;
			lstHosts.Columns[2].Width = 80;
			lstHosts.Columns[3].Width = 80;

			lstHosts.Columns[0].TextAlign = HorizontalAlignment.Left;
			lstHosts.Columns[1].TextAlign = HorizontalAlignment.Left;
			lstHosts.Columns[2].TextAlign = HorizontalAlignment.Center;
			lstHosts.Columns[3].TextAlign = HorizontalAlignment.Center;

			DataSet dsHosts = new DataSet("hosts");
			dsHosts.ReadXml("C:\\Users\\Jassim\\Documents\\Visual Studio 2005\\Projects\\HostHealth\\hosts.xml");

			foreach (DataRow drHosts in dsHosts.Tables[0].Rows)
			{
				lstHosts.Items.Add("Pending..");
				lstHosts.Items[lstHosts.Items.Count - 1].SubItems.Add(drHosts["IPAddress"].ToString());
				lstHosts.Items[lstHosts.Items.Count - 1].SubItems.Add("");
				lstHosts.Items[lstHosts.Items.Count - 1].SubItems.Add("");
			}
		}

		private void toolStart_Click(object sender, EventArgs e)
		{
			if (toolStart.Text == "START")
			{
				toolStatus.Text = "Idle";
				toolStart.Text = "STOP!!";
				toolStart.ForeColor = Color.Red;

				toolStart.ToolTipText = "  Stop Monitoring  ";

				timerPing.Enabled = true;
			}
			else
			{
				timerPing.Enabled = false;

				toolStatus.Text = "Off";
				toolStart.Text = "START";
				toolStart.ForeColor = Color.Black;

				toolStart.ToolTipText = "  Start Monitoring  ";
			}
		}

		private void timerPing_Tick(object sender, EventArgs e)
		{
			this.ping_thread = new Thread(new ThreadStart(this.ping_to_host));
			this.ping_thread.Start();

			ping_to_host();
		}
	}
}
#2

هذا موضوع كامل عن الThreading

/index.php?showtopic=86234

و هذه المشاركة هي حل مشكلتك

/index.ph...6234&st=15#

Sr. Software Development Engineer
Hulu, LLC
My Blogs

هذا الموضوع مغلق.

مواضيع مشابهة