Skip to content

Commit 0f03c3e

Browse files
committed
corrections to MessageBulker to workaround C# tasks being iffy
1 parent 2deabfe commit 0f03c3e

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

MessageBulker.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Text;
5+
using System.Threading;
56
using System.Threading.Tasks;
67
using FreneticUtilities.FreneticToolkit;
78
using Discord;
@@ -41,15 +42,16 @@ public struct InternalData
4142
public bool HasSendTaskActive;
4243

4344
/// <summary>The internal method that gets scheduled for later when scheduling a delayed send task.</summary>
44-
public void WaitingTask()
45+
public static void WaitingTask(object _self)
4546
{
47+
MessageBulker self = _self as MessageBulker;
4648
Task.Delay(400).Wait();
47-
lock (Locker)
49+
lock (self.Internal.Locker)
4850
{
49-
HasSendTaskActive = false;
51+
self.Internal.HasSendTaskActive = false;
5052
try
5153
{
52-
DoSend();
54+
self.Internal.DoSend();
5355
}
5456
catch (Exception ex)
5557
{
@@ -69,8 +71,7 @@ public void ScheduleTask()
6971
}
7072
HasSendTaskActive = true;
7173
}
72-
MessageBulker self = Self; // C# gets iffy around structs and tasks, don't remove this
73-
Task.Factory.StartNew(() => self.Internal.WaitingTask);
74+
new Thread(new ParameterizedThreadStart(WaitingTask)).Start(Self); // Not using tasks due to C# being janky around tasks with structs
7475
}
7576

7677
/// <summary>The internal direct send-to-channel message. Do not call directly, use <see cref="Send(string)"/>.</summary>
@@ -104,7 +105,7 @@ public void DoSend()
104105
LastSentTicks = Environment.TickCount64;
105106
}
106107
MessageBulker self = Self;
107-
Task.Factory.StartNew(() =>
108+
Task.Factory.StartNew(() => // TODO: Does this need to be swapped to Thread as well?
108109
{
109110
try
110111
{
@@ -136,7 +137,7 @@ public void Send(string text)
136137
lock (Internal.Locker)
137138
{
138139
Internal.ToSend.Enqueue(text);
139-
if (!Internal.HasSendTaskActive && (Internal.LastSentTicks <= 0 || Internal.LastSentTicks + 500 < Environment.TickCount64 || text.Length > 1800))
140+
if (!Internal.HasSendTaskActive && (Internal.LastSentTicks <= 0 || Internal.LastSentTicks + 1500 < Environment.TickCount64 || text.Length > 1800))
140141
{
141142
Internal.DoSend();
142143
}

0 commit comments

Comments
 (0)