将对象传递给工作线程函数 [英] Passing Objects Into Worker Thread Function

查看:64
本文介绍了将对象传递给工作线程函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VS2005用于Windows窗体应用程序。我需要能够使用工作线程函数来从UI中卸载一些处理工作。
线程。工作线程需要访问

表单上的datagridview。我使用以下代码生成工作线程...

Thread WorkerThread = new Thread(new ThreadStart(WT_MyFunction));

WorkerThread.IsBackground = true;

WorkerThread.Start();


我遇到的问题是......我似乎无法弄清楚如何传递

对象进入工作线程函数。例如,当我尝试使用以下代码时,请使用以下代码...


Thread WorkerThread =新线程(新的ThreadStart(WT_MyFunction(参考

dgvMyDataGridView)));

WorkerThread.IsBackground = true;

WorkerThread.Start();


。 ...我得到了编译时错误:预期的方法名称。


我做错了什么,我该怎么做才能做到这一点?

JP

I am using VS2005 for a windows forms application. I need to be able to
use a worker thread function to offload some processing from the UI
thread. The worker thread will need access to a datagridview on the
form. I am using the following code to spawn the worker thread...

Thread WorkerThread = new Thread(new ThreadStart(WT_MyFunction));
WorkerThread.IsBackground = true;
WorkerThread.Start();

The problem I am having is...I cannot seem to figure out how to pass
objects into the worker thread function. For example, when I try to
use the following code...

Thread WorkerThread = new Thread(new ThreadStart(WT_MyFunction(ref
dgvMyDataGridView)));
WorkerThread.IsBackground = true;
WorkerThread.Start();

....I get the compile time error: "Method name expected".

What am I doing wrong, and how can I make this work?

JP

推荐答案

OK; ThreadStart不接受参数;在2.0中有一个/ b
参数化的线程,但就bang-for-buck而言,实际上在匿名代表中捕获了
变量可以更方便,而且它是

类型安全:


线程workerThread =新线程((ThreadStart)委托{

SomeFunction(someValue); //你需要定义SomeFunction

});


这里是someValue (可能是你的网格视图)将被捕获。 (实际上

移入了一个单独的类实例);大括号内的所有内容都是

实际上是将在新线程上调用的外部函数,并且

* not *作为赋值给workerThread的一部分进行评估。注意

SomeFunction可以强类型来接受datagridview,而不是

对象(这是ParameterizedThreadStart所采用的)。


但是:这里更大的问题是线程亲和力。你说你需要访问

到datagridview - 坏消息。你可能做不了多少,因为你的b $ b将获得非法的线程异常,除非你在

线程之间进行大量切换(通过Control.Invoke / Control .BeginInvoke)。您可能需要在后台线程上执行

处理,然后将结果合并到UI线程上的单个

块中以保持性能。


Marc


< jo ********* @ topscene.comwrote in message

news:11 **********************@s13g2000cwa.googlegr oups.com ...
OK; ThreadStart does not accept parameters; in 2.0 there is
ParameterizedThreadStart, but in terms of bang-for-buck, actually captured
variables in an anonymous delegate can be more convenient, plus it is
type-safe:

Thread workerThread = new Thread((ThreadStart) delegate {
SomeFunction(someValue); // you need to define SomeFunction
});

Here "someValue" (which could be your gridview) will be "captured" (actually
moved into a seprate class instance); everything inside the braces is
actually the outer function that will be invoked on the new thread, and is
*not* evaluated as part of the assignment to workerThread. Note that
SomeFunction can be strongly typed to accept a datagridview, rather than an
object (which is what ParameterizedThreadStart takes).

However: a bigger problem here is thread affinity. You say you need access
to datagridview - well bad news. You probably can''t do much with it, as you
will get illegal thread exceptions unless you do a lot of switching between
threads (via Control.Invoke / Control.BeginInvoke). You may need to do the
processing on the background thread, and then merge the results in a single
block back on the UI thread to maintain performance.

Marc

<jo*********@topscene.comwrote in message
news:11**********************@s13g2000cwa.googlegr oups.com...

>我正在使用VS2005的Windows窗体应用程序。我需要能够使用工作线程函数来从UI中卸载一些处理工作。
线程。工作线程需要访问

表单上的datagridview。我使用以下代码生成工作线程...

Thread WorkerThread = new Thread(new ThreadStart(WT_MyFunction));

WorkerThread.IsBackground = true;

WorkerThread.Start();


我遇到的问题是......我似乎无法弄清楚如何传递

对象进入工作线程函数。例如,当我尝试使用以下代码时,请使用以下代码...


Thread WorkerThread =新线程(新的ThreadStart(WT_MyFunction(参考

dgvMyDataGridView)));

WorkerThread.IsBackground = true;

WorkerThread.Start();


。 ..我得到编译时错误:预期的方法名称。


我做错了什么,我怎样才能做到这一点?


JP
>I am using VS2005 for a windows forms application. I need to be able to
use a worker thread function to offload some processing from the UI
thread. The worker thread will need access to a datagridview on the
form. I am using the following code to spawn the worker thread...

Thread WorkerThread = new Thread(new ThreadStart(WT_MyFunction));
WorkerThread.IsBackground = true;
WorkerThread.Start();

The problem I am having is...I cannot seem to figure out how to pass
objects into the worker thread function. For example, when I try to
use the following code...

Thread WorkerThread = new Thread(new ThreadStart(WT_MyFunction(ref
dgvMyDataGridView)));
WorkerThread.IsBackground = true;
WorkerThread.Start();

...I get the compile time error: "Method name expected".

