Skip to content

Commit

Permalink
fixed thread aborting
Browse files Browse the repository at this point in the history
  • Loading branch information
Zwirbelbart committed May 1, 2015
1 parent 05ebc00 commit 7359e62
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 83 deletions.
6 changes: 3 additions & 3 deletions src/IrcClient/Channel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public class Channel
{
private string _Name;
private string _Key = String.Empty;
private Hashtable _Users = Hashtable.Synchronized(new Hashtable(new CaseInsensitiveHashCodeProvider(), new CaseInsensitiveComparer()));
private Hashtable _Ops = Hashtable.Synchronized(new Hashtable(new CaseInsensitiveHashCodeProvider(), new CaseInsensitiveComparer()));
private Hashtable _Voices = Hashtable.Synchronized(new Hashtable(new CaseInsensitiveHashCodeProvider(), new CaseInsensitiveComparer()));
private Hashtable _Users = Hashtable.Synchronized(new Hashtable(StringComparer.OrdinalIgnoreCase));
private Hashtable _Ops = Hashtable.Synchronized(new Hashtable(StringComparer.OrdinalIgnoreCase));
private Hashtable _Voices = Hashtable.Synchronized(new Hashtable(StringComparer.OrdinalIgnoreCase));
private StringCollection _Bans = new StringCollection();
private List<string> _BanExcepts = new List<string>();
private List<string> _InviteExcepts = new List<string>();
Expand Down
4 changes: 2 additions & 2 deletions src/IrcClient/IrcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public class IrcClient : IrcCommands
private bool _MotdReceived;
private Array _ReplyCodes = Enum.GetValues(typeof(ReplyCode));
private StringCollection _JoinedChannels = new StringCollection();
private Hashtable _Channels = Hashtable.Synchronized(new Hashtable(new CaseInsensitiveHashCodeProvider(), new CaseInsensitiveComparer()));
private Hashtable _IrcUsers = Hashtable.Synchronized(new Hashtable(new CaseInsensitiveHashCodeProvider(), new CaseInsensitiveComparer()));
private Hashtable _Channels = Hashtable.Synchronized(new Hashtable(StringComparer.OrdinalIgnoreCase));
private Hashtable _IrcUsers = Hashtable.Synchronized(new Hashtable(StringComparer.OrdinalIgnoreCase));
private List<ChannelInfo> _ChannelList;
private Object _ChannelListSyncRoot = new Object();
private AutoResetEvent _ChannelListReceivedEvent;
Expand Down
7 changes: 4 additions & 3 deletions src/IrcClient/NonRfcChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

using System;
using System.Collections;
using System.Collections.Specialized;

Expand All @@ -37,9 +38,9 @@ namespace Meebey.SmartIrc4net
/// <threadsafety static="true" instance="true" />
public class NonRfcChannel : Channel
{
private Hashtable _Owners = Hashtable.Synchronized(new Hashtable(new CaseInsensitiveHashCodeProvider(), new CaseInsensitiveComparer()));
private Hashtable _ChannelAdmins = Hashtable.Synchronized(new Hashtable(new CaseInsensitiveHashCodeProvider(), new CaseInsensitiveComparer()));
private Hashtable _Halfops = Hashtable.Synchronized(new Hashtable(new CaseInsensitiveHashCodeProvider(), new CaseInsensitiveComparer()));
private Hashtable _Owners = Hashtable.Synchronized(new Hashtable(StringComparer.OrdinalIgnoreCase));
private Hashtable _ChannelAdmins = Hashtable.Synchronized(new Hashtable(StringComparer.OrdinalIgnoreCase));
private Hashtable _Halfops = Hashtable.Synchronized(new Hashtable(StringComparer.OrdinalIgnoreCase));

/// <summary>
///
Expand Down
148 changes: 73 additions & 75 deletions src/IrcConnection/IrcConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1051,54 +1051,54 @@ private void _OnConnectionError(object sender, EventArgs e)
}
}

/// <summary>
///
/// </summary>
private abstract class FiniteThread
{
/// <summary>
///
/// </summary>
public Thread Thread { get; internal set; }

/// <summary>
///
/// </summary>
public bool IsStopRequested { get; internal set; }

/// <summary>
///
/// </summary>
public string ThreadName { get; internal set; }

/// <summary>
///
/// </summary>
public void RequestStop()
{
IsStopRequested = true;
}

protected FiniteThread()
{
IsStopRequested = false;
ThreadName = "(unnamed thread)";
}

protected abstract void PrepareStart();

protected abstract void Worker();

/// <summary>
///
/// </summary>
public void Start() {
PrepareStart();

Thread = new Thread(Worker) { IsBackground = true, Name = ThreadName};
Thread.Start();
}
}
/// <summary>
///
/// </summary>
private abstract class FiniteThread
{
/// <summary>
///
/// </summary>
public Thread Thread { get; internal set; }

/// <summary>
///
/// </summary>
public bool IsStopRequested { get; internal set; }

/// <summary>
///
/// </summary>
public string ThreadName { get; internal set; }

/// <summary>
///
/// </summary>
public void RequestStop()
{
IsStopRequested = true;
}

protected FiniteThread()
{
IsStopRequested = false;
ThreadName = "(unnamed thread)";
}

protected abstract void PrepareStart();

protected abstract void Worker();

/// <summary>
///
/// </summary>
public void Start() {
PrepareStart();

Thread = new Thread(Worker) { IsBackground = true, Name = ThreadName};
Thread.Start();
}
}

