-3

I am trying to allow two of my methods to run every ms in the background of my script. However, when my method's are called, the everything else stops running while they are active, I believe this is because at the end of my method I call them both again. I need to run these every ms so everything works correctly, while also allowing everything else to run. Here is the code:

public async Task MuteUpdate()
        {
            //UPDATE `mutes` SET MuteUntil = MuteUntil - 1

            string connstr = "Server=echstreme.de;Port=3306;Database=c1Look;Uid=c1Look;Pwd=Redacted;SSL Mode =None";
            MySqlConnection conn = new MySqlConnection(connstr);

            try
            {
                conn.Open();
                MySqlCommand cmd = new MySqlCommand(SQLMUteRemove, conn);

                cmd.ExecuteNonQuery();

                
                    conn.Close();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
             MuteRead();
             MuteUpdate();
            
        }

        public  async Task MuteRead()
        {
            string connStr = "Server=echstreme.de;Port=3306;Database=c1Look;Uid=c1Look;Pwd=redactedINfo;SSL Mode =None";
            MySqlConnection conn = new MySqlConnection(connStr);
            try
            {

                conn.Open();

                MySqlCommand cmd = new MySqlCommand("SELECT * FROM `mutes` WHERE MuteUntil <= 0", conn);
                MySqlDataReader rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {
                    ulong id = Convert.ToUInt64(rdr[1]);
                    Context.Guild.GetUser(id);
                    await SendLog("__Mute Manager__", "**Member**        | <@" + rdr[1] + ">\n **Punishment**       | UNMUTE" + "\n **Punished by**          | <@" + rdr[4] + ">");
                    sqlQuery = "DELETE FROM `mutes` WHERE `UserID` = " + rdr[1] + ";";
                    await Context.Guild.GetUser(id).RemoveRoleAsync(876561161062076416);
                }
                rdr.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
Luke
  • 101
  • 1
  • 1
  • 7
  • 1
    https://stackoverflow.com/questions/2775692/c-sharp-and-mysql-net-connector-any-way-of-preventing-sql-injection-attacks-i – mjwills Aug 19 '21 at 23:08
  • 1
    Please share a [mcve]. Short (and incomplete) answer, to get you _started_ - give `Task.Run` a whirl. – mjwills Aug 19 '21 at 23:09
  • If you want to run anything at some time interval, why not use a timer? – JonasH Aug 20 '21 at 07:01

1 Answers1

0

You don't use the await keyword anywhere in MuteUpdate, so that function will run synchronously. One way you can solve this by using Task.Run to transform a function or section of code into an awaitable task.

Either use it inside your function like this:

public async Task foo()
{
    await Task.Run(() => {
        // Do things.
    };
}

Or create an "async" version of your function:

public void foo()
{
    // Do things.
}

public async Task fooAsync()
{
    await Task.Run(() => foo());
}
iWiggins
  • 636
  • 1
  • 6
  • 12