What am I doing wrong, and how can I make this work?

JP



你不应该从

创建的线程以外的任何东西访问控制数据控制。在VS2005中,我建议使用BackgroundWorker类

并让表单/控件订阅ProgressChanged事件(如果你是
没有进行只有原子操作的a返回)或

RunWorkerCompleted事件。此类确保在创建BackgroundWorker对象的线程

上调用ProgressChanged事件

处理程序和RunWorkerCompleted事件处理程序(这意味着您必须创建它

在创建控件的线程上,以使其正常工作)。

假设最简单的情况,你可以做任何你想要的控件/表格
$ RunWorkerCompleted事件处理程序中的b $ b。如果你的处理程序不是静态的,那么

this关键字可用于访问表单/控件及其属性和

,无需传递进入线程。


-
http://www.peterRitchie.com/blog/

Microsoft MVP,Visual Developer - Visual C#

" jo ***** ****@topscene.com"写道:
You should not access control data from anything other than the thread that
created the control. In VS2005, I suggest using the BackgroundWorker class
and have the form/control subscribe to the ProgressChanged event (if you''re
not doing an atomic operation that only has a "return") or the
RunWorkerCompleted event. This class ensures that the ProgressChanged event
handler and the RunWorkerCompleted event handler are called on the thread
that created the BackgroundWorker object (which means you have to create it
on the thread that created the control in order for it to work properly).
Assuming the simplest case, you can do whatever you want to the control/form
in the RunWorkerCompleted event handler. If your handler is not static the
"this" keyword can be used to access the form/control and it''s properties and
there is no need to "pass" it into the thread.

--
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
"jo*********@topscene.com" wrote:

我正在使用VS2005的Windows窗体应用程序。我需要能够使用工作线程函数来从UI中卸载一些处理工作。
线程。工作线程需要访问

表单上的datagridview。我使用以下代码生成工作线程...

Thread WorkerThread = new Thread(new ThreadStart(WT_MyFunction));

WorkerThread.IsBackground = true;

WorkerThread.Start();


我遇到的问题是......我似乎无法弄清楚如何传递

对象进入工作线程函数。例如,当我尝试使用以下代码时,请使用以下代码...


Thread WorkerThread =新线程(新的ThreadStart(WT_MyFunction(参考

dgvMyDataGridView)));

WorkerThread.IsBackground = true;

WorkerThread.Start();


。 ...我得到了编译时错误:预期的方法名称。


我做错了什么,我该怎么做才能做到这一点?

JP

I am using VS2005 for a windows forms application. I need to be able to
use a worker thread function to offload some processing from the UI
thread. The worker thread will need access to a datagridview on the
form. I am using the following code to spawn the worker thread...

Thread WorkerThread = new Thread(new ThreadStart(WT_MyFunction));
WorkerThread.IsBackground = true;
WorkerThread.Start();

The problem I am having is...I cannot seem to figure out how to pass
objects into the worker thread function. For example, when I try to
use the following code...

Thread WorkerThread = new Thread(new ThreadStart(WT_MyFunction(ref
dgvMyDataGridView)));
WorkerThread.IsBackground = true;
WorkerThread.Start();

....I get the compile time error: "Method name expected".

What am I doing wrong, and how can I make this work?

JP


Joey,

..NET 2.0提供ParameterizedThreadStart委托:

http:// msdn2.microsoft.com/en-us/lib...readstart.aspx


否则,关于UI等,Peter Ritchie的评论将适用。 />
彼得


-

联合创始人,Eggheadcafe.com开发者门户网站:
http://www.eggheadcafe.com

UnBlog:
http://petesbloggerama.blogspot.com


" jo ******* **@topscene.com"写道:
Joey,
..NET 2.0 offers the ParameterizedThreadStart delegate:

http://msdn2.microsoft.com/en-us/lib...readstart.aspx

Otherwise, regarding UI etc. Peter Ritchie''s comment would apply.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"jo*********@topscene.com" wrote:

我正在使用VS2005的Windows窗体应用程序。我需要能够使用工作线程函数来从UI中卸载一些处理工作。
线程。工作线程需要访问

表单上的datagridview。我使用以下代码生成工作线程...

Thread WorkerThread = new Thread(new ThreadStart(WT_MyFunction));

WorkerThread.IsBackground = true;

WorkerThread.Start();


我遇到的问题是......我似乎无法弄清楚如何传递

对象进入工作线程函数。例如,当我尝试使用以下代码时,请使用以下代码...


Thread WorkerThread =新线程(新的ThreadStart(WT_MyFunction(参考

dgvMyDataGridView)));

WorkerThread.IsBackground = true;

WorkerThread.Start();


。 ...我得到了编译时错误:预期的方法名称。


我做错了什么,我该怎么做才能做到这一点?

JP

I am using VS2005 for a windows forms application. I need to be able to
use a worker thread function to offload some processing from the UI
thread. The worker thread will need access to a datagridview on the
form. I am using the following code to spawn the worker thread...

Thread WorkerThread = new Thread(new ThreadStart(WT_MyFunction));
WorkerThread.IsBackground = true;
WorkerThread.Start();

The problem I am having is...I cannot seem to figure out how to pass
objects into the worker thread function. For example, when I try to
use the following code...

Thread WorkerThread = new Thread(new ThreadStart(WT_MyFunction(ref
dgvMyDataGridView)));
WorkerThread.IsBackground = true;
WorkerThread.Start();

....I get the compile time error: "Method name expected".

What am I doing wrong, and how can I make this work?

JP


这篇关于将对象传递给工作线程函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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