Passive/Active listening server
-
I have a question. Right now i have made a server. It works just fine but there is a problem. I need to push a button in order to recieve data. I would like suggestions (and examples) how i can create a server wich regularly (always?) listens if data is sendt. Can anyone help me with this?
-
I have a question. Right now i have made a server. It works just fine but there is a problem. I need to push a button in order to recieve data. I would like suggestions (and examples) how i can create a server wich regularly (always?) listens if data is sendt. Can anyone help me with this?
In generally you solve this problem by using loops. Make a button "Start listening" which sets a bool variable, lets call it KeepListening, to true and call the listenfunction which contains a loop like while (KeepListening) { // Put Receive-Code in here... System.Threading.Thread.Sleep(10); } Now the second button sets the variable KeepListening to false. If you click it, the application will leave the loop. If you want to make your application listen to incoming traffic and also being available for user inputs, you will have to use threads (create a listening thread). There are nice threading-tutorials out there if you aren't familiar with threading at all. I'm too lazy/busy to search for a nice explanation but i will do if you are not successful. Lets sum it up: - You will have to use a Loop to listen all the time. - You will have to make use of the multithreading if your application should be available for other tasks while listening. Good luck, mik :)
-
In generally you solve this problem by using loops. Make a button "Start listening" which sets a bool variable, lets call it KeepListening, to true and call the listenfunction which contains a loop like while (KeepListening) { // Put Receive-Code in here... System.Threading.Thread.Sleep(10); } Now the second button sets the variable KeepListening to false. If you click it, the application will leave the loop. If you want to make your application listen to incoming traffic and also being available for user inputs, you will have to use threads (create a listening thread). There are nice threading-tutorials out there if you aren't familiar with threading at all. I'm too lazy/busy to search for a nice explanation but i will do if you are not successful. Lets sum it up: - You will have to use a Loop to listen all the time. - You will have to make use of the multithreading if your application should be available for other tasks while listening. Good luck, mik :)
Yo Mik, Tnx a lot for you're answer. I have read some tuts on threading but they are not really clear to me. If u have time and u know one please tell me :). I'm not in a big hurry so ...... Again tnx.
-
Yo Mik, Tnx a lot for you're answer. I have read some tuts on threading but they are not really clear to me. If u have time and u know one please tell me :). I'm not in a big hurry so ...... Again tnx.
okay, lets create a simple listening function and the eventhandlers for those buttons :)
// This variable indicates whether the listen method should keep listening or not ;) bool KeepListening = false; // This is the eventhandler for the "Start Listening" button. public void button_StartListen_click(object sender, EventArgs e) { KeepListening = true; System.Threading.Thread myListenThread = new System.Threading.Tread(new System.Threading.ThreadStart(Listen)); myListenThread.Start(); } // This is the eventhandler for the "Stop Listening" button. public void button_StopListen_click(object sender, EventArgs e) { KeepListening = false; } // This is the Listen method. private void Listen() { while (KeepListening) { // Put your "receiving code" in here... System.Threading.Thread.Sleep(10); } }
Okay, thats how it could look like now lets see what exactly happens here. If you click on the "Start Listening" Button, the method button_StartListen_Click is called and: - Sets KeepListening to 'true' - Initiates a new object of the Thread Class and pass one argument in its constructor. - Starts the new Thread. Now the thread is running in the background. The threads task is to call the method "Listen" with no arguments. When doing so the method won't exit until you click the "Stop Listening" button because of the loop. If you click the "Stop Listening" button now the button_StopListen_Click method will: - Sets KeepListening to 'false' Setting the KeepListening variable to false will cause the loop condition (while (KeepListening)) to be not forfilled. This makes the method exit and the thread will do so too! This is no explanation about threading since this is a very complex subject. You should start playing around with them because multithreading is what you want your application to do ;D However, hope you understood what i was talking about - if the code example does not work i will start my ide and create a working example ;) -
okay, lets create a simple listening function and the eventhandlers for those buttons :)
// This variable indicates whether the listen method should keep listening or not ;) bool KeepListening = false; // This is the eventhandler for the "Start Listening" button. public void button_StartListen_click(object sender, EventArgs e) { KeepListening = true; System.Threading.Thread myListenThread = new System.Threading.Tread(new System.Threading.ThreadStart(Listen)); myListenThread.Start(); } // This is the eventhandler for the "Stop Listening" button. public void button_StopListen_click(object sender, EventArgs e) { KeepListening = false; } // This is the Listen method. private void Listen() { while (KeepListening) { // Put your "receiving code" in here... System.Threading.Thread.Sleep(10); } }
Okay, thats how it could look like now lets see what exactly happens here. If you click on the "Start Listening" Button, the method button_StartListen_Click is called and: - Sets KeepListening to 'true' - Initiates a new object of the Thread Class and pass one argument in its constructor. - Starts the new Thread. Now the thread is running in the background. The threads task is to call the method "Listen" with no arguments. When doing so the method won't exit until you click the "Stop Listening" button because of the loop. If you click the "Stop Listening" button now the button_StopListen_Click method will: - Sets KeepListening to 'false' Setting the KeepListening variable to false will cause the loop condition (while (KeepListening)) to be not forfilled. This makes the method exit and the thread will do so too! This is no explanation about threading since this is a very complex subject. You should start playing around with them because multithreading is what you want your application to do ;D However, hope you understood what i was talking about - if the code example does not work i will start my ide and create a working example ;)Yo Mik :-D, Its working perfect the only small problem i got left is when i want to add the recieved stuff to a listbox i get the following error : Additional information: Cross-thread operation not valid: Control 'lbReceived' accessed from a thread other than the thread it was created on. Any idea how i can fix this ? Greetings , Jacco
-
Yo Mik :-D, Its working perfect the only small problem i got left is when i want to add the recieved stuff to a listbox i get the following error : Additional information: Cross-thread operation not valid: Control 'lbReceived' accessed from a thread other than the thread it was created on. Any idea how i can fix this ? Greetings , Jacco
hehe this is where it gets complicated. i haven't done much with threads at all but i also came across this problem when writing a "popup" class. as you may have noticed you were starting another thread for the listening method. the thread where all the controls remain to is the one where you started the second thread from. sounds complicated but it isn't. now you have to communicate from one thread to another. you can do this by using the invoke procedure of the control. i just will give you the sourcecode and you will see what will happen :) instead of using the regular add method of the listbox you have to use the listboxs "invoke" method. Put this code somewhere inside your class:
private delegate void AddTextInvoker(string textToAdd);
Now replace the line of code where you want to add an item to the listbox with these lines:object [] invokeParams = {"TextOrItemToAdd"}; lbReceived.Invoke(new AddTextInvoker(AddTextToListbox), invokeParams);
finally add the "AddTextToListbox" method to your class and make it add an item to the listbox:private void AddTextToListbox(string textToAdd) { lbReceived.Add(textToAdd); }
but be careful with invoking anything. it happened to me that one thread disposed a control and another one tried to change on of its properties - that will result in an exception. you should try to make yourself more familiar with threads since my knowledge ends here too. maybe someone else could tell you WHY its not possible to do a cross-thread operation and stuff - for now you should be able to solve your problem :P // edit: sorry code snippet did not work because it just was wrong - now it should work :P -- modified at 9:00 Tuesday 10th October, 2006