Thursday, January 27, 2011

C#, Windows mobile 5.0, delegate and multithreading

This week was the 3rd week of school and it was also the week i started to code the 3rd year project! The project goal is in fact to add internet multiplayer functionality to an already existing racing game, after spending 2 weeks refining the software requirements and filling papers and papers of documentation, we finally got to the interesting part, the part where we actually learn something.

So I started the week by beginning the development of a chat interface which will run on a Windows mobile 5.0 PDA (old stuff dating from over 3 years ago). The interface part was pretty easy to do, Winforms is great for that, you just have to drag' n drop to create the layout and then you can start programming. I got all the layout working within 1 or 2 hours but then i wondered what was the best way to keep the information between the window, and also create the communication to the server. After talking to a collegue, i decided to simply create a "Session" object which will control the displayed page as well as the messaging service, I don't know if that was the best way to do it but i'm open to comments, i'm at school to learn.

So after that i started to think of the best way to receive messages from the server, i decided to go with starting a thread which would wait for the input from the server and then would go on notifying the interface to update itself. But to do that i needed to know 2 things: what is a delegate and how to start a thread! After 1-2 hours of reading tutorials, i got the hang of delegates and decided to continue on with the implementation. I created the thread which ran on an instance of the messaging object (yeah I know, that is probably not the best way to do it) and don't worry it didn't access any data, it was actually just waiting for an answer from the server. The real problem was calling the delegate to afterward, since it resided on the main thread, the function couldn't be accessed except by quitting the thread, so i had to learn to use the .Invoke function (which blocks the incoming thread). But finally, I was able to make the 2nd thread totally separate from the first one. Now the next step is to actually communicate with the server.

No comments:

Post a Comment