Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-threading C# 2.0 confusion

I am trying to figure out how to multi-thread an application. I am stuck trying to find the entry point to start the thread.

The thread that I am trying to start is : plugin.FireOnCommand(this, newArgs);

...
PluginBase plugin = Plugins.GetPlugin(Commands.GetInternalName(command));
plugin.FireOnCommand(this, newArgs);
...

The FireOnCommand method is:

 public void FireOnCommand(BotShell bot, CommandArgs args)

I am not having any luck using ParameterizedThreadStart or ThreadStart, I can't seem to get the syntax correct.

EDIT: Tried both

Thread newThread = 
  new Thread(new ParameterizedThreadStart(plugin.FireOnCommand(this, newArgs))); 

and

Thread newThread = 
  new Thread(new ThreadStart(plugin.FireOnCommand(this, newArgs)));
like image 445
Michael Hartmann Avatar asked Jul 29 '26 22:07

Michael Hartmann


1 Answers

In .NET 2, you would need to create a method for this, with a custom type. For example, you could do:

internal class StartPlugin
{
    private BotShell bot;
    private CommandArgs args;
    private PluginBase plugin;

    public StartPlugin(PluginBase plugin, BotShell bot, CommandArgs args)
    {
       this.plugin = plugin;
       this.bot = bot;
       this.args = args;
    }

    public void Start()
    {
        plugin.FireOnCommand(bot, args);
    }
}

You can then do:

StartPlugin starter = new StartPlugin(plugin, this, newArgs);

Thread thread = new Thread(new ThreadStart(starter.Start));
thread.Start();
like image 186
Reed Copsey Avatar answered Aug 01 '26 11:08

Reed Copsey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!