Re: Client callback adding items to databound collection = crossthread error



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)




"Steve K." <noway@xxxxxxxxxxx> wrote in message
news:OugFS72UIHA.3556@xxxxxxxxxxxxxxxxxxxxxxx
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



.



Relevant Pages

  • Re: 2 onlick events?
    ... for the on click you will need to call the server side script from your ... the client, it's server based after all. ... > Which should invoke: ...
    (microsoft.public.dotnet.framework.aspnet.buildingcontrols)
  • Re: 2 onlick events?
    ... for the on click you will need to call the server side script from your ... the client, it's server based after all. ... > Which should invoke: ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: 2 onlick events?
    ... for the on click you will need to call the server side script from your ... the client, it's server based after all. ... > Which should invoke: ...
    (microsoft.public.dotnet.general)
  • Http error 413->Request entity too large
    ... I have a webservice under IIS6/Win2003 Server. ... When I try to invoke a method ... I mention that this dataset is very large and if the client ... I checked the settings in machine.config file and the atributte for ...
    (microsoft.public.dotnet.framework.remoting)
  • Http error 413-> Request entity too large
    ... I have a webservice under IIS6/Win2003 Server. ... When I try to invoke a method ... I mention that this dataset is very large and if the client ... I checked the settings in machine.config file and the atributte for ...
    (microsoft.public.dotnet.framework.adonet)