又一个线程/调用问题...... [英] Yet another threading/invoking question...

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

问题描述




我目前正在重写一些使用

multithredaing从数据库中检索数据集并更新网格的功能

控制。我发现网格(Infragistics UltraGrid,虽然它没有b $ b无关紧要)被更新(即grid.DataSource = dsDataSet;)和

用于不同目的工作线程,所以现在我要包装

所有那些调用grid'的方法通过调用这是一个痛苦的

a $$考虑到不同的数量方法UltraGrid有。所以我想要

来看看我现在是否可以留下一些这样的电话。


所以我的问题是:它真的真的......我的意思是真的不好打电话:


对象o = mygridcontrol.Tag;


来自工人线程,即使我是100%确定这个mygridcontrol在我打这个电话的同时,不会在UI线程中使用
吗?


考虑这个样本:


// UI(主)线程中的某个地方:

mygridcontrol = new UltraGrid();

线程t =新线程(新的ThreadStarter(UpdateFuncDelegate) ));

....之后没有代码...

//在wroker线程中 - 函数UpdateFunc


object o = mygridcontrol.Tag;


//////////////////////////

我知道这真糟糕糟糕!但是仍然......


请不要告诉我我不应该这样做,我知道。

告诉我 - 如果控制从工作线程调用它的方法时不使用

- 是否会导致错误?

如果是,为什么?

谢谢!

MuZZy

Hi,

I''m currently rewriting some functionality which was using
multithredaing for retrieving datasets from database and updating a grid
control. I found that the grids (Infragistics UltraGrid, though it
doesn''t matter) were updated (i.e. grid.DataSource = dsDataSet;) and
used for different purposes in the worker thread, so now i''m wrapping
all those calls to grid''s methods thru invoking which is a pain in the
a$$ considering number of different methods UltraGrid has. So i wanted
to see if for now i could leave some of those calls as is.

So my question is: is it really-really... and i mean REALLY bad to call:

object o = mygridcontrol.Tag;

from a worker thread even if i am 100% sure this "mygridcontrol" is not
going to be used in the UI thread at the same time as i make this call?

Consider this sample:

// Somewhere in the UI (main) thread:
mygridcontrol = new UltraGrid();
Thread t = new Thread(new ThreadStarter(UpdateFuncDelegate));
.... and no code after that...
// In wroker thread - in function UpdateFunc

object o = mygridcontrol.Tag;

//////////////////////////

I know it''s really bad bad bad!!! But still...

Please don''t tell me i shouldn''t do that, i know that.
Just tell me - if control is not used at time when its method is called
from a worker thread - is it going to lead to an error or not?
And if yes, why?
Thank you!
MuZZy

推荐答案

考虑UltraGrid的不同方法的数量。所以我想要

来看看我现在是否可以留下一些这样的电话。


所以我的问题是:它真的真的......我的意思是真的不好打电话:


对象o = mygridcontrol.Tag;


来自工人线程,即使我是100%确定这个mygridcontrol在我打这个电话的同时,不会在UI线程中使用
吗?


考虑这个样本:


// UI(主)线程中的某个地方:

mygridcontrol = new UltraGrid();

线程t =新线程(新的ThreadStarter(UpdateFuncDelegate) ));

....之后没有代码...

//在wroker线程中 - 函数UpdateFunc


object o = mygridcontrol.Tag;


//////////////////////////

我知道这真糟糕糟糕!但是仍然......


请不要告诉我我不应该这样做,我知道。

告诉我 - 如果控制从工作线程调用它的方法时不使用

- 是否会导致错误?

如果是,为什么?

谢谢!

MuZZy
considering number of different methods UltraGrid has. So i wanted
to see if for now i could leave some of those calls as is.

So my question is: is it really-really... and i mean REALLY bad to call:

object o = mygridcontrol.Tag;

from a worker thread even if i am 100% sure this "mygridcontrol" is not
going to be used in the UI thread at the same time as i make this call?

