Thread's colliding?

Please support our C# advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
Reply

Join Date: Apr 2004
Posts: 127
Reputation: johnroach1985 is an unknown quantity at this point 
Solved Threads: 0
johnroach1985's Avatar
johnroach1985 johnroach1985 is offline Offline
Junior Poster

Thread's colliding?

 
0
  #1
Nov 25th, 2007
Hi. I am trying to figure out this code I found on the net that allows users to chat with each other. It gives a thread error that I just can't fix. Please help me.

This is the Form1.cs file

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.Net;
  8. using System.Net.Sockets;
  9. using System.Threading;
  10. using System.Text;
  11. using System.Security.Principal;
  12. using System.Reflection;
  13.  
  14. namespace LanChat
  15. {
  16. public class LanChat : System.Windows.Forms.Form
  17. {
  18. Thread thread;
  19. bool IsThreadRunning = false;
  20. ReaderWriterLock rwl = new ReaderWriterLock();
  21. ArrayList userList = new ArrayList();
  22.  
  23. public System.Windows.Forms.RichTextBox richTextBoxRecieveMsg;
  24. public System.Windows.Forms.Label label1;
  25. public System.Windows.Forms.ComboBox comboBoxUserNames;
  26. public System.Windows.Forms.RichTextBox richTextBoxSendMsg;
  27. public System.Windows.Forms.Button buttonStop;
  28. public System.Windows.Forms.Button buttonAdvertise;
  29. public System.Windows.Forms.Button buttonSend;
  30. public System.Windows.Forms.ContextMenu contextMenuNotify;
  31. public System.Windows.Forms.MenuItem menuItem3;
  32. public System.Windows.Forms.MenuItem menuItemExit;
  33. public System.Windows.Forms.MenuItem menuItemOpen;
  34. public System.Windows.Forms.MenuItem menuItemAbout;
  35. public System.Windows.Forms.NotifyIcon notifyIcon;
  36. public PictureBox pictureBox1;
  37. public System.ComponentModel.IContainer components;
  38.  
  39. public LanChat()
  40. {
  41. InitializeComponent();
  42. thread = new Thread(new ThreadStart(MulticastListener));
  43. thread.Start();
  44. String message = "Hello$" + GetUserName();
  45. MulticastSend("224.168.100.2", 1000, message);
  46. notifyIcon.Icon = LoadIcon();
  47. this.Icon = LoadIcon();
  48. }
  49. protected override void Dispose( bool disposing )
  50. {
  51. if( disposing )
  52. {
  53. if (components != null)
  54. {
  55. components.Dispose();
  56. }
  57. }
  58. base.Dispose( disposing );
  59. }
  60. #region Windows Form Designer generated code
  61. private void InitializeComponent()
  62. {
  63. this.components = new System.ComponentModel.Container();
  64. this.richTextBoxRecieveMsg = new System.Windows.Forms.RichTextBox();
  65. this.label1 = new System.Windows.Forms.Label();
  66. this.comboBoxUserNames = new System.Windows.Forms.ComboBox();
  67. this.buttonSend = new System.Windows.Forms.Button();
  68. this.richTextBoxSendMsg = new System.Windows.Forms.RichTextBox();
  69. this.buttonStop = new System.Windows.Forms.Button();
  70. this.buttonAdvertise = new System.Windows.Forms.Button();
  71. this.contextMenuNotify = new System.Windows.Forms.ContextMenu();
  72. this.menuItemOpen = new System.Windows.Forms.MenuItem();
  73. this.menuItemAbout = new System.Windows.Forms.MenuItem();
  74. this.menuItem3 = new System.Windows.Forms.MenuItem();
  75. this.menuItemExit = new System.Windows.Forms.MenuItem();
  76. this.notifyIcon = new System.Windows.Forms.NotifyIcon(this.components);
  77. this.pictureBox1 = new System.Windows.Forms.PictureBox();
  78. ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
  79. this.SuspendLayout();
  80. //
  81. // richTextBoxRecieveMsg
  82. //
  83. this.richTextBoxRecieveMsg.BackColor = System.Drawing.Color.WhiteSmoke;
  84. this.richTextBoxRecieveMsg.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  85. this.richTextBoxRecieveMsg.Font = new System.Drawing.Font("Courier New", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  86. this.richTextBoxRecieveMsg.Location = new System.Drawing.Point(7, 176);
  87. this.richTextBoxRecieveMsg.Name = "richTextBoxRecieveMsg";
  88. this.richTextBoxRecieveMsg.ReadOnly = true;
  89. this.richTextBoxRecieveMsg.ShowSelectionMargin = true;
  90. this.richTextBoxRecieveMsg.Size = new System.Drawing.Size(535, 218);
  91. this.richTextBoxRecieveMsg.TabIndex = 0;
  92. this.richTextBoxRecieveMsg.Text = "";
  93. //
  94. // label1
  95. //
  96. this.label1.AutoSize = true;
  97. this.label1.BackColor = System.Drawing.Color.Black;
  98. this.label1.Font = new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  99. this.label1.Location = new System.Drawing.Point(219, 27);
  100. this.label1.Name = "label1";
  101. this.label1.Size = new System.Drawing.Size(80, 16);
  102. this.label1.TabIndex = 1;
  103. this.label1.Text = "User Name";
  104. //
  105. // comboBoxUserNames
  106. //
  107. this.comboBoxUserNames.BackColor = System.Drawing.Color.WhiteSmoke;
  108. this.comboBoxUserNames.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  109. this.comboBoxUserNames.Location = new System.Drawing.Point(305, 25);
  110. this.comboBoxUserNames.Name = "comboBoxUserNames";
  111. this.comboBoxUserNames.Size = new System.Drawing.Size(152, 21);
  112. this.comboBoxUserNames.TabIndex = 2;
  113. //
  114. // buttonSend
  115. //
  116. this.buttonSend.BackColor = System.Drawing.Color.Black;
  117. this.buttonSend.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  118. this.buttonSend.Font = new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  119. this.buttonSend.ForeColor = System.Drawing.Color.White;
  120. this.buttonSend.Location = new System.Drawing.Point(548, 409);
  121. this.buttonSend.Name = "buttonSend";
  122. this.buttonSend.Size = new System.Drawing.Size(72, 48);
  123. this.buttonSend.TabIndex = 3;
  124. this.buttonSend.Text = "Send";
  125. this.buttonSend.UseVisualStyleBackColor = false;
  126. this.buttonSend.Click += new System.EventHandler(this.buttonSend_Click);
  127. //
  128. // richTextBoxSendMsg
  129. //
  130. this.richTextBoxSendMsg.BackColor = System.Drawing.Color.WhiteSmoke;
  131. this.richTextBoxSendMsg.BorderStyle = System.Windows.Forms.BorderStyle.None;
  132. this.richTextBoxSendMsg.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  133. this.richTextBoxSendMsg.Location = new System.Drawing.Point(7, 409);
  134. this.richTextBoxSendMsg.MaxLength = 500;
  135. this.richTextBoxSendMsg.Name = "richTextBoxSendMsg";
  136. this.richTextBoxSendMsg.ShowSelectionMargin = true;
  137. this.richTextBoxSendMsg.Size = new System.Drawing.Size(535, 48);
  138. this.richTextBoxSendMsg.TabIndex = 4;
  139. this.richTextBoxSendMsg.Text = "";
  140. //
  141. // buttonStop
  142. //
  143. this.buttonStop.BackColor = System.Drawing.Color.Black;
  144. this.buttonStop.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  145. this.buttonStop.Font = new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  146. this.buttonStop.Location = new System.Drawing.Point(355, 73);
  147. this.buttonStop.Name = "buttonStop";
  148. this.buttonStop.Size = new System.Drawing.Size(100, 24);
  149. this.buttonStop.TabIndex = 5;
  150. this.buttonStop.Text = "Stop";
  151. this.buttonStop.UseVisualStyleBackColor = false;
  152. this.buttonStop.Click += new System.EventHandler(this.buttonStop_Click);
  153. //
  154. // buttonAdvertise
  155. //
  156. this.buttonAdvertise.BackColor = System.Drawing.Color.Black;
  157. this.buttonAdvertise.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  158. this.buttonAdvertise.Font = new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  159. this.buttonAdvertise.Location = new System.Drawing.Point(249, 73);
  160. this.buttonAdvertise.Name = "buttonAdvertise";
  161. this.buttonAdvertise.Size = new System.Drawing.Size(100, 24);
  162. this.buttonAdvertise.TabIndex = 6;
  163. this.buttonAdvertise.Text = "Advertise";
  164. this.buttonAdvertise.UseVisualStyleBackColor = false;
  165. this.buttonAdvertise.Click += new System.EventHandler(this.buttonAdvertise_Click);
  166. //
  167. // contextMenuNotify
  168. //
  169. this.contextMenuNotify.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  170. this.menuItemOpen,
  171. this.menuItemAbout,
  172. this.menuItem3,
  173. this.menuItemExit});
  174. //
  175. // menuItemOpen
  176. //
  177. this.menuItemOpen.Index = 0;
  178. this.menuItemOpen.Text = "Open";
  179. this.menuItemOpen.Click += new System.EventHandler(this.menuItemOpen_Click);
  180. //
  181. // menuItemAbout
  182. //
  183. this.menuItemAbout.Index = 1;
  184. this.menuItemAbout.Text = "About LanChat";
  185. this.menuItemAbout.Click += new System.EventHandler(this.menuItemAbout_Click);
  186. //
  187. // menuItem3
  188. //
  189. this.menuItem3.Index = 2;
  190. this.menuItem3.Text = "-";
  191. //
  192. // menuItemExit
  193. //
  194. this.menuItemExit.Index = 3;
  195. this.menuItemExit.Text = "Exit";
  196. this.menuItemExit.Click += new System.EventHandler(this.menuItemExit_Click);
  197. //
  198. // notifyIcon
  199. //
  200. this.notifyIcon.ContextMenu = this.contextMenuNotify;
  201. this.notifyIcon.Text = "notifyIcon";
  202. this.notifyIcon.Visible = true;
  203. this.notifyIcon.DoubleClick += new System.EventHandler(this.notifyIcon_DoubleClick);
  204. //
  205. // pictureBox1
  206. //
  207. this.pictureBox1.Image = global::LanChat.Properties.Resources.forweb;
  208. this.pictureBox1.Location = new System.Drawing.Point(12, 12);
  209. this.pictureBox1.Name = "pictureBox1";
  210. this.pictureBox1.Size = new System.Drawing.Size(201, 151);
  211. this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
  212. this.pictureBox1.TabIndex = 7;
  213. this.pictureBox1.TabStop = false;
  214. //
  215. // LanChat
  216. //
  217. this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  218. this.BackColor = System.Drawing.Color.Black;
  219. this.ClientSize = new System.Drawing.Size(632, 469);
  220. this.Controls.Add(this.pictureBox1);
  221. this.Controls.Add(this.buttonAdvertise);
  222. this.Controls.Add(this.buttonStop);
  223. this.Controls.Add(this.richTextBoxRecieveMsg);
  224. this.Controls.Add(this.richTextBoxSendMsg);
  225. this.Controls.Add(this.buttonSend);
  226. this.Controls.Add(this.comboBoxUserNames);
  227. this.Controls.Add(this.label1);
  228. this.ForeColor = System.Drawing.Color.White;
  229. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
  230. this.MaximizeBox = false;
  231. this.Name = "LanChat";
  232. this.ShowInTaskbar = false;
  233. this.Text = "Chat";
  234. this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
  235. ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
  236. this.ResumeLayout(false);
  237. this.PerformLayout();
  238.  
  239. }
  240. #endregion
  241. [STAThread]
  242. static void Main()
  243. {
  244. LanChat f = new LanChat();
  245. Application.Run(f);
  246. }
  247. public void buttonSend_Click(object sender, EventArgs e)
  248. {
  249. if(richTextBoxSendMsg.Text == "")
  250. return;
  251. String message = "MSG:$" + GetUserName() +" : " + richTextBoxSendMsg.Text;
  252. string []host = comboBoxUserNames.Text.Split('\\');
  253. if(host.Length >1)
  254. {
  255. string hostName = Dns.GetHostByName(host[0]).AddressList[0].ToString();
  256. MulticastSend(hostName, 1000, message);
  257. richTextBoxSendMsg.Text = "";
  258. }
  259. }
  260. public void buttonAdvertise_Click(object sender, System.EventArgs e)
  261. {
  262. String message = "Hello$" + GetUserName();
  263. MulticastSend("224.168.100.2", 1000, message);
  264. }
  265. public void buttonStop_Click(object sender, System.EventArgs e)
  266. {
  267. String message = "Stop$" + GetUserName();
  268. MulticastSend("224.168.100.2", 1000, message);
  269. }
  270. public void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  271. {
  272. rwl.AcquireWriterLock(10);
  273. IsThreadRunning = false;
  274. rwl.ReleaseWriterLock();
  275. thread.Abort();
  276. }
  277. public void MulticastListener()
  278. {
  279. recv("224.168.100.2", "1000");
  280. this.Controls.Add(this.comboBoxUserNames);
  281.  
  282. }
  283. public void MulticastSend(string mAddress, int port, string message)
  284. {
  285. try
  286. {
  287. IPAddress GroupAddress = IPAddress.Parse(mAddress);
  288. int GroupPort = port;
  289. UdpClient sender = new UdpClient();
  290. IPEndPoint groupEP = new IPEndPoint(GroupAddress,GroupPort);
  291. byte[] bytes = Encoding.ASCII.GetBytes(message);
  292. sender.Send(bytes, bytes.Length, groupEP);
  293. sender.Close();
  294. }
  295. catch (Exception e)
  296. {
  297. MessageBox.Show(e.ToString());
  298. }
  299. }
  300. void recv(string mcastGroup, string port)
  301. {
  302. Socket s=new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  303. IPEndPoint ipep=new IPEndPoint(IPAddress.Any,int.Parse(port));
  304. s.Bind(ipep);
  305. IPAddress ip=IPAddress.Parse(mcastGroup);
  306. s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip,IPAddress.Any));
  307. IsThreadRunning = true;
  308. while(IsThreadRunning)
  309. {
  310. byte[] b=new byte[500];
  311. s.Receive(b);
  312. string str = System.Text.Encoding.ASCII.GetString(b,0,b.Length);
  313. str = str.Replace("\0","");
  314. if(str.StartsWith("Hello"))
  315. {
  316. string []array = str.Split('$');
  317. if(array.Length > 1)
  318. {
  319. if(!userList.Contains(array[1].ToString()))
  320. {
  321. userList.Add(array[1]);
  322. UpdateComboBox();
  323.  
  324. }
  325. }
  326. }
  327. else if(str.StartsWith("Stop"))
  328. {
  329. string []array = str.Split('$');
  330. if(array.Length > 1)
  331. {
  332. if(userList.Contains(array[1].ToString()))
  333. {
  334. userList.Remove(array[1]);
  335.  
  336. UpdateComboBox();
  337. }
  338. }
  339. }
  340. else if(str.StartsWith("MSG:"))
  341. {
  342. str = str.Replace("MSG:$", "");
  343. int start = this.richTextBoxRecieveMsg.TextLength;
  344. int length = str.IndexOf(':');
  345. richTextBoxRecieveMsg.AppendText(str + "\n");
  346. if(start >= 0 && start < richTextBoxRecieveMsg.TextLength && length > 0 && length + start < richTextBoxRecieveMsg.TextLength)
  347. {
  348. this.richTextBoxRecieveMsg.Select(start, length);
  349. this.richTextBoxRecieveMsg.SelectionColor = Color.Blue;
  350. this.richTextBoxRecieveMsg.SelectionLength = 0;
  351. }
  352. }
  353. }
  354. }
  355. public void UpdateComboBox()
  356. {
  357. comboBoxUserNames.Items.Clear();
  358. comboBoxUserNames.Items.AddRange(userList.ToArray());
  359. }
  360. string GetUserName()
  361. {
  362. string str;
  363. str = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
  364. return str;
  365. }
  366. Icon LoadIcon()
  367. {
  368. Assembly assembly = typeof(LanChat).Assembly;
  369. string Name = "LanChat.LanChat.ico";
  370. return new Icon(assembly.GetManifestResourceStream(Name));
  371. }
  372. public void notifyIcon_DoubleClick(object sender, System.EventArgs e)
  373. {
  374. if (this.WindowState == FormWindowState.Minimized)
  375. this.WindowState = FormWindowState.Normal;
  376. this.Activate();
  377. }
  378. public void menuItemOpen_Click(object sender, System.EventArgs e)
  379. {
  380. this.Activate();
  381. }
  382. public void menuItemAbout_Click(object sender, System.EventArgs e)
  383. {
  384. About about = new About();
  385. about.Location = Control.MousePosition;
  386. about.Show();
  387. }
  388. public void menuItemExit_Click(object sender, System.EventArgs e)
  389. {
  390. this.Close();
  391. }
  392. }
  393. }

