how to add timeOut without using thread.sleep

hi,

i have wrote a client/server ClassLibrary, i use separated thread to receive packets(receiver thread) , and i receive packets but not in order, for example if i send requestA and request B, i can get responseB b4 responseA, and in some cases the server will not send responseB (this is exactly how i want it to work)

so here its my question,

How can i add timeOut to my class (without blocking)

right now i use thread sleep something like this

int counter = 0;

bool MethodB()

{

Client.SendRequestB();

while (counter < 1000 && responseB == null)

{

counter++;

System.Threading.Thread.Sleep(60);

}

if (responseB != null) throw new ABCException(

"TimeOut: You sent RequestB but server didn't Response To It");

return true;

}

like that if the server didn't send ResopnseB within a Min i'll raise exception, i can't make the thread.sleep take longer time because the receiver thread is active and can forward other packets so like that i give a chance for it

but I know thread.sleep is not the best thing to do here, i think there is a better way to do that, i have searched but i got nothing about this, would anyone help me in that

thanks in advance




Answer this question

how to add timeOut without using thread.sleep

  • Tarana

    thanks for the detailed response ;-)

    Well the timer will return back to the caller as soon as its finished but it won't return a value back since its return type is void.

    you can start the timer wherever you like and stop it within the timer tick event for example, and set this object you are playing with in global scope so when the caller gets focus back, it can access this object, which would have been modified by say the timer tick/elapsed event.

    you can, as you have done, create custom events which will be raised in the timer event for example or wherever you like. Trying to dish out the ideas....



  • hr0nix

    thank you ahmad

    i don't send any objects to the server, the object that i were talking about is a packet (data in special formate which server can understand it like first name , last name , or pass word etc but the packet has to has other data for the server like packet length .. etc)

    what i want to do here is something like when authencticate a user name online it take few steps server can stop responding in any time for example

    send the user name (if the server didn't return back its valid user withen 1 min return time out, if the server respond go to next step)

    send the user name and the password ( then same thing here )

    .... etc

    i hade to wirte a separated class for that,

    i just was wishing to wirte it in the form of socket class set the time out for send or recieve something like that , but at least it working now

    anyway many thanks



  • ZopoStyle

    you could place a timer on the form or use the System.Timers.Timer class and set the interval for the timer, implement the event and do whatever it is you like on the timer tick event. This would probably be a reasonable solution.

    Of course, make sure that you have some way of accessing the "response" object, by placing it in global scope so that the timer tick event can access it and check the value, and if its null for example, stop the timer and throw the exception until its handled or something, then start the timer again when its ready and needing to start the process over again.

    Having a timer tick event would be better than say doing a for loop and having a Thread.Sleep() event, its more efficient using the timer approach.

    is this something you may be interested in



  • Chirag Patel

    hi, Ahmed and nobugs
    Ahmed :its a class library not form
    nobugs : i use TCP Socket for connection , to send and receieve custom packets, but I can't block the connection for one reason , there are many classes depend on it at the same time so if i blocked it i will disturb the entire program, I need to make a timeOut variable in my class LIKE THE CONNECTION TIME OUT BUT WITHOUT USING THE CONNECTION TIMEOUT.

    yeah i thought in timers either(System.Timers.Timer, Threading.Timer)
    but i found 2 problems
    System.timer doesn't have any sense of previous action if you notice in my code i want when the timer finish checking "it returns to the caller method" to continue, but System Timer Doesn't return to the caller method when finish

    i have tried Threading.Timer this timer return to the caller method , but there is a problem, it run in a threadpool so if i changed the flag variable in current thread it will not have any sense with it so it will not return

    currently i wrote a separated class just for this job(instead of above method) i used System.Timers, and i use events to raise timeOUt, it work in funny way but at least it works,

    so if anyone has a better way , plz tell me, thanks in advance



  • Mikael H&amp;#229;kansson

    You don't give any details on what class you use to implement the connection or how the responseB reference gets set. I assume you have a separate thread that receives the response. You can block your thread using a EventWaitHandle instance. Use the WaitOne(Int32, Boolean) method overload to specify the timeout. Let the thread that processes the response call the Set() method.


  • how to add timeOut without using thread.sleep