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# / January 2008

Tip: Looking for answers? Try searching our database.

Client callback adding items to databound collection = crossthread error

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Steve K. - 10 Jan 2008 09:54 GMT
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
Steve K. - 10 Jan 2008 10:22 GMT
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)

>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
[quoted text clipped - 27 lines]
>
> -Steve

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.