异步主线程方法执行 [英] Async main-thread method execution
问题描述
是否可以从工作线程异步调用方法,以便
此方法将在主线程中执行?我正在做一些工作的工作
线程,并希望向主线程报告进度以更新一些
控件。但是我想异步这样做,这样工作线程就不会阻塞,直到主线程完成更新并刷新控件,这需要花费一些时间。使用invoke是因为它阻止了工作线程,直到
方法结束。
问候
Tomaz
使用ThreadPool执行您需要的任何内容,例如
System.Threading.ThreadPool.QueueUserWorkItem(dele gate {OnEvent(X);});
" Tomaz Koritnik" < no **** @ nospam.comha scritto nel messaggio
新闻:aG ****************** @ news.siol.net。 ..
是否可以从工作线程异步调用方法,以便
此方法将在主线程中执行?我正在做一些工作的工作
线程,并希望向主线程报告进度以更新一些
控件。但我想异步这样做,以便工作线程
不会阻塞,直到主线程完成更新并刷新
控件需要一些时间。使用invoke是因为它阻止了工作程序
线程直到方法完成。
问候
Tomaz
" Tomaz Koritnik" < no **** @ nospam.comwrote in message
新闻:aG ****************** @ news.siol.net .. 。
是否可以从工作线程异步调用方法,以便此方法
将在主线程中执行?我正在工作线程中做一些工作,并希望
向主线程报告进度以更新一些控件。但是我想异步地这样做
,这样工作线程就不会阻塞,直到主线程完成更新
和刷新控件需要一些时间。使用invoke是因为它阻止了工作程序
线程直到方法完成。
问候
Tomaz
使用BeginInvoke而不是Invoke!
Willy。
To Laura T.还......
你不明白我的问题。我已经在
后台线程中执行了一些工作,但是我想在主线程中执行一个方法。
Myexample:worker thread有一些数据有要显示。我不能从这个线程更新控件
,所以我必须以某种方式将这些数据传递给
主线程。而且我不想这样做异步,所以工作线程继续工作
,同时更新控件。在Win32中,完美的解决方案是发送
消息。现在就明白我的观点吧?
问候
Tomaz
" Willy Denoyette [MVP]" < wi ************* @ telenet.bewrote in message
新闻:OW ************* @ TK2MSFTNGP04.phx .gbl ...
" Tomaz Koritnik" < no **** @ nospam.comwrote in message
新闻:aG ****************** @ news.siol.net .. 。
>是否可以从工作线程异步调用方法,以便这个方法在主线程中执行?我正在做一些工作
线程的工作,并希望向主线程报告进度以更新一些
控件。但我想异步这样做,以便工作线程不会阻塞,直到主线程完成更新和刷新控件需要一些时间。使用invoke是因为它会阻止worker
线程,直到方法完成。
Tomaz
使用BeginInvoke代替Invoke!
Willy。
Is it possible to asynchronously call a method from worker thread so that
this method would execute in main-thread? I''m doing some work in worker
thread and would like to report progress to main thread to update some
controls. But I''d like to do that asynchronously so that worker thread would
not block until main thread finished updating and refreshing controls which
take some time. Using invoke is out since it blocks worker thread until
method finishes.
regards
Tomaz
Use ThreadPool to execute whatever you need, like
System.Threading.ThreadPool.QueueUserWorkItem(dele gate { OnEvent(X); });
"Tomaz Koritnik" <no****@nospam.comha scritto nel messaggio
news:aG******************@news.siol.net...Is it possible to asynchronously call a method from worker thread so that
this method would execute in main-thread? I''m doing some work in worker
thread and would like to report progress to main thread to update some
controls. But I''d like to do that asynchronously so that worker thread
would not block until main thread finished updating and refreshing
controls which take some time. Using invoke is out since it blocks worker
thread until method finishes.
regards
Tomaz
"Tomaz Koritnik" <no****@nospam.comwrote in message
news:aG******************@news.siol.net...Is it possible to asynchronously call a method from worker thread so that this method
would execute in main-thread? I''m doing some work in worker thread and would like to
report progress to main thread to update some controls. But I''d like to do that
asynchronously so that worker thread would not block until main thread finished updating
and refreshing controls which take some time. Using invoke is out since it blocks worker
thread until method finishes.
regards
Tomaz
Use BeginInvoke instead of Invoke!
Willy.
To Laura T. also...
You didn''t understand my question. I''m already executing some work in a
background thread but I would like to execute a method in main-thread.
Myexample: worker thread has some data which has to be displayed. I must not
update controls from this thread, so I must somehow pass this data to
main-thread. And I wan''t to do this async so worker thread continues working
while controls are updated. In Win32 the perfect solution to this is to send
message. Get my point now?
regards
Tomaz
"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:OW*************@TK2MSFTNGP04.phx.gbl..."Tomaz Koritnik" <no****@nospam.comwrote in message
news:aG******************@news.siol.net...>Is it possible to asynchronously call a method from worker thread so that
this method would execute in main-thread? I''m doing some work in worker
thread and would like to report progress to main thread to update some
controls. But I''d like to do that asynchronously so that worker thread
would not block until main thread finished updating and refreshing
controls which take some time. Using invoke is out since it blocks worker
thread until method finishes.
regards
Tomaz
Use BeginInvoke instead of Invoke!
Willy.
这篇关于异步主线程方法执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!