User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 455,970 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,773 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C# advertiser: Programming Forums
Views: 697 | Replies: 5
Reply
Join Date: Apr 2004
Location: in a galaxy far far away :)
Posts: 121
Reputation: johnroach1985 is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
johnroach1985's Avatar
johnroach1985 johnroach1985 is offline Offline
Junior Poster

Help Thread's colliding?

  #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

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
using System.Security.Principal;
using System.Reflection;

namespace LanChat
{
	public class LanChat : System.Windows.Forms.Form
	{
		Thread thread;
		bool IsThreadRunning = false;
		ReaderWriterLock rwl = new ReaderWriterLock();
		ArrayList userList = new ArrayList();
		
		public System.Windows.Forms.RichTextBox richTextBoxRecieveMsg;
        public System.Windows.Forms.Label label1;
        public System.Windows.Forms.ComboBox comboBoxUserNames;
        public System.Windows.Forms.RichTextBox richTextBoxSendMsg;
        public System.Windows.Forms.Button buttonStop;
        public System.Windows.Forms.Button buttonAdvertise;
        public System.Windows.Forms.Button buttonSend;
        public System.Windows.Forms.ContextMenu contextMenuNotify;
        public System.Windows.Forms.MenuItem menuItem3;
        public System.Windows.Forms.MenuItem menuItemExit;
        public System.Windows.Forms.MenuItem menuItemOpen;
        public System.Windows.Forms.MenuItem menuItemAbout;
        public System.Windows.Forms.NotifyIcon notifyIcon;
        public PictureBox pictureBox1;
        public System.ComponentModel.IContainer components;
	
