Multithreading in Windows Store applications written with .NET and C#: get to know agile classes
Posted: (EET/GMT+2)
If you are developing Windows Store applications with C# and .NET, you are probably already familiar with asynchronous tasks and running things, like web resource access, in the background without interrupting the user interface.
In .NET programming, many classes can be accessed from multiple threads of different tasks, with the exception of user interface controls, which usually cannot be accessed from another threads than the one that was used to create the control.
In Windows Store applications, that is, in Windows Runtime based applications, the rule of thumb is largely the same. You can access many Windows Runtime APIs from different threads and background tasks, but not all.
In Windows Runtime programming, classes are said to be agile if they can be accessed from multiple threads or tasks. An agile class is, however, not necessary thread-safe. But unless a class is agile, you cannot call it from another thread: if you do, you will get an InvalidCastException exception.
How do you know that a Windows Runtime class is agile or not? This is pretty straightforward: if a class implements the IAgileObject interface, or specifies the class-wide attribute MarshalingBehaviorAttribute with the value of MarshalingType.Agile.
Furthermore on MSDN documentation, the class attribute section mentions this agile attribute, if it exists.
Happy multithreading!