Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactored threadpool to task #166

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/Engine-IRC/Protocols/Irc/IrcProtocolManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
using Meebey.SmartIrc4net;
using Smuxi.Common;
using IrcProxyType = Meebey.SmartIrc4net.ProxyType;
using System.Threading.Tasks;

namespace Smuxi.Engine
{
Expand Down Expand Up @@ -1398,7 +1399,7 @@ public void CommandQuery(CommandModel cd)
} else {
// HACK: lower probability of sync race condition swallowing
// messages, see: https://www.smuxi.org/issues/show/634
ThreadPool.QueueUserWorkItem(delegate {
Task.Factory.StartNew(()=> {
Thread.Sleep(1000);
Session.SyncChat(chat);
});
Expand Down Expand Up @@ -3012,7 +3013,7 @@ private void _OnQueryMessage(object sender, IrcEventArgs e)
Session.AddMessageToChat(chat, msg);
// HACK: lower probability of sync race condition swallowing
// messages, see: https://www.smuxi.org/issues/show/634
ThreadPool.QueueUserWorkItem(delegate {
Task.Factory.StartNew(()=> {
Thread.Sleep(1000);
Session.SyncChat(chat);
});
Expand Down Expand Up @@ -3057,7 +3058,7 @@ private void _OnQueryAction(object sender, ActionEventArgs e)
Session.AddMessageToChat(chat, msg);
// HACK: lower probability of sync race condition swallowing
// messages, see: https://www.smuxi.org/issues/show/634
ThreadPool.QueueUserWorkItem(delegate {
Task.Factory.StartNew(()=> {
Thread.Sleep(1000);
Session.SyncChat(chat);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
using Twitterizer;
using Twitterizer.Core;
using Smuxi.Common;
using System.Threading.Tasks;

namespace Smuxi.Engine
{
Expand Down Expand Up @@ -302,7 +303,7 @@ public override void Connect(FrontendManager fm, ServerModel server)
}

// twitter is sometimes pretty slow, so fetch this in the background
ThreadPool.QueueUserWorkItem(delegate {
Task.Factory.StartNew(delegate {
try {
// FIXME: replace with AutoResetEvent
while (!HasTokens) {
Expand Down Expand Up @@ -361,7 +362,7 @@ public override void Connect(FrontendManager fm, ServerModel server)
Session.AddMessageToChat(Chat, msg);
}
});
ThreadPool.QueueUserWorkItem(delegate {
Task.Factory.StartNew(delegate {
try {
// FIXME: replace with AutoResetEvent
// f_TwitterUser needed for proper self detection in the
Expand Down
10 changes: 7 additions & 3 deletions src/Engine-XMPP/Protocols/Xmpp/XmppProtocolManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
using Smuxi.Common;
using System.Runtime.CompilerServices;
using agsXMPP.protocol.extensions.nickname;
using System.Threading.Tasks;

namespace Smuxi.Engine
{
Expand Down Expand Up @@ -574,7 +575,8 @@ public void OpenContactChat()
}

// HACK: lower probability of sync race condition during connect
ThreadPool.QueueUserWorkItem(delegate {
Task.Factory.StartNew(()=>
{
Thread.Sleep(5000);
lock (this) {
if (IsDisposed) {
Expand Down Expand Up @@ -1855,7 +1857,8 @@ void OnGroupChatPresence(XmppGroupChatModel chat, Presence pres)
if (pres.From.Resource == chat.OwnNickname) {
chat.IsJoining = false;
// HACK: lower probability of sync race condition swallowing messages
ThreadPool.QueueUserWorkItem(delegate {
Task.Factory.StartNew(()=>
{
Thread.Sleep(1000);
lock (this) {
if (IsDisposed) {
Expand Down Expand Up @@ -2485,7 +2488,8 @@ void Reconnect(TimeSpan span)
builder.AppendText(_("Reconnecting to {0} in {1} seconds"),
JabberClient.Server, span.TotalSeconds);
Session.AddMessageToChat(Chat, builder.ToMessage());
ThreadPool.QueueUserWorkItem(delegate {
Task.Factory.StartNew(()=>
{
Thread.Sleep(delay);
lock (this) {
// prevent this timer from calling connect after it has been closed
Expand Down
11 changes: 6 additions & 5 deletions src/Engine/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
using System.Runtime.Remoting;
using System.Threading;
using Smuxi.Common;
using System.Threading.Tasks;

namespace Smuxi.Engine
{
Expand Down Expand Up @@ -245,7 +246,7 @@ public void RegisterFrontendUI(IFrontendUI ui)

var srv = server;
// run connects in background threads as they block
ThreadPool.QueueUserWorkItem(delegate {
Task.Factory.StartNew(()=> {
bool isError = false;
try {
IProtocolManager protocolManager = Connect(srv, fm);
Expand Down Expand Up @@ -574,7 +575,7 @@ public void CommandConnect(CommandModel cd)
}

// run in background so it can't block the command queue
ThreadPool.QueueUserWorkItem(delegate {
Task.Factory.StartNew(()=> {
try {
if (protocolManager == null && server != null) {
protocolManager = Connect(server, fm);
Expand Down Expand Up @@ -652,7 +653,7 @@ public void CommandReconnect(CommandModel cd)
return;
}

ThreadPool.QueueUserWorkItem(delegate {
Task.Factory.StartNew(()=> {
try {
pm.Reconnect(cd.FrontendManager);
} catch (Exception ex) {
Expand Down Expand Up @@ -941,7 +942,7 @@ private void _CommandNetworkClose(CommandModel cd)
}

// disconnect in background as could be blocking
ThreadPool.QueueUserWorkItem(delegate {
Task.Factory.StartNew(()=> {
try {
pm.Disconnect(cd.FrontendManager);
pm.Dispose();
Expand Down Expand Up @@ -1416,7 +1417,7 @@ public IProtocolManager Connect(ServerModel server, FrontendManager frontendMana

if (server.OnConnectCommands != null && server.OnConnectCommands.Count > 0) {
protocolManager.Connected += delegate {
ThreadPool.QueueUserWorkItem(delegate {
Task.Factory.StartNew(()=> {
try {
foreach (string command in server.OnConnectCommands) {
if (command.Length == 0) {
Expand Down