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...