/// <summary>
///
Expand Down Expand Up @@ -1127,15 +1127,15 @@ public ReadThread(IrcConnection connection)
{
_Connection = connection;
QueuedEvent = new AutoResetEvent(false);
ThreadName = "ReadThread ("+_Connection.Address+":"+_Connection.Port+")";
ThreadName = "ReadThread ("+_Connection.Address+":"+_Connection.Port+")";
}

protected override void PrepareStart() { }
protected override void PrepareStart() { }

protected override void Worker()
protected override void Worker()
{
#if LOG4NET
Logger.Socket.Debug("ReadThread started");
Logger.Socket.Debug("ReadThread Worker(): starting");
#endif
try {
string data = "";
Expand All @@ -1148,8 +1148,15 @@ protected override void Worker()
Logger.Socket.Debug("received: \""+data+"\"");
#endif
}
#if LOG4NET
Logger.Socket.Debug("ReadThread Worker(): loop ended");
Logger.Socket.Debug("ReadThread Worker(): closing reader");
#endif
_Connection._Reader.Close();

_Queue.Clear();
// clean up our receive queue else we continue processing old
// messages when the read thread is restarted!
_Queue.Clear();
} catch (IOException e) {
#if LOG4NET
Logger.Socket.Warn("IOException: "+e.Message);
Expand All @@ -1164,11 +1171,6 @@ protected override void Worker()
_Connection.IsConnectionError = true;
}
}
} catch (ThreadAbortException) {
Thread.ResetAbort();
#if LOG4NET
Logger.Socket.Debug("ReadThread aborted");
#endif
} catch (Exception ex) {
#if LOG4NET
Logger.Socket.Error(ex);
Expand Down Expand Up @@ -1206,15 +1208,15 @@ public WriteThread(IrcConnection connection)
{
_Connection = connection;
QueuedEvent = new AutoResetEvent(false);
ThreadName = "WriteThread (" + _Connection.Address + ":" + _Connection.Port + ")";
ThreadName = "WriteThread (" + _Connection.Address + ":" + _Connection.Port + ")";
}

protected override void PrepareStart() { }
protected override void PrepareStart() { }

protected override void Worker()
protected override void Worker()
{
#if LOG4NET
Logger.Socket.Debug("WriteThread started");
Logger.Socket.Debug("WriteThread Worker(): starting");
#endif
try {
try {
Expand All @@ -1226,8 +1228,11 @@ protected override void Worker()
Thread.Sleep(_Connection._SendDelay);
} while (!isBufferEmpty);
}

_Connection._Writer.Close();
#if LOG4NET
Logger.Socket.Debug("WriteThread Worker(): loop ended");
Logger.Socket.Debug("WriteThread Worker(): closing writer");
#endif
_Connection._Writer.Close();
} catch (IOException e) {
#if LOG4NET
Logger.Socket.Warn("IOException: " + e.Message);
Expand All @@ -1242,11 +1247,6 @@ protected override void Worker()
_Connection.IsConnectionError = true;
}
}
} catch (ThreadAbortException) {
Thread.ResetAbort();
#if LOG4NET
Logger.Socket.Debug("WriteThread aborted");
#endif
} catch (Exception ex) {
#if LOG4NET
Logger.Socket.Error(ex);
Expand Down Expand Up @@ -1426,7 +1426,7 @@ private class IdleWorkerThread : FiniteThread
public IdleWorkerThread(IrcConnection connection)
{
_Connection = connection;
ThreadName = "IdleWorkerThread ("+_Connection.Address+":"+_Connection.Port+")";
ThreadName = "IdleWorkerThread ("+_Connection.Address+":"+_Connection.Port+")";
}

/// <summary>
Expand All @@ -1439,10 +1439,10 @@ protected override void PrepareStart()
_Connection._LastPongReceived = now;
}

protected override void Worker()
protected override void Worker()
{
#if LOG4NET
Logger.Socket.Debug("IdleWorkerThread started");
Logger.Socket.Debug("IdleWorkerThread Worker(): starting");
#endif
try {
while (!IsStopRequested && _Connection.IsConnected ) {
Expand Down Expand Up @@ -1482,10 +1482,8 @@ protected override void Worker()
break;
}
}
} catch (ThreadAbortException) {
Thread.ResetAbort();
#if LOG4NET
Logger.Socket.Debug("IdleWorkerThread aborted");
Logger.Socket.Debug("IdleWorkerThread Worker(): loop ended");
#endif
} catch (Exception ex) {
#if LOG4NET
Expand Down

0 comments on commit 7359e62

Please sign in to comment.