-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|677|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
SoCoder -> Article Home -> Advanced Techniques


 
Afr0
Created : 18 February 2012
Edited : 11 September 2016
System : Cross Platform
Language : C/C++

Concurrency in C#

Why concurrency in C# is awesome!

So lately I've been working on a patchclient in C#, and I thought I'd show why concurrency in C# is so deeply embedded into the language and how it can be used in client applications as well as servers.
If you haven't heard about callback functions before, a callback function is a function that takes a return value from another function and does something with it.
So instead of returning a value to the callee, functions that uses callback functions return their value to another function instead.
In C#, this allows for highly advanced asynchronous programming with a not-so-steep learning curve, because callback functions are automatically handed over to a new (waiting) thread in the internal threadpool when a function returns its value.
The only drawback is that you can't easily define your own functions that return their values into callback functions (or at least I never learned how to do that!)

Let's start with an example. When my patch client starts, it calls GetManifestAsync(), which looks like;



The important thing to note about this code is the call to BeginGetResponse(). It gets a response from a webserver on a separate thread, and then returns its value (in this case a WebRequest instance which can be used to conclude the request) into GotInitialResponse(), which is launched in yet another thread.

As you can see, GotInitialResponse basically fills out a RequestState and starts reading the actual response from the web server asynchronously.

A RequestState is used to transfer data between two callbacks, and looks as such:



When ReadCallback() has finished downloading a file, it will call OnFinishedFile(), which will determine whether the file downloaded was a manifest or a regular file. Then it will call the appropriate delegate.

A delegate, according to MSDN, is:

"… a type that represents references to methods with a particular parameter list and return type. When you instantiate a delegate, you can associate its instance with any method with a compatible signature and return type. You can invoke (or call) the method through the delegate instance."

In laymen's terms, a delegate is an event that can be exposed by a class and then subscribed to by other classes. These other classes then determine what happens when the event is called.

Here is how I'm using the above classes to fetch updates from Dropbox:



Hopefully this article has shown you how concurrency is deeply ingrained into C#, and how you can use it to your advantage.
Any questions can be posted below or mailed to afr0@afr0games.com

 

Comments


Sunday, 19 February 2012, 01:56
Afr0
Done!
Sunday, 11 September 2016, 12:28
Afr0
The code was really old and had one (or more?) bugs, which was really annoying me. So I updated it!