English 中文(简体)
为什么必须派一名代表来弥合read子及其方法?
原标题:Why there must be a delegate to bridge a Thread and its Method?

下述法典很常见:

Work w = new Work();
w.Data = 42;
threadDelegate = new ThreadStart(w.DoMoreWork);
newThread = new Thread(threadDelegate);
newThread.Start();

我很想知道,为什么必须派一名代表来bridge,Thread and the Methods to Implement on that thread?

难道我们只是将方法名称直接寄给Thread吗?

最佳回答

难道我们只是将方法名称直接寄给Thread吗?

作为一种扼杀手段? E/CN.6/2009/1。 如果不是说说是str,那么你将如何提议告诉read子什么执行? 代表是idiomatic在NET中代表“某种执行需要特定签名”。

请注意,您无需使用<代码>新版Start或使用单独的变量。 采用方法组别转换法进行罚款:

newThread = new Thread(w.DoMoreWork);
newThread.Start();

或者,如果你赢得,则需要所有这些变量:

new Thread(new Work { Data = 42 }.DoWork).Start();

EDIT: Note that as of .NET 4, it s generally a better concept to use the Task Equal Library for this such of what.

问题回答

代表是一种数据类型,就像Integer或双重数据一样。 代表界定了由其签名确定的职能类别(回归类型和参数)。 如同“Integer”方法参数一样,一名代表明确指出,预期会出现一种带有特定签名的方法。 你们需要具体说明预期的哪类功能,其原因与使用原始数据类型相同。 当你履行职务时,它就通过你想要援引的职能或反对方法,即确定该职能的记忆中的位置。 如果你刚刚以职能名称通过,就不知道该名称的功能在哪里;除非你对含有你所要求职能定义的类别作出某种界定。

See the following for more info on delegates:

微软的C#代表信息

stackoverflow question - 这就是为什么-do-we-need-c-sharp-delegates

以下链接是利用 Java的反思以名义援引方法的一个例子。 你可以认为,你需要具体说明哪类含有你想要援引的方法。 代表的实际参数是该方法的一个直接点,这就是签字必须匹配的原因。

rel=“nofollow noreferer”>Java reflection





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签