Client callback adding items to databound collection = crossthread error

S

Steve K.

I understand the concepts and rules for invoking UI methods from a non-UI
thread. In the past I have always checked with the control's InvokeRequired
property and Invoked my delegate accordingly. This has worked fine, nothing
special.

BUT, I am now working on my first client/server app and I have a slightly
new challenge that I'm not finding a good solution for.

My client passes an EventShim with a callback to the server. When something
special happens on the server it fires this event which the client of course
responds to.

The event handler on the client is taking args from the server-fired event
and adding items to a DataBound collection. I designed it this way on
purpose, I didn't want to tear down and build up the UI controls every time
a couple new items came over, data binding seemed a perfect fit.

Problem is that when the items are added to the collection and they raise
the INotifyChange event (I think that's the one) I'm getting cross-thread
exceptions. Not surprising. Now, since I'm not directly making calls to a
"UI" object my old habits of InvokeRequire/BeginInvoke on the control won't
work.

My question: How do I invoke a delegate on the UI thread without having
acccess to a System.Windows.Forms.Control object? How else can we execute
on the UI thread?

Thanks for any help, I'm close to getting this deliverable wrapped up and
going to sleep (just in time for breakfast!) :0)

-Steve
 
S

Steve K.

Wow, found a cool new class!
SynchronizationContext

I'll let the following code explain, it's easier and will make more sense.

<code>
private System.Threading.SynchronizationContext _uiContext = null;

public FaxService()
{
try
{
RemotingConfiguration.Configure("Shell.exe.config", false);

_faxManager = RemotingServices.Connect(typeof(IFaxManager),
ConfigurationManager.AppSettings["OfficeStudioServerURL"]) as
IFaxManager;

_newFaxesHandler = NewFaxesAvailableShim.Create(new
NewFaxesAvailable(OnNewFaxesAvailable));
_faxManager.NewFaxesAvailable += _newFaxesHandler;

// store a reference to the context before we register with
// the server (and potentially open this up to event callbacks)
_uiContext = System.Threading.SynchronizationContext.Current;

// Register with the server
_faxManager.RegisterClient(Environment.MachineName);
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
}

public void OnNewFaxesAvailable(string[] faxIDs)
{
FaxMessageCollection faxes = new FaxMessageCollection();

faxes.Query
.Select
(
faxes.Query.DateReceived
)
.Where
(
faxes.Query.MessageID.In(faxIDs)
);

if (faxes.Query.Load())
{
// This next part will effect the UI controls that are bound
// to this collection and as such we need to invoke the
// following code on the UI thread. We stored a
// SynchronizationContext earlier in the service's Ctor, us it
// now to finish this up.
try
{
_uiContext.Send(delegate
{
foreach (FaxMessage fax in faxes)
{
// attach the new fax to the collection
_faxes.AttachEntity(fax);
};
}, null);
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
</code>

Works like a charm, very easy!

:0)
 

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