0

Im trying to figure out how Im able to run an async in a thread so i can make this more efficient for my discord bot.

using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Discord;
using Gommon;
using Qmmands;
using Volte.Core.Entities;
using Volte.Commands;
using Volte.Core.Helpers;
using Volte.Services;
using System.Threading;
using Discord.WebSocket;

namespace Volte.Commands.Modules
{
    public sealed partial class BotOwnerModule
    {
        private readonly DiscordShardedClient _client;
        [Command("CreatePoll", "Cp")]
        [Description("Creates polls and sends to every server.")]
        public Task<ActionResult> CreatePollAync([Remainder, Description("The content of the poll. Format is `question;option1[;option2;option3;option4;option5]`. You do not need to provide the brackets.")] string poll)
        {
            var content = poll.Split(';', StringSplitOptions.RemoveEmptyEntries);
            var pollInfo = PollHelper.GetPollBody(content);
            if (!pollInfo.IsValid)
                return BadRequest(content.Length - 1 > 5
                    ? "More than 5 options were specified."
                    : "No options specified.");

            var embed = Context.CreateEmbedBuilder()
                .WithTitle(Format.Bold(content[0]));

            return None(async () =>
            {

                foreach (var guild in Context.Client.Guilds)
                {
                    new Thread(() => {

                        Thread.CurrentThread.IsBackground = true;
                        await Context.Client.GetGuild(guild.Id).GetTextChannel(new DatabaseService(_client).GetData(guild).Configuration.channelid).SendMessageAsync("test");
                         var m = await pollInfo.Apply(embed).SendToAsync(Context.Client.GetGuild(guild.Id).GetTextChannel(new DatabaseService(_client).GetData(guild).Configuration.channelid));
                            _ = await Context.Message.TryDeleteAsync("Poll invocation message.");
                           await PollHelper.AddPollReactionsAsync(pollInfo.Fields.Count, m);
                    }).Start();
                }
            });
        }
    }
}

I have tried a few ways like returning None inside of the thread but that didnt work either so thats why im on here asking for help as i have googled this issue but this exact issue doesnt seem to pop up

  • 2
    All code your program executes, it executes in a thread. There's no "in a thread/not in a thread". Likewise, async/await works the same no matter which thread you are in, other than the obvious distinction of threads with synchronization contexts and those without. See duplicate for details. If you still need help, post a question that includes a proper [mcve], along with a detailed explanation of what that code does, how it's different from what you want, and what _specifically_ you need help with. – Peter Duniho Jun 01 '21 at 07:07
  • Related: [Async thread body loop, It just works, but how?](https://stackoverflow.com/questions/30044846/async-thread-body-loop-it-just-works-but-how) In short, passing an async delegate to the `Thread` constructor is a bug. It results to an `async void` method, which is almost certainly [not what you want](https://learn.microsoft.com/en-us/archive/msdn-magazine/2013/march/async-await-best-practices-in-asynchronous-programming#avoid-async-void). – Theodor Zoulias Jun 01 '21 at 08:51

0 Answers0