		public LanChat()
		{
			InitializeComponent();
			thread = new Thread(new ThreadStart(MulticastListener));
			thread.Start();
			String message = "Hello$"  + GetUserName();
			MulticastSend("224.168.100.2", 1000, message);
			notifyIcon.Icon = LoadIcon();
			this.Icon = LoadIcon();
		}
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}
		#region Windows Form Designer generated code
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            this.richTextBoxRecieveMsg = new System.Windows.Forms.RichTextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.comboBoxUserNames = new System.Windows.Forms.ComboBox();
            this.buttonSend = new System.Windows.Forms.Button();
            this.richTextBoxSendMsg = new System.Windows.Forms.RichTextBox();
            this.buttonStop = new System.Windows.Forms.Button();
            this.buttonAdvertise = new System.Windows.Forms.Button();
            this.contextMenuNotify = new System.Windows.Forms.ContextMenu();
            this.menuItemOpen = new System.Windows.Forms.MenuItem();
            this.menuItemAbout = new System.Windows.Forms.MenuItem();
            this.menuItem3 = new System.Windows.Forms.MenuItem();
            this.menuItemExit = new System.Windows.Forms.MenuItem();
            this.notifyIcon = new System.Windows.Forms.NotifyIcon(this.components);
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // richTextBoxRecieveMsg
            // 
            this.richTextBoxRecieveMsg.BackColor = System.Drawing.Color.WhiteSmoke;
            this.richTextBoxRecieveMsg.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.richTextBoxRecieveMsg.Font = new System.Drawing.Font("Courier New", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.richTextBoxRecieveMsg.Location = new System.Drawing.Point(7, 176);
            this.richTextBoxRecieveMsg.Name = "richTextBoxRecieveMsg";
            this.richTextBoxRecieveMsg.ReadOnly = true;
            this.richTextBoxRecieveMsg.ShowSelectionMargin = true;
            this.richTextBoxRecieveMsg.Size = new System.Drawing.Size(535, 218);
            this.richTextBoxRecieveMsg.TabIndex = 0;
            this.richTextBoxRecieveMsg.Text = "";
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.BackColor = System.Drawing.Color.Black;
            this.label1.Font = new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.Location = new System.Drawing.Point(219, 27);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(80, 16);
            this.label1.TabIndex = 1;
            this.label1.Text = "User Name";
            // 
            // comboBoxUserNames
            // 
            this.comboBoxUserNames.BackColor = System.Drawing.Color.WhiteSmoke;
            this.comboBoxUserNames.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboBoxUserNames.Location = new System.Drawing.Point(305, 25);
            this.comboBoxUserNames.Name = "comboBoxUserNames";
            this.comboBoxUserNames.Size = new System.Drawing.Size(152, 21);
            this.comboBoxUserNames.TabIndex = 2;
            // 
            // buttonSend
            // 
            this.buttonSend.BackColor = System.Drawing.Color.Black;
            this.buttonSend.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.buttonSend.Font = new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.buttonSend.ForeColor = System.Drawing.Color.White;
            this.buttonSend.Location = new System.Drawing.Point(548, 409);
            this.buttonSend.Name = "buttonSend";
            this.buttonSend.Size = new System.Drawing.Size(72, 48);
            this.buttonSend.TabIndex = 3;
            this.buttonSend.Text = "Send";
            this.buttonSend.UseVisualStyleBackColor = false;
            this.buttonSend.Click += new System.EventHandler(this.buttonSend_Click);
            // 
            // richTextBoxSendMsg
            // 
            this.richTextBoxSendMsg.BackColor = System.Drawing.Color.WhiteSmoke;
            this.richTextBoxSendMsg.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.richTextBoxSendMsg.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.richTextBoxSendMsg.Location = new System.Drawing.Point(7, 409);
            this.richTextBoxSendMsg.MaxLength = 500;
            this.richTextBoxSendMsg.Name = "richTextBoxSendMsg";
            this.richTextBoxSendMsg.ShowSelectionMargin = true;
            this.richTextBoxSendMsg.Size = new System.Drawing.Size(535, 48);
            this.richTextBoxSendMsg.TabIndex = 4;
            this.richTextBoxSendMsg.Text = "";
            // 
            // buttonStop
            // 
            this.buttonStop.BackColor = System.Drawing.Color.Black;
            this.buttonStop.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.buttonStop.Font = new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.buttonStop.Location = new System.Drawing.Point(355, 73);
            this.buttonStop.Name = "buttonStop";
            this.buttonStop.Size = new System.Drawing.Size(100, 24);
            this.buttonStop.TabIndex = 5;
            this.buttonStop.Text = "Stop";
            this.buttonStop.UseVisualStyleBackColor = false;
            this.buttonStop.Click += new System.EventHandler(this.buttonStop_Click);
            // 
            // buttonAdvertise
            // 
            this.buttonAdvertise.BackColor = System.Drawing.Color.Black;
            this.buttonAdvertise.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.buttonAdvertise.Font = new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.buttonAdvertise.Location = new System.Drawing.Point(249, 73);
            this.buttonAdvertise.Name = "buttonAdvertise";
            this.buttonAdvertise.Size = new System.Drawing.Size(100, 24);
            this.buttonAdvertise.TabIndex = 6;
            this.buttonAdvertise.Text = "Advertise";
            this.buttonAdvertise.UseVisualStyleBackColor = false;
            this.buttonAdvertise.Click += new System.EventHandler(this.buttonAdvertise_Click);
            // 
            // contextMenuNotify
            // 
            this.contextMenuNotify.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuItemOpen,
            this.menuItemAbout,
            this.menuItem3,
            this.menuItemExit});
            // 
            // menuItemOpen
            // 
            this.menuItemOpen.Index = 0;
            this.menuItemOpen.Text = "Open";
            this.menuItemOpen.Click += new System.EventHandler(this.menuItemOpen_Click);
            // 
            // menuItemAbout
            // 
            this.menuItemAbout.Index = 1;
            this.menuItemAbout.Text = "About LanChat";
            this.menuItemAbout.Click += new System.EventHandler(this.menuItemAbout_Click);
            // 
            // menuItem3
            // 
            this.menuItem3.Index = 2;
            this.menuItem3.Text = "-";
            // 
            // menuItemExit
            // 
            this.menuItemExit.Index = 3;
            this.menuItemExit.Text = "Exit";
            this.menuItemExit.Click += new System.EventHandler(this.menuItemExit_Click);
            // 
            // notifyIcon
            // 
            this.notifyIcon.ContextMenu = this.contextMenuNotify;
            this.notifyIcon.Text = "notifyIcon";
            this.notifyIcon.Visible = true;
            this.notifyIcon.DoubleClick += new System.EventHandler(this.notifyIcon_DoubleClick);
            // 
            // pictureBox1
            // 
            this.pictureBox1.Image = global::LanChat.Properties.Resources.forweb;
            this.pictureBox1.Location = new System.Drawing.Point(12, 12);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(201, 151);
            this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
            this.pictureBox1.TabIndex = 7;
            this.pictureBox1.TabStop = false;
            // 
            // LanChat
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.BackColor = System.Drawing.Color.Black;
            this.ClientSize = new System.Drawing.Size(632, 469);
            this.Controls.Add(this.pictureBox1);
            this.Controls.Add(this.buttonAdvertise);
            this.Controls.Add(this.buttonStop);
            this.Controls.Add(this.richTextBoxRecieveMsg);
            this.Controls.Add(this.richTextBoxSendMsg);
            this.Controls.Add(this.buttonSend);
            this.Controls.Add(this.comboBoxUserNames);
            this.Controls.Add(this.label1);
            this.ForeColor = System.Drawing.Color.White;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.Name = "LanChat";
            this.ShowInTaskbar = false;
            this.Text = "Chat";
            this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion
		[STAThread]
		static void Main() 
		{
			LanChat f = new LanChat();
			Application.Run(f);
		}
        public void buttonSend_Click(object sender, EventArgs e)
		{
			if(richTextBoxSendMsg.Text == "")
				return;
			String message = "MSG:$" + GetUserName() +" : " + richTextBoxSendMsg.Text;
			string []host = comboBoxUserNames.Text.Split('\\');
			if(host.Length >1)
			{
				string hostName = Dns.GetHostByName(host[0]).AddressList[0].ToString();
				MulticastSend(hostName, 1000, message);
				richTextBoxSendMsg.Text = "";
			}
		}
        public void buttonAdvertise_Click(object sender, System.EventArgs e)
		{
			String message = "Hello$"  + GetUserName();
			MulticastSend("224.168.100.2", 1000, message);
		}
        public void buttonStop_Click(object sender, System.EventArgs e)
		{
			String message = "Stop$"  + GetUserName();
			MulticastSend("224.168.100.2", 1000, message);
		}
        public void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			rwl.AcquireWriterLock(10);
			IsThreadRunning = false;
			rwl.ReleaseWriterLock();
			thread.Abort();
		}
        public void MulticastListener()
		{
			recv("224.168.100.2", "1000");
            this.Controls.Add(this.comboBoxUserNames);

		}
        public void MulticastSend(string mAddress, int port, string message)
		{
			try 
			{
				IPAddress GroupAddress = IPAddress.Parse(mAddress);
				int GroupPort = port;
				UdpClient sender = new UdpClient();
				IPEndPoint groupEP = new IPEndPoint(GroupAddress,GroupPort);
				byte[] bytes = Encoding.ASCII.GetBytes(message);
				sender.Send(bytes, bytes.Length, groupEP);
				sender.Close();
    		} 
			catch (Exception e) 
			{
				MessageBox.Show(e.ToString());
			}
		}
		void recv(string mcastGroup, string port) 
		{
			Socket s=new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
			IPEndPoint ipep=new IPEndPoint(IPAddress.Any,int.Parse(port));
			s.Bind(ipep);
			IPAddress ip=IPAddress.Parse(mcastGroup);
			s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip,IPAddress.Any));
			IsThreadRunning = true;
			while(IsThreadRunning) 
			{
				byte[] b=new byte[500];
				s.Receive(b);
				string str = System.Text.Encoding.ASCII.GetString(b,0,b.Length);
				str = str.Replace("\0","");
				if(str.StartsWith("Hello"))
				{
					string []array = str.Split('$');
					if(array.Length > 1)
					{
						if(!userList.Contains(array[1].ToString()))
						{
							userList.Add(array[1]);
							UpdateComboBox();
                           
						}
					}
				}
				else if(str.StartsWith("Stop"))
				{
					string []array = str.Split('$');
					if(array.Length > 1)
					{
						if(userList.Contains(array[1].ToString()))
						{
							userList.Remove(array[1]);
                          
							UpdateComboBox();
						}
					}
				}
				else if(str.StartsWith("MSG:"))
				{
					str = str.Replace("MSG:$", "");
					int start = this.richTextBoxRecieveMsg.TextLength;
					int length = str.IndexOf(':');
					richTextBoxRecieveMsg.AppendText(str + "\n");
					if(start >= 0 && start < richTextBoxRecieveMsg.TextLength && length > 0 && length + start < richTextBoxRecieveMsg.TextLength)
					{
						this.richTextBoxRecieveMsg.Select(start, length);
						this.richTextBoxRecieveMsg.SelectionColor = Color.Blue;
						this.richTextBoxRecieveMsg.SelectionLength = 0;
					}
				}
			}
		}
		public void UpdateComboBox()
		{
			comboBoxUserNames.Items.Clear();
			comboBoxUserNames.Items.AddRange(userList.ToArray());
		}
		string GetUserName()
		{
			string str;
			str = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
			return str;
		}
		Icon LoadIcon()
		{
			Assembly assembly = typeof(LanChat).Assembly;
			string Name = "LanChat.LanChat.ico";
			return new Icon(assembly.GetManifestResourceStream(Name));
		}
        public void notifyIcon_DoubleClick(object sender, System.EventArgs e)
		{
			if (this.WindowState == FormWindowState.Minimized)
				this.WindowState = FormWindowState.Normal;
			this.Activate();
		}
        public void menuItemOpen_Click(object sender, System.EventArgs e)
		{
			this.Activate();
		}
        public void menuItemAbout_Click(object sender, System.EventArgs e)
		{
			About about = new About();
			about.Location =  Control.MousePosition;
			about.Show();
		}
        public void menuItemExit_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}
	}
}

