Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

invoking the execution of a function on another thread

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jon - 18 Mar 2008 21:35 GMT
Hi,

"THREAD_UI" = thread under which the UI controls were created.
"THREAD_XX" = another thread.
"FUNC_UI"   = function that needs to run on THREAD_UI.

From THREAD_XX I need to start the execution of FUNC_UI, that needs to
run on THREAD_UI. Inside FUNC_UI, I will modify many controls present
on the form. Since I don't want to use Invoke() or BeginInvoke() for
each of those calls, this is why I thought it would be more efficient
if FUNC_UI run on THREAD_UI.

How, from THREAD_XX, can I invoke FUNC_UI to run on THREAD_UI ?

FUNC_UI is not tied to any single control. Invoke() and BeginInvoke()
are member methods of one control. Is there an elegant way to invoke
FUNC_UI without having to refer to a specific control?

Thank you very much,
Jon
Marc Gravell - 18 Mar 2008 21:42 GMT
One option is to pass an ISynchronizeInvoke into your code; any handy
Control will satisfy this, and it abstracts the way of calling Invoke
etc.

The other approach is to use the ambient sync-context; the running
form will set itself up as the sync-context:

           SynchronizationContext ctx =
SynchronizationContext.Current;
           if (ctx == null)
           { // no sync-context: execute directly
               Foo();
           }
           else
           { // ask the sync-context to execute it for us
               ctx.Send(delegate { Foo(); }, null);
           }

Marc

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.