Need any more information on this question please ask...
"By the data to date, there is only one animal in the Galaxy dangerous to man—man himself. So he must supply his own indispensable competition. He has no enemy to help him."
From Time Enough for Love by Robert A. Heinlein
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 15,990
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 510
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is online now Online
Moderator

Re: Thread's colliding?

 
0
  #2
Nov 25th, 2007
you need to use the threadworker object
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 45
Reputation: _r0ckbaer is an unknown quantity at this point 
Solved Threads: 7
_r0ckbaer's Avatar
_r0ckbaer _r0ckbaer is offline Offline
Light Poster

Re: Thread's colliding?

 
0
  #3
Nov 25th, 2007
Here is corrected source:

  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using System.Reflection;
  8. using System.Security.Principal;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Windows.Forms;
  12.  
  13. namespace LanChat
  14. {
  15. public class LanChat : Form
  16. {
  17. public Button buttonAdvertise;
  18. public Button buttonSend;
  19. public Button buttonStop;
  20. public ComboBox comboBoxUserNames;
  21. public IContainer components;
  22. public ContextMenu contextMenuNotify;
  23. private bool IsThreadRunning = false;
  24. public Label label1;
  25. public MenuItem menuItem3;
  26. public MenuItem menuItemAbout;
  27. public MenuItem menuItemExit;
  28. public MenuItem menuItemOpen;
  29. public NotifyIcon notifyIcon;
  30. public PictureBox pictureBox1;
  31. public RichTextBox richTextBoxRecieveMsg;
  32. public RichTextBox richTextBoxSendMsg;
  33. private ReaderWriterLock rwl = new ReaderWriterLock();
  34. private Thread thread;
  35. private ArrayList userList = new ArrayList();
  36.  
  37. public LanChat()
  38. {
  39. InitializeComponent();
  40. thread = new Thread(new ThreadStart(MulticastListener));
  41. thread.Start();
  42. String message = "Hello$" + GetUserName();
  43. MulticastSend("224.168.100.2", 1000, message);
  44. notifyIcon.Icon = LoadIcon();
  45. Icon = LoadIcon();
  46. }
  47.  
  48. protected override void Dispose(bool disposing)
  49. {
  50. if (disposing)
  51. {
  52. if (components != null)
  53. {
  54. components.Dispose();
  55. }
  56. }
  57. base.Dispose(disposing);
  58. }
  59.  
  60. [STAThread]
  61. private static void Main()
  62. {
  63. LanChat f = new LanChat();
  64. Application.Run(f);
  65. }
  66.  
  67. public void buttonSend_Click(object sender, EventArgs e)
  68. {
  69. if (richTextBoxSendMsg.Text == "")
  70. return;
  71. String message = "MSG:$" + GetUserName() + " : " + richTextBoxSendMsg.Text;
  72. string[] host = comboBoxUserNames.Text.Split('\\');
  73. if (host.Length > 1)
  74. {
  75. string hostName = Dns.GetHostByName(host[0]).AddressList[0].ToString();
  76. MulticastSend(hostName, 1000, message);
  77. richTextBoxSendMsg.Text = "";
  78. }
  79. }
  80.  
  81. public void buttonAdvertise_Click(object sender, EventArgs e)
  82. {
  83. String message = "Hello$" + GetUserName();
  84. MulticastSend("224.168.100.2", 1000, message);
  85. }
  86.  
  87. public void buttonStop_Click(object sender, EventArgs e)
  88. {
  89. String message = "Stop$" + GetUserName();
  90. MulticastSend("224.168.100.2", 1000, message);
  91. }
  92.  
  93. public void Form1_Closing(object sender, CancelEventArgs e)
  94. {
  95. rwl.AcquireWriterLock(10);
  96. IsThreadRunning = false;
  97. rwl.ReleaseWriterLock();
  98. thread.Abort();
  99. }
  100.  
  101. public void MulticastListener()
  102. {
  103. recv("224.168.100.2", "1000");
  104. Controls.Add(comboBoxUserNames);
  105. }
  106.  
  107. public void MulticastSend(string mAddress, int port, string message)
  108. {
  109. try
  110. {
  111. IPAddress GroupAddress = IPAddress.Parse(mAddress);
  112. int GroupPort = port;
  113. UdpClient sender = new UdpClient();
  114. IPEndPoint groupEP = new IPEndPoint(GroupAddress, GroupPort);
  115. byte[] bytes = Encoding.ASCII.GetBytes(message);
  116. sender.Send(bytes, bytes.Length, groupEP);
  117. sender.Close();
  118. }
  119. catch (Exception e)
  120. {
  121. MessageBox.Show(e.ToString());
  122. }
  123. }
  124.  
  125. private void recv(string mcastGroup, string port)
  126. {
  127. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  128. IPEndPoint ipep = new IPEndPoint(IPAddress.Any, int.Parse(port));
  129. s.Bind(ipep);
  130. IPAddress ip = IPAddress.Parse(mcastGroup);
  131. s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership,
  132. new MulticastOption(ip, IPAddress.Any));
  133. IsThreadRunning = true;
  134. while (IsThreadRunning)
  135. {
  136. byte[] b = new byte[500];
  137. s.Receive(b);
  138. string str = Encoding.ASCII.GetString(b, 0, b.Length);
  139. str = str.Replace("\0", "");
  140. if (str.StartsWith("Hello"))
  141. {
  142. string[] array = str.Split('$');
  143. if (array.Length > 1)
  144. {
  145. if (!userList.Contains(array[1].ToString()))
  146. {
  147. userList.Add(array[1]);
  148. UpdateComboBox();
  149. }
  150. }
  151. }
  152. else if (str.StartsWith("Stop"))
  153. {
  154. string[] array = str.Split('$');
  155. if (array.Length > 1)
  156. {
  157. if (userList.Contains(array[1].ToString()))
  158. {
  159. userList.Remove(array[1]);
  160.  
  161. if (comboBoxUserNames.InvokeRequired)
  162. {
  163. comboBoxUserNames.Invoke(new UpdateComboBoxHandler(UpdateComboBox));
  164. }
  165. else
  166. {
  167. UpdateComboBox();
  168. }
  169. }
  170. }
  171. }
  172. else if (str.StartsWith("MSG:"))
  173. {
  174. int start = -1;
  175. int length = -1;
  176. if (richTextBoxRecieveMsg.InvokeRequired)
  177. {
  178. if (richTextBoxRecieveMsg.InvokeRequired)
  179. richTextBoxSendMsg.Invoke(new SetStartHandler(UpdateControl), new object[] {start, str, length});
  180. }
  181. else
  182. {
  183. UpdateControl(ref start, ref str, ref length);
  184. }
  185. }
  186. }
  187. }
  188.  
  189. private delegate void UpdateComboBoxHandler();
  190. private delegate void SetStartHandler(ref int start, ref string str, ref int length);
  191. private void UpdateControl(ref int start, ref string str, ref int length)
  192. {
  193. str = str.Replace("MSG:$", "");
  194. start = richTextBoxSendMsg.TextLength;
  195. length = str.IndexOf(':');
  196. richTextBoxRecieveMsg.AppendText(str + "\n");
  197. if (start >= 0 && start < richTextBoxRecieveMsg.TextLength && length > 0 &&
  198. length + start < richTextBoxRecieveMsg.TextLength)
  199. {
  200. richTextBoxRecieveMsg.Select(start, length);
  201. richTextBoxRecieveMsg.SelectionColor = Color.Blue;
  202. richTextBoxRecieveMsg.SelectionLength = 0;
  203. }
  204. }
  205.  
  206. public void UpdateComboBox()
  207. {
  208. comboBoxUserNames.Items.Clear();
  209. comboBoxUserNames.Items.AddRange(userList.ToArray());
  210. }
  211.  
  212. private string GetUserName()
  213. {
  214. string str;
  215. str = WindowsIdentity.GetCurrent().Name.ToString();
  216. return str;
  217. }
  218.  
  219. private Icon LoadIcon()
  220. {
  221. Assembly assembly = typeof (LanChat).Assembly;
  222. string Name = "LanChat.LanChat.ico";
  223. return null;
  224. }
  225.  
  226. public void notifyIcon_DoubleClick(object sender, EventArgs e)
  227. {
  228. if (WindowState == FormWindowState.Minimized)
  229. WindowState = FormWindowState.Normal;
  230. Activate();
  231. }
  232.  
  233. public void menuItemOpen_Click(object sender, EventArgs e)
  234. {
  235. Activate();
  236. }
  237.  
  238. public void menuItemAbout_Click(object sender, EventArgs e)
  239. {
  240. // About about = new About();
  241. // about.Location = MousePosition;
  242. // about.Show();
  243. }
  244.  
  245. public void menuItemExit_Click(object sender, EventArgs e)
  246. {
  247. Close();
  248. }
  249.  
  250. #region Windows Form Designer generated code
  251.  
  252. private void InitializeComponent()
  253. {
  254. this.components = new System.ComponentModel.Container();
  255. this.richTextBoxRecieveMsg = new System.Windows.Forms.RichTextBox();
  256. this.label1 = new System.Windows.Forms.Label();
  257. this.comboBoxUserNames = new System.Windows.Forms.ComboBox();
  258. this.buttonSend = new System.Windows.Forms.Button();
  259. this.richTextBoxSendMsg = new System.Windows.Forms.RichTextBox();
  260. this.buttonStop = new System.Windows.Forms.Button();
  261. this.buttonAdvertise = new System.Windows.Forms.Button();
  262. this.contextMenuNotify = new System.Windows.Forms.ContextMenu();
  263. this.menuItemOpen = new System.Windows.Forms.MenuItem();
  264. this.menuItemAbout = new System.Windows.Forms.MenuItem();
  265. this.menuItem3 = new System.Windows.Forms.MenuItem();
  266. this.menuItemExit = new System.Windows.Forms.MenuItem();
  267. this.notifyIcon = new System.Windows.Forms.NotifyIcon(this.components);
  268. this.pictureBox1 = new System.Windows.Forms.PictureBox();
  269. ((System.ComponentModel.ISupportInitialize) (this.pictureBox1)).BeginInit();
  270. this.SuspendLayout();
  271. //
  272. // richTextBoxRecieveMsg
  273. //
  274. this.richTextBoxRecieveMsg.BackColor = System.Drawing.Color.WhiteSmoke;
  275. this.richTextBoxRecieveMsg.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  276. this.richTextBoxRecieveMsg.Font =
  277. new System.Drawing.Font("Courier New", 9F, System.Drawing.FontStyle.Regular,
  278. System.Drawing.GraphicsUnit.Point, ((byte) (0)));
  279. this.richTextBoxRecieveMsg.Location = new System.Drawing.Point(7, 176);
  280. this.richTextBoxRecieveMsg.Name = "richTextBoxRecieveMsg";
  281. this.richTextBoxRecieveMsg.ReadOnly = true;
  282. this.richTextBoxRecieveMsg.ShowSelectionMargin = true;
  283. this.richTextBoxRecieveMsg.Size = new System.Drawing.Size(535, 218);
  284. this.richTextBoxRecieveMsg.TabIndex = 0;
  285. this.richTextBoxRecieveMsg.Text = "";
  286. //
  287. // label1
  288. //
  289. this.label1.AutoSize = true;
  290. this.label1.BackColor = System.Drawing.Color.Black;
  291. this.label1.Font =
  292. new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Regular,
  293. System.Drawing.GraphicsUnit.Point, ((byte) (0)));
  294. this.label1.Location = new System.Drawing.Point(219, 27);
  295. this.label1.Name = "label1";
  296. this.label1.Size = new System.Drawing.Size(80, 16);
  297. this.label1.TabIndex = 1;
  298. this.label1.Text = "User Name";
  299. //
  300. // comboBoxUserNames
  301. //
  302. this.comboBoxUserNames.BackColor = System.Drawing.Color.WhiteSmoke;
  303. this.comboBoxUserNames.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  304. this.comboBoxUserNames.Location = new System.Drawing.Point(305, 25);
  305. this.comboBoxUserNames.Name = "comboBoxUserNames";
  306. this.comboBoxUserNames.Size = new System.Drawing.Size(152, 21);
  307. this.comboBoxUserNames.TabIndex = 2;
  308. //
  309. // buttonSend
  310. //
  311. this.buttonSend.BackColor = System.Drawing.Color.Black;
  312. this.buttonSend.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  313. this.buttonSend.Font =
  314. new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Bold,
  315. System.Drawing.GraphicsUnit.Point, ((byte) (0)));
  316. this.buttonSend.ForeColor = System.Drawing.Color.White;
  317. this.buttonSend.Location = new System.Drawing.Point(548, 409);
  318. this.buttonSend.Name = "buttonSend";
  319. this.buttonSend.Size = new System.Drawing.Size(72, 48);
  320. this.buttonSend.TabIndex = 3;
  321. this.buttonSend.Text = "Send";
  322. this.buttonSend.UseVisualStyleBackColor = false;
  323. this.buttonSend.Click += new System.EventHandler(this.buttonSend_Click);
  324. //
  325. // richTextBoxSendMsg
  326. //
  327. this.richTextBoxSendMsg.BackColor = System.Drawing.Color.WhiteSmoke;
  328. this.richTextBoxSendMsg.BorderStyle = System.Windows.Forms.BorderStyle.None;
  329. this.richTextBoxSendMsg.Font =
  330. new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular,
  331. System.Drawing.GraphicsUnit.Point, ((byte) (0)));
  332. this.richTextBoxSendMsg.Location = new System.Drawing.Point(7, 409);
  333. this.richTextBoxSendMsg.MaxLength = 500;
  334. this.richTextBoxSendMsg.Name = "richTextBoxSendMsg";
  335. this.richTextBoxSendMsg.ShowSelectionMargin = true;
  336. this.richTextBoxSendMsg.Size = new System.Drawing.Size(535, 48);
  337. this.richTextBoxSendMsg.TabIndex = 4;
  338. this.richTextBoxSendMsg.Text = "";
  339. //
  340. // buttonStop
  341. //
  342. this.buttonStop.BackColor = System.Drawing.Color.Black;
  343. this.buttonStop.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  344. this.buttonStop.Font =
  345. new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Regular,
  346. System.Drawing.GraphicsUnit.Point, ((byte) (0)));
  347. this.buttonStop.Location = new System.Drawing.Point(355, 73);
  348. this.buttonStop.Name = "buttonStop";
  349. this.buttonStop.Size = new System.Drawing.Size(100, 24);
  350. this.buttonStop.TabIndex = 5;
  351. this.buttonStop.Text = "Stop";
  352. this.buttonStop.UseVisualStyleBackColor = false;
  353. this.buttonStop.Click += new System.EventHandler(this.buttonStop_Click);
  354. //
  355. // buttonAdvertise
  356. //
  357. this.buttonAdvertise.BackColor = System.Drawing.Color.Black;
  358. this.buttonAdvertise.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  359. this.buttonAdvertise.Font =
  360. new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Regular,
  361. System.Drawing.GraphicsUnit.Point, ((byte) (0)));
  362. this.buttonAdvertise.Location = new System.Drawing.Point(249, 73);
  363. this.buttonAdvertise.Name = "buttonAdvertise";
  364. this.buttonAdvertise.Size = new System.Drawing.Size(100, 24);
  365. this.buttonAdvertise.TabIndex = 6;
  366. this.buttonAdvertise.Text = "Advertise";
  367. this.buttonAdvertise.UseVisualStyleBackColor = false;
  368. this.buttonAdvertise.Click += new System.EventHandler(this.buttonAdvertise_Click);
  369. //
  370. // contextMenuNotify
  371. //
  372. this.contextMenuNotify.MenuItems.AddRange(new System.Windows.Forms.MenuItem[]
  373. {
  374. this.menuItemOpen,
  375. this.menuItemAbout,
  376. this.menuItem3,
  377. this.menuItemExit
  378. });
  379. //
  380. // menuItemOpen
  381. //
  382. this.menuItemOpen.Index = 0;
  383. this.menuItemOpen.Text = "Open";
  384. this.menuItemOpen.Click += new System.EventHandler(this.menuItemOpen_Click);
  385. //
  386. // menuItemAbout
  387. //
  388. this.menuItemAbout.Index = 1;
  389. this.menuItemAbout.Text = "About LanChat";
  390. this.menuItemAbout.Click += new System.EventHandler(this.menuItemAbout_Click);
  391. //
  392. // menuItem3
  393. //
  394. this.menuItem3.Index = 2;
  395. this.menuItem3.Text = "-";
  396. //
  397. // menuItemExit
  398. //
  399. this.menuItemExit.Index = 3;
  400. this.menuItemExit.Text = "Exit";
  401. this.menuItemExit.Click += new System.EventHandler(this.menuItemExit_Click);
  402. //
  403. // notifyIcon
  404. //
  405. this.notifyIcon.ContextMenu = this.contextMenuNotify;
  406. this.notifyIcon.Text = "notifyIcon";
  407. this.notifyIcon.Visible = true;
  408. this.notifyIcon.DoubleClick += new System.EventHandler(this.notifyIcon_DoubleClick);
  409. //
  410. // pictureBox1
  411. //
  412. // this.pictureBox1.Image = global::LanChat.Properties.Resources.forweb;
  413. this.pictureBox1.Location = new System.Drawing.Point(12, 12);
  414. this.pictureBox1.Name = "pictureBox1";
  415. this.pictureBox1.Size = new System.Drawing.Size(201, 151);
  416. this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
  417. this.pictureBox1.TabIndex = 7;
  418. this.pictureBox1.TabStop = false;
  419. //
  420. // LanChat
  421. //
  422. this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  423. this.BackColor = System.Drawing.Color.Black;
  424. this.ClientSize = new System.Drawing.Size(632, 469);
  425. this.Controls.Add(this.pictureBox1);
  426. this.Controls.Add(this.buttonAdvertise);
  427. this.Controls.Add(this.buttonStop);
  428. this.Controls.Add(this.richTextBoxRecieveMsg);
  429. this.Controls.Add(this.richTextBoxSendMsg);
  430. this.Controls.Add(this.buttonSend);
  431. this.Controls.Add(this.comboBoxUserNames);
  432. this.Controls.Add(this.label1);
  433. this.ForeColor = System.Drawing.Color.White;
  434. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
  435. this.MaximizeBox = false;
  436. this.Name = "LanChat";
  437. this.ShowInTaskbar = false;
  438. this.Text = "Chat";
  439. this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
  440. ((System.ComponentModel.ISupportInitialize) (this.pictureBox1)).EndInit();
  441. this.ResumeLayout(false);
  442. this.PerformLayout();
  443. }
  444.  
  445. #endregion
  446. }
  447. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 127
