Marshal data from thread, use...SynchronizationContext?

D

_DD

Existing problem: A function that runs on a thread needs to return
data to the UI thread. When the data is returned, that will trigger
secondary tasks, including access to form controls and lookup of data
within a database.

I'm not sure of the implications of using data returned by the thread
within database access code, but I do know about InvokeRequired() etc
for access to forms controls. I've used that often when individual
controls are the focal point. Not so here.

In this case, I'd like to simplify things by marshaling ALL data
coming back from the thread. Then it could be used by controls or
other functions with no worries. So the thread would return data
through a 'channel object' that takes care of all marshaling in one
place.

As for the mechanics involved:

InvokeRequired() is associated with a specific control, so that is not
relevant.

BackgroundWorker seems like it might be applicable, but I haven't seen
it used in the context, maybe for good reason.

SynchronizationContext seems like a good clue but I have not been able
to find enough info on actual usage to be sure that this is the
simplest approach.

Thanks for any perspective you can provide.
 
N

Nick Hounsome

InvokeRequired is NOT associated with a single control.
It is associated with the UI thread that created the control.
In a typical application that will be the ONLY UI thread.

To keep it as general as possible pass the ISychronizeInvoke interface to
your work thread rather than an actual control.

SynchronizationContext is just a property holding an ISynchronizeInvoke for
exactly this purpose.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top