Need any more information on this question please ask...
------

"A rational anachist believes that concepts such as 'state' and 'society' and 'government' have no existence save as physically exemplified in acts of self-responsible individuals.He believese that it is impossible to shift blame,share blame,distrubute blame.... as blame,guilt,responsibility are matters taking place inside human beings singly and nowhere else.But being rational,he knows that not all individuals hold his evaluations,so he tries to live perfectly in an imperfectworld...aware that his effort wil be less than perfect yet undismayed by self-knowledge of self-failure."

-Robert A. Heinlein

-----
John Roach
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2005
Location: Dundee, Scotland
Posts: 12,855
Reputation: jbennet is just really nice jbennet is just really nice jbennet is just really nice jbennet is just really nice 
Rep Power: 32
Solved Threads: 304
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator

Re: Thread's colliding?

  #2  
Nov 25th, 2007
you need to use the threadworker object
TRY MY SUGGESTIONS AT YOUR OWN RISK

Master of puppets Im pulling your strings, blinded by me, you cant see a thing. Master! Master!
Reply With Quote  
Join Date: Dec 2005
Posts: 46
Reputation: _r0ckbaer is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 7
_r0ckbaer's Avatar
_r0ckbaer _r0ckbaer is offline Offline
Light Poster

Re: Thread's colliding?

  #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