Reputation: johnroach1985 is an unknown quantity at this point 
Solved Threads: 0
johnroach1985's Avatar
johnroach1985 johnroach1985 is offline Offline
Junior Poster

Re: Thread's colliding?

 
0
  #4
Nov 29th, 2007
Hi the program still gives a

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

Error after you click the advertise button and than the combobox...

The error is given at

  1. public void UpdateComboBox()
  2.  
  3. {
  4.  
  5. comboBoxUserNames.Items.Clear();
  6.  
  7. comboBoxUserNames.Items.AddRange(userList.ToArray());
  8.  
  9. }
"By the data to date, there is only one animal in the Galaxy dangerous to man—man himself. So he must supply his own indispensable competition. He has no enemy to help him."
From Time Enough for Love by Robert A. Heinlein
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 45
Reputation: _r0ckbaer is an unknown quantity at this point 
Solved Threads: 7
_r0ckbaer's Avatar
_r0ckbaer _r0ckbaer is offline Offline
Light Poster

Re: Thread's colliding?

 
0
  #5
Nov 29th, 2007
Try to correct that one by yourself, the answer is right there in the code, have a look at how i handle the other case (it's exactly the same), we're not here to chew you every piece in advance...
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 127
Reputation: johnroach1985 is an unknown quantity at this point 
Solved Threads: 0
johnroach1985's Avatar
johnroach1985 johnroach1985 is offline Offline
Junior Poster

Re: Thread's colliding?

 
0
  #6
Dec 3rd, 2007
Ok... Found out where the problem is...
Than you _r0ckbaer
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC