线程池参数

发布于 2024-12-01 14:41:03 字数 130 浏览 1 评论 0原文

我一直在到处搜索,我需要一个解决方案,让我可以使用接受参数的特定函数添加 200-300 个作业。我知道委托和对象作为单个参数,但我希望有一些东西可以允许使用不同的参数对每个任务进行排队,而不仅仅是对象参数。

任何帮助将不胜感激。

I've been searching everywhere and I need a solution that would let me add 200-300 jobs with a certain function THAT ACCEPTS parameters. I know about delegate and object as single parameter but I was hoping for something that would allow to queue the tasks each with different paramentars instead of object parametar only.

Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

赠我空喜 2024-12-08 14:41:03

您可以执行以下操作:

void MyMethod(int param)
{
    ....
}

...

ThreadPool.QueueUserWorkItem(_ => MyMethod(1));
ThreadPool.QueueUserWorkItem(_ => MyMethod(2));
ThreadPool.QueueUserWorkItem(_ => MyMethod(3));
...
ThreadPool.QueueUserWorkItem(_ => MyMethod(42));

另一种选择是让 MyMethod 接受 Object 类型的参数,并使用 QueueUserWorkItem 的第二个重载:

void MyMethod(object param)
{
    int value = (int)param;
    ....
}

...

ThreadPool.QueueUserWorkItem(MyMethod, 1);
ThreadPool.QueueUserWorkItem(MyMethod, 2);
ThreadPool.QueueUserWorkItem(MyMethod, 3);
...
ThreadPool.QueueUserWorkItem(MyMethod, 42);

You can do something like this:

void MyMethod(int param)
{
    ....
}

...

ThreadPool.QueueUserWorkItem(_ => MyMethod(1));
ThreadPool.QueueUserWorkItem(_ => MyMethod(2));
ThreadPool.QueueUserWorkItem(_ => MyMethod(3));
...
ThreadPool.QueueUserWorkItem(_ => MyMethod(42));

Another option is to make MyMethod accept a parameter of type Object, and use the second overload of QueueUserWorkItem:

void MyMethod(object param)
{
    int value = (int)param;
    ....
}

...

ThreadPool.QueueUserWorkItem(MyMethod, 1);
ThreadPool.QueueUserWorkItem(MyMethod, 2);
ThreadPool.QueueUserWorkItem(MyMethod, 3);
...
ThreadPool.QueueUserWorkItem(MyMethod, 42);
最佳男配角 2024-12-08 14:41:03

简单样本:

        for (int i = 0; i < 100; i++)
        {
            System.Threading.ThreadPool.QueueUserWorkItem(k =>
            {
                TestMethod(k);
            }, i);
        }

simple sample :

        for (int i = 0; i < 100; i++)
        {
            System.Threading.ThreadPool.QueueUserWorkItem(k =>
            {
                TestMethod(k);
            }, i);
        }
久伴你 2024-12-08 14:41:03

没有什么可以阻止您将对象列表作为对象参数发送,因此实际上您可以将任意数量的参数传递给线程函数。

There is nothing that prevents you from sending a list of objects as an object parameters, so in practice you can pass any number of parameters to a thread function.

半衾梦 2024-12-08 14:41:03

您是否正在寻找参数化线程启动(下面的示例)

string myUrl = 'asd'
Thread t = new Thread (new ParameterizedThreadStart(FetchUrl));
t.Start (myUrl);


static void FetchUrl(object url)
{
    // use url here, probably casting it to a known type before use
}

或者您可以使用 System.Threading.Tasks 中找到的任务..

Task.Factory.StartNew(() => {
                    File.WriteAllBytes(path, response);
                });

are you looking for a paramterized thread start(example below)

string myUrl = 'asd'
Thread t = new Thread (new ParameterizedThreadStart(FetchUrl));
t.Start (myUrl);


static void FetchUrl(object url)
{
    // use url here, probably casting it to a known type before use
}

or you can use tasks found in System.Threading.Tasks..

Task.Factory.StartNew(() => {
                    File.WriteAllBytes(path, response);
                });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文