MULTIPLAYER: THREADS

Runing the demo of the previous lesson you have noted that, when the program is "reading" a webservice the avatar stops.

To avoid this problem we need to use threads.

We will have a "main program" having the "main loop" and some code running in paralel.

We need to define some new variables, including:

...
Thread thread;
ThreadStart job;
...

At the "main loop" we will interrupt the "main program" some time using:

...
Thread.Sleep(5);
...

At the OnLoaded:

...
 job = new ThreadStart(ThreadJob);
 thread = new Thread(job);
 thread.Start(); 
...

And the paralel code will do the webservices - it's like an "auxiliary loop":

...
public void ThreadJob(){
 while(op==0||op==1){ //a constant loop
  if(init1==1){ 
  //for the avatares
   if(avatar=="A"){
    sDB.UpdAvatar1A(m11,m12,m13,m14,m21,m22,m23,m24,m31,m32,m33,m34, m41,m42,m43,m44);  
    dsgen = sDB.SelAvatar1();
  }
  if(avatar=="B"){
   sDB.UpdAvatar1B(m11,m12,m13,m14,m21,m22,m23,m24,m31,m32,m33,m34, m41,m42,m43,m44); 
   dsgen = sDB.SelAvatar1();
  }
  //for the door
   if(sDB.Door001Sit()=="T" && op==0){
    door="open";
    op=1;
   }
   if(sDB.Door001Sit()=="F"&& op==1){
    door="close"; 
    op=0;
   }
  }
 }  
}
...

WARNING: Don't use objects defined using .XAML inside a paralel thread. (Can be a bug...)

You need to abort the paralel thread when the user close the application (defining Closing="OnClosing" at the .XAML file) or it remains running!

...
void OnClosing (object sender,  EventArgs e){
 thread.Abort();
}
...

Better that you do the download, run the application, analise the code, try to change something etc.

Create a folder having the name of the .ZIP file and download it - right-clicking the link - and unzip inside the folder :

tu18vt.zip

TIP:After the compilation using the Visual C# Express, run Tu18vt.exe 3 times. But "acquire" an avatar before to run another copy. Wait 30 seconds to start the new copy. Move the avatares, open the door, wait the syncronization...

NOTES: 1 - You need to liberate the firewall for the access to our webhoster, for this application. 2 - Starting the application, wait some seconds for the comunication to the server. 3 - If you close the application having the avatar A (or B) it will go out of the other copy... 4 - REMEMBER THAT MANY READERS OF THIS TUTORIAL CAN BE TESTING THE APPLICATION AT HE SAME TIME AND YOU CAN HAVE SURPRISED STATUS!!!! IT'S A MULTIPLAYER REALTIME DEMO !!!!!

If you like only to see the application running you don't need to do the above download. Only liberate the firewall and play this web3d application (wait the contact with the server ( at 999 that doesn't appear ), liberate the firewall etc. Wait a little more to choose an avatar.). Click this button again to create another copy of the application.Close/open the door at a copy, move the avatar and at the new synchronization they will change their status at the other copy.:

We are doing our tests at a: Notebook DELL Latitude D600 - Intel Pentium M755 2.0 GHZ - Memory: 512 MB - VideoCard ATI Radeon 9000 32MB DDR 4xAGP - Resolution 1400x1050 - Communication: ADSL 256 kbps (Download: 45 kBps, Upload: 14 kBps)


PREVIOUS LESSON NEXT LESSON
T.CONTENTS HOMEPAGE