System.ObjectDisposedException: Доступ к ликвидированному объекту невозможен - C# (234898)
Формулировка задачи:
Смастерил Irc Client, но при закрытии формы с чатом вылетает
System.ObjectDisposedException: Доступ к ликвидированному объекту невозможен
Прошу помочь, вод, собственно, сам код.
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Diagnostics;
- using System.IO;
- using System.Configuration;
- using System.Collections.Specialized;
- using TechLifeForum;
- using System.Threading;
- namespace WindowsFormsApplication44
- {
- public partial class Form4 : Form
- {
- IrcClient client;
- public string a;
- public Form4()
- {
- InitializeComponent();
- txtName.ReadOnly = true;
- this.MouseDown += new MouseEventHandler(Form4_MouseDown);
- }
- void Form4_MouseDown(object sender, MouseEventArgs e)
- {
- base.Capture = false;
- Message m = Message.Create(base.Handle, 0xa1, new IntPtr(2), IntPtr.Zero);
- this.WndProc(ref m);
- }
- private void Form4_Load(object sender, EventArgs e)
- {
- btnConnect_Click(sender, e);
- }
- private delegate void _UpdateUsers(string[] userlist);
- private void UpdateUsers(string[] userlist)
- {
- #region CrossThreadCheck
- if (InvokeRequired)
- {
- this.Invoke(new _UpdateUsers(UpdateUsers), (object)userlist);
- return;
- }
- #endregion
- lstNames.Items.Clear();
- foreach (string u in userlist)
- lstNames.Items.Add(u);
- }// UpdateUsers
- private void UserJoined(string user)
- {
- ShowMessage("*" + user + " присоединился к комнате чата");
- }// UserJoined
- private void UserLeft(string user)
- {
- ShowMessage("*" + user + " покинул комнату чата");
- }// UserLeft
- private void ChannelMessage(string user, string message)
- {
- ShowMessage("<" + user + ">: " + message);
- }// ChannelMessage
- private void NoticeMessage(string user, string message)
- {
- ShowMessage("<--Заметка--> " + user + ": " + message);
- }// NoticeMessage
- private void PrivateMessage(string user, string message)
- {
- ShowMessage("<--Личное сообщение--> от " + user + ">: " + message);
- }// PrivateMessage
- private delegate void _ShowMessage(string message);
- private void ShowMessage(string message)
- {
- #region CrossThreadCheck
- if (InvokeRequired)
- {
- this.Invoke(new _ShowMessage(ShowMessage), message);
- return;
- }
- #endregion
- rtbWindow.AppendText("\n" + message);
- rtbWindow.ScrollToCaret();
- }
- private void Connect()
- {
- // connect to the irc server
- client.Connect("#LittleHlp");
- }// Connect
- private void txtSend_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (e.KeyChar == 13) // enter key
- {
- e.Handled = true;
- btnSend_Click(sender, e);
- }
- }
- private void AddEvents()
- {
- // Save settings event
- // events triggered by the client
- client.UpdateUsers += new UpdateUserListEventDelegate(UpdateUsers);
- client.UserJoined += new UserJoinedEventDelegate(UserJoined);
- client.UserLeft += new UserLeftEventDelegate(UserLeft);
- client.ChannelMessage += new ChannelMesssageEventDelegate(ChannelMessage);
- client.NoticeMessage += new NoticeMessageEventDelegate(NoticeMessage);
- client.PrivateMessage += new PrivateMessageEventDelegate(PrivateMessage);
- client.Disconnected += new DisconnectedEventDelegate(Disconnect);
- client.ServerMessage += new ServerMessageEventDelegate(ShowMessage);
- }// AddEvents
- private void timer1_Tick(object sender, EventArgs e)
- {
- usrNum.Text = lstNames.Items.Count.ToString();
- }
- private void pictureBox1_Click(object sender, EventArgs e)
- {
- Disconnect();
- this.Close();
- }
- private void pictureBox2_Click(object sender, EventArgs e)
- {
- this.WindowState = FormWindowState.Minimized;
- }
- private void pictureBox1_MouseEnter(object sender, EventArgs e)
- {
- pictureBox1.Image = Properties.Resources.Close;
- }
- private void pictureBox1_MouseLeave(object sender, EventArgs e)
- {
- pictureBox1.Image = Properties.Resources.Clos;
- }
- private void pictureBox2_MouseEnter(object sender, EventArgs e)
- {
- pictureBox2.Image = Properties.Resources.m2;
- }
- private void pictureBox2_MouseLeave(object sender, EventArgs e)
- {
- pictureBox2.Image = Properties.Resources.m1;
- }
- private void btnSend_Click(object sender, EventArgs e)
- {
- if (chkRaw.Checked)
- {
- client.SendRAW("PRIVMSG " + txtName.Text + " " + txtSend.Text.Trim());
- ShowMessage("<--Личное сообщение--> к " + txtName.Text + ">: " + txtSend.Text);
- }
- else
- {
- client.SendMessage(txtSend.Text.Trim());
- ShowMessage("<" + client.Nick + ">: " + txtSend.Text);
- }
- txtSend.Clear();
- txtSend.Focus();
- }
- private void btnConnect_Click(object sender, EventArgs e)
- {
- Disconnect();
- Form2 main = this.Owner as Form2;
- a = main.label1.Text;
- if (a == "Chesko")
- {
- txtName.ReadOnly = false;
- txtSend.Text = "";
- txtName.BackColor = System.Drawing.SystemColors.Window;
- txtName.ForeColor = System.Drawing.SystemColors.WindowText;
- }
- if (client != null)
- {
- client.Disconnect();
- return;
- }
- client = new IrcClient("zoidberg.centralchat.net", 6667);
- client.Nick = a;
- AddEvents();
- Thread t = new Thread(Connect);
- t.IsBackground = true;
- t.Start();
- btnSend.Visible = true;
- }
- private void lstNames_SelectedIndexChanged(object sender, EventArgs e)
- {
- string name = lstNames.SelectedItem.ToString();
- if (name.IndexOf("@") >-1 | name.IndexOf("~") >-1)
- {
- txtName.Text = name.Remove(0, 1);
- }
- else
- {
- txtName.Text = name;
- }
- if (name == "@ChanServ")
- {
- txtName.Text = "";
- MessageBox.Show("Невозможно отправить личное сообщение данному пользователю");
- }
- }
- private void Disconnect()
- {
- if (client != null)
- {
- client.Disconnect();
- client = null;
- }
- }
- }
- }
Решение задачи: «System.ObjectDisposedException: Доступ к ликвидированному объекту невозможен»
textual
Листинг программы
- private void DeleteEvents()
- {
- client.UpdateUsers -= new UpdateUserListEventDelegate(UpdateUsers);
- client.UserJoined -= new UserJoinedEventDelegate(UserJoined);
- client.UserLeft -= new UserLeftEventDelegate(UserLeft);
- client.ChannelMessage -= new ChannelMesssageEventDelegate(ChannelMessage);
- client.NoticeMessage -= new NoticeMessageEventDelegate(NoticeMessage);
- client.PrivateMessage -= new PrivateMessageEventDelegate(PrivateMessage);
- client.Disconnected -= new DisconnectedEventDelegate(Disconnect);
- client.ServerMessage -= new ServerMessageEventDelegate(ShowMessage);
- }// DeleteEvents
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д