Paul Cowan <[EMAIL PROTECTED]> wrote: > Hi, > > I am trying to handle a piece of MSMQ asynchronous from a web application. > I have the following piece of code that is in the code behind of a user > control in an eventhandler > > ReloadCart(); > OrderCalculatedHandler resultHandler = new > OrderCalculatedHandler(ResultHandler); > _msmqAdapter.CalculateOrder(_cart, resultHandler);
This CalcualteOrder method - why doesn't it return an IAsyncResult that you can block on, on its WaitHandle? > Profile.ShoppingCart = _cart; > RetrieveOrderFromProfile(); > > The line _msmqAdapter.CalculateOrder(_cart, resultHandler); is the line that > calls the asynchronous method. resultHandler is function pointer to the > following piece of code: > > private void ResultHandler(object sender, MSMQEventArguments<ShoppingCart> e) > { > _cart = e.Result; > Profile.ShoppingCart = _cart; > } > > The problem being that the method takes a long time to run and the page > finishes processing before an event is raised back to the page. > > What can I do to tell the page to wait for the ResultHandler method to > execute? If you absolutely have to work within the constraints you've described, then one way would be to create an event and pass it to the handler, and block on the event - either in your event code above, or later in the page processing cycle. For example: ---8<--- > ReloadCart(); using (ManualResetEvent done = new ManualResetEvent(false)) { _msmqAdapter.CalculateOrder(_cart, delegate(object sender, MSMQEventArguments<ShoppingCart> e) { ResultHandler(sender, e); done.Set(); } done.WaitOne(); } > Profile.ShoppingCart = _cart; // !!! The above line of code seems to be the same in both the // async callback and in your main thread code??? // That's a race, between the callback and this thread; you // could lose the value the callback returns. > RetrieveOrderFromProfile(); --->8--- The above code basically makes the asynchronous method synchronous, and is wasteful etc. Ideally you'd block on the 'done' event as late as possible in the page processing cycle. Also, you'll want to fix that race bug. -- Barry -- http://barrkel.blogspot.com/ =================================== This list is hosted by DevelopMentorĀ® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com