System.Thread和多个方法调用 [英] System.Thread and multiple method calls

查看:65
本文介绍了System.Thread和多个方法调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨组,


我对C#比较新(虽然我有很多编程

在Java和C等其他语言上的经验)。目前我是

寻找这个问题的解决方案:


假设你有3个方法A,B和C.所有这些都应该运行

线程(I / O任务)和一个接一个。我可以创建一个线程和

安排一个方法就像:


线程t =新线程(A);


但是如何添加其他2种方法呢?这可以用.NET库类以某种方式完成,或者我必须创建自己的Thread-Method -

Manager吗?以此目的?我还想添加和删除动态执行的

方法。


Thansk的想法!


问候,

Paul

Hi group,

I am relatively new to C# (although I have a lot of programming
excperience in other languages like Java and C). Currently I am
searching for a solution to this problem:

Suppose you have 3 methods A, B and C. All of them shall be run
threaded (I/O tasks) and one after another. I can create a thread and
schedule one method just like:

Thread t = new Thread(A);

But how do I add the other 2 methods? Can this be done somehow with
the .NET Library classes or do I have to create my own "Thread-Method-
Manager" for this purpose? I also would like to add and remove the
methods being executed dynamically.

Thansk for your ideas!

Regards,
Paul

推荐答案



" Paul Schwann" < pa ********** @ gmail.comwrote in message

news:66 ******************** ************** @ e23g2000 prf.googlegroups.com ...

"Paul Schwann" <pa**********@gmail.comwrote in message
news:66**********************************@e23g2000 prf.googlegroups.com...

嗨组,

我对C#相对较新(虽然我有很多编程

在Java和C等其他语言中的经验)。目前我是

寻找这个问题的解决方案:


假设你有3个方法A,B和C.所有这些都应该运行

线程(I / O任务)和一个接一个。我可以创建一个线程和

安排一个方法就像:


线程t =新线程(A);


但是如何添加其他2种方法呢?这可以用.NET库类以某种方式完成,或者我必须创建自己的Thread-Method -

Manager吗?以此目的?我还想添加和删除动态执行的

方法。


Thansk的想法!


问候,

Paul
Hi group,

I am relatively new to C# (although I have a lot of programming
excperience in other languages like Java and C). Currently I am
searching for a solution to this problem:

Suppose you have 3 methods A, B and C. All of them shall be run
threaded (I/O tasks) and one after another. I can create a thread and
schedule one method just like:

Thread t = new Thread(A);

But how do I add the other 2 methods? Can this be done somehow with
the .NET Library classes or do I have to create my own "Thread-Method-
Manager" for this purpose? I also would like to add and remove the
methods being executed dynamically.

Thansk for your ideas!

Regards,
Paul



嗨Paul,


对Java和C不太确定,但在我用过的每种语言中,

线程都有一个起始点和线性执行路径,就像

" main"程序线程。所以,在你的情况下,如果你想执行方法A,

B和C,你需要创建一个调用A,B和C的新方法D和

使用方法D作为你的线程proc。当然,您可以将参数传递给

方法D,以确定要执行的其他方法。


我想你可以*实现某种方式消息泵(就像'main'

程序线程通常所拥有的那样)并动态执行基于

消息的方法,但这似乎有点过分,取决于什么你是想要完成的



Scott

Hi Paul,

Not so sure about Java and C, but in every language I''ve ever worked in a
thread has a single starting point and linear execution path, just like the
"main" program thread. So, in your case, if you want to execute methods A,
B, and C, you would need to create a new method D that calls A, B, and C and
use method D as your thread proc. You can, of course, pass parameters to
method D to identify which other methods to be executed.

I suppose you *could* implement some sort of message pump (like the "main"
program thread usually has) and dynamically execute methods based on
messages, but that seems like it may be overkill, depending on what you are
trying to accomplish.

Scott


Hi Scott,


感谢您的回复!自己实施这样一种方法就是我现在或多或少会做的事情。我只是觉得它会在某种代表的基础上有多好......



线程t =新线程();

t.executeThis + = A;

t.start();

....

t.executeThis + = B;

....

t.executeThis + = C;

....

t .executeThis - = A;


....你明白了...... :-)


问候,

Paul
Hi Scott,

thanks for your reply! Implementing such a method D by myself is what
I more or less will do now. I just thought how nice it would be based
on some kind of delegates... Like

Thread t = new Thread();
t.executeThis += A;
t.start();
....
t.executeThis += B;
....
t.executeThis += C;
....
t.executeThis -= A;

.... You get the idea... :-)

Regards,
Paul


Scott Roberts< sr ****** @ no.spam.here-webworks-software.comwrote:
Scott Roberts <sr******@no.spam.here-webworks-software.comwrote:

对Java和C不太确定,但在我用过的每种语言中,
线程只有一个起点和线性执行路径,比如

" main"程序线程。所以,在你的情况下,如果你想执行方法A,

B和C,你需要创建一个调用A,B和C的新方法D和

使用方法D作为你的线程proc。当然,您可以将参数传递给

方法D,以确定要执行的其他方法。
Not so sure about Java and C, but in every language I''ve ever worked in a
thread has a single starting point and linear execution path, just like the
"main" program thread. So, in your case, if you want to execute methods A,
B, and C, you would need to create a new method D that calls A, B, and C and
use method D as your thread proc. You can, of course, pass parameters to
method D to identify which other methods to be executed.



是的。一个皱纹对于.NET来说,当你创建一个

线程时传入的是一个委托 - 而且委托可以有多个目标。我以前从未以这种方式使用过ThreadStart,但它会起作用。这是

很奇怪...


使用系统;

使用System.Threading;


类测试

{

static void Main()

{

ThreadStart start = null;


start + = A;

start + = B;

start + = C;


新主题(开始)。开始();

}


静态无效A()

{

Console.WriteLine(" A");

}


static void B()

{

Console.WriteLine(" B");

}


static void C()

{

Console.WriteLine(" C");

}

}


请注意,在你已经启动线程后,你不能添加到要执行的方法列表中。

Yup. One "wrinkle" to .NET is that what you pass in when you create a
thread is a delegate - and delegates can have multiple targets. I''ve
*never* used ThreadStart in this way before, but it will work. It''s
pretty odd...

using System;
using System.Threading;

class Test
{
static void Main()
{
ThreadStart start = null;

start += A;
start += B;
start += C;

new Thread(start).Start();
}

static void A()
{
Console.WriteLine ("A");
}

static void B()
{
Console.WriteLine ("B");
}

static void C()
{
Console.WriteLine ("C");
}
}

Note that you can''t add to the list of methods to execute after you''ve
started the thread though.


我想你可以*实现某种消息泵(比如主要的

程序线程通常有)a并动态执行基于

消息的方法,但这似乎有点矫枉过正,取决于你想要完成的

I suppose you *could* implement some sort of message pump (like the "main"
program thread usually has) and dynamically execute methods based on
messages, but that seems like it may be overkill, depending on what you are
trying to accomplish.



再次,我同意。幸运的是,如果OP *确实*需要这样做,那么对于生产者/消费者队列而言,这并不是很难实现。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

英国的世界级.NET培训: http://iterativetraining.co.uk

Again, I agree. Fortunately if the OP *does* need to do this, it''s not
too hard to do with a producer/consumer queue.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


这篇关于System.Thread和多个方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