Consider this sample:

// Somewhere in the UI (main) thread:
mygridcontrol = new UltraGrid();
Thread t = new Thread(new ThreadStarter(UpdateFuncDelegate));
.... and no code after that...
// In wroker thread - in function UpdateFunc

object o = mygridcontrol.Tag;

//////////////////////////

I know it''s really bad bad bad!!! But still...

Please don''t tell me i shouldn''t do that, i know that.
Just tell me - if control is not used at time when its method is called
from a worker thread - is it going to lead to an error or not?
And if yes, why?
Thank you!
MuZZy


MuZZy< tn *@newsgroups.nospam>写道:


< snip>
MuZZy <tn*@newsgroups.nospam> wrote:

<snip>
我知道这真糟糕坏了!但是还是......

请不要告诉我,我不应该这样做,我知道。
告诉我 - 如果控制时间没有使用它的方法从工人线程中调用
- 它是否会导致错误?
如果是,为什么?
I know it''s really bad bad bad!!! But still...

Please don''t tell me i shouldn''t do that, i know that.
Just tell me - if control is not used at time when its method is called
from a worker thread - is it going to lead to an error or not?
And if yes, why?




我不会我想确定是否会导致错误。我已经认为你知道你不应该这样做的事实就足够了

让你不这样做。毕竟,即使它在测试中有效,

你也很容易在客户盒子上出现问题,到那时候,它太晚了。 。


-

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

如果回复该群组,请不要给我发邮件



I wouldn''t like to say for sure whether it will lead to an error. I''d
have thought the fact that you know you shouldn''t do it would be enough
to make you not do it, however. After all, even if it works in test,
you could easily get an issue on a customer box, by which time it''s too
late to do it properly.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


>从工人线程即使我100%确定这个mygridcontrol是不是
> from a worker thread even if i am 100% sure this "mygridcontrol" is not
将在我进行此调用的同时在UI线程中使用?
请不要告诉我,我不应该这样做,我知道。
请告诉我 - 如果在工人调用方法时没有使用控件
线程 - 是否会导致错误?
如果是,为什么?


[在我看来]:我认为它不仅仅是你的代码。您可以检查并确保
保证在您的工作线程更新时不会触摸网格。它是
可能是windows框架的代码访问你的网格,而不是你想要的b
,如绘画网格。我不确定它真的发生了什么

我说的关于通过框架绘制网格,但我确定这样的事情会发生这样的事情,这就是为什么人们他们访问那里时遇到问题UI

来自除了拥有线程之外的其他控件有时该代码

有效且有时有效它没有。但那就是在竞争条件下发生的事情,有时他们会出现,有时他们不会。那么为什么不遵循离散的

规则并通过你现在正在做的调用工作来帮自己一个忙。它将来会对你有利。正如乔恩所说,你的代码可能不会在客户的盒子上工作。想象一下会发生什么。

所有那些调用网格方法通过调用这是一个痛苦的
a
going to be used in the UI thread at the same time as i make this call? Please don''t tell me i shouldn''t do that, i know that.
Just tell me - if control is not used at time when its method is called
from a worker thread - is it going to lead to an error or not?
And if yes, why?
[in my opinion]: I think its not only "your code" that you can check and
guarantee wont touch the grid while your worker thread is updating it. It
could be windows framework''s code accessing your grid at a time you dont
anticipate, like painting the grid. I''m not sure that it really happens what
I said about painting grid by the framework, but I''m sure things like this
happens and thats why people have problems while they access there UI
controls from other than there owning thread cuz "some times" that code
works and "sometimes" it doesnt. But thats what happen in race conditions,
sometimes they arise and sometimes they dont. So why not follow the discrete
rules and do yourself a favor by doing the invoking work that u r doing
right now. It''ll benifit you in the future. As Jon said, your code may not
work on your customer''s box. Imagine what will happen than.
all those calls to grid''s methods thru invoking which is a pain in the
a


这篇关于又一个线程/调用问题......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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