Making Async Popups in WSAs

The async and await keywords are awesome and allow for a much easier style of async programming over the traditional callback approach. Ideally, we could use these everywhere in our programming - particularly for UI environments such as Window Store apps. But not all the APIs are programmed this way in WinRT - take for example the Popup control. You show the Popup by setting the IsOpen property to "true" and then close it by resetting it to "false". This is a holdover from the WPF/SL world … [Read more...]

Mixing asynchronous and synchronous code

I had a question from a former student today related to a deadlock he was encountering in his Windows Store application. He had an interface which was already in use (and therefore could not be changed) which had to call a method that performs I/O. In WSAs (Windows Store Apps) all I/O is now asynchronous - so he used the async/await keyword which is the normal approach here. The problem was that he had to block waiting on the results due to the interface design. Here's a similar block of code to … [Read more...]

Moving from synchronous APIs to async APIs

One of the main features added to .NET 4.5 is a whole new set of asynchronous APIs that mirror the current synchronous versions.  These were added primarily for Windows Store Apps (where they actually replace the existing APIs) but also to improve the responsiveness of UI applications with the advent of the async/await keywords in C# 5. The .NET Bio project has a whole set of APIs to hit common bioinformatic web services such as BLAST which searches various databases trying to find matching … [Read more...]

Adding to an ObservableCollection from a background thread

One annoying thing about ObservableCollection<T> is that it doesn't support modifications from background threads.  That is to say, the CollectionChanged notification doesn't marshal to the proper thread when it is raised and it causes the exception "This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread".  I ran into this problem a while back and searched out to see if someone else had solved it. I … [Read more...]

When .NET 1.1 threads are faster than .NET 3.5!

Today at the GNET Michael Kennedy, Jason Whittington and I discovered a pretty interesting change in .NET 2.0 SP1 (included in .NET 3.5). We knew they increased the thread pool limit from 25 threads/cpu to 250 threads/cpu, but we also found they changed the heuristics for creating threads to avoid creating a bunch of threads in a short period of time. In previous versions of .NET, threads were allocated at a rate of 1 every 500ms assuming there were queued items. This is documented behavior … [Read more...]

Adding Pause/Resume into SynchronizationContext based components

About a year ago, I blogged on using the .NET 2.0 facility SynchronizationContext to create thread-aware components which could be hosted and would "do the right thing" for callbacks. Here's the Link to that post. Recently, I got an email from a reader who wanted to use that paradigm, but wanted to be able to suspend the operation for some period of time and then resume it later on. He asked if there was an easy way to modify the sample I presented, and it turns out that there is. It … [Read more...]

The Mystery of Concurrent GC

There's been a discussion going on within DevelopMentor for a couple weeks regarding concurrent GC and when it really applies. The idea behind the concurrent collector is to do as much of the GC while the UI thread continues to process UI stuff and then only interrupt the application threads when memory is being shuffled around and fixups are occurring. This provides for more responsive UI applications at the expense of slower collections and a higher memory utilization. The instructors who … [Read more...]

Creating synchronization sensitive components

One issue that's always a struggle with building reusable components is managing asynchronous operations.  The problem is that depending on the type of application that is going to use the component, the thread used for callbacks and events may or may not be important.  For example, with Console based applications, callbacks on different threads might be ok - at least as long as the application itself ensures thread safety.  But, with a Windows Forms application, threading is critical - you are … [Read more...]