Thread.Sleep()

Page 1 of 1

5 Replies - 2899 Views - Last Post: 01 October 2012 - 01:08 AM Rate Topic: -----

#1 Poornima Mani  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 178
  • Joined: 25-January 09

Thread.Sleep()

Post icon  Posted 17 March 2009 - 08:20 AM

Hi
I have used multithreading for udp client/server communication.When i run the server both the threads are executing at the same time.So i used a Thread.Sleep() after the 1st thread.So the 1st thread works properly.When it comes to the 2nd thread it gives"the existing connection was forcibly closed by the remote host".Can you please give me your suggestion
public UdpServer()
		{
			try
			{
				startServer = new Thread(new ThreadStart(start_server));
				startServer.Start();
			}
			catch (Exception e)
			{
				Console.WriteLine(e.Message);
				//startServer.Abort();
			}

		   Thread.Sleep(20000);
			try
			{			   
				
				startServer2 = new Thread(new ThreadStart(start_server2));				
				startServer2.Start();
			}
			catch (Exception e)
			{
				Console.WriteLine(e.Message);
				//startServer2.Abort();
			}

		} public static void start_server()
		{
		   
				IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10001);
			try
			{
				Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
				newsock.Bind(ipep);
				Console.WriteLine("Waiting for a client...");

				while (true)
				{  //IPEndPoint sender = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8050);
						EndPoint tmpRemote = (EndPoint)ipep;
						// EndPoint tmpRemote = (sender);

						byte[] data = new byte[1024];
						Console.WriteLine("hai");

						int recv = newsock.ReceiveFrom(data, 0, data.Length, SocketFlags.None, ref tmpRemote);
						
						Console.WriteLine("Message received from {0}:", tmpRemote.ToString());
						Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));//hello is received  

						data = new byte[1024];
						string ss = "Welcome to the Server";
						data = Encoding.ASCII.GetBytes(ss);
						newsock.SendTo(data, 0, data.Length, SocketFlags.None, tmpRemote);

						Console.WriteLine("\nSent Acknowledgement");
			   

					}
			}
					catch (SocketException e)
					{
						Console.WriteLine(e.Message);
					}


					//start_server();
					//startServer.Start();			

		}
public static void start_server2()
{//same as above
}


Is This A Good Question/Topic? 0
  • +

Replies To: Thread.Sleep()

#2 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 170
  • View blog
  • Posts: 637
  • Joined: 07-October 08

Re: Thread.Sleep()

Posted 18 March 2009 - 08:20 AM

Hi, not overly sure I'm getting you, but I would look into using the AutoResetEvent/ManualResetEvent for blocking thread execution. I noticed the thread is sleeping for 20000ms (20s) is that just for convienience or is that an absolute value that you want it to always sleep for?
Was This Post Helpful? 0
  • +
  • -

#3 Poornima Mani  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 178
  • Joined: 25-January 09

Re: Thread.Sleep()

Posted 19 March 2009 - 02:17 AM

View Postragingben, on 18 Mar, 2009 - 07:20 AM, said:

Hi, not overly sure I'm getting you, but I would look into using the AutoResetEvent/ManualResetEvent for blocking thread execution. I noticed the thread is sleeping for 20000ms (20s) is that just for convienience or is that an absolute value that you want it to always sleep for?

Hi since i have defined 2 threads ,when i execute the code the 2nd thread starts immediately after the 1st.I want the 1st thread to finish completely and then the 2nd one should begin and that is why i have given that.The use of 20s is not a must
Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Thread.Sleep()

Posted 19 March 2009 - 09:00 AM

Use a loop that iterates until the first thread has completed, instead of putting the main thread to sleep. Use the IsAlive property of the Thread to determine how long the loop should iterate.

public UdpServer()
		{
			try
			{
				startServer = new Thread(new ThreadStart(start_server));
				startServer.Start();
			}
			catch (Exception e)
			{
				Console.WriteLine(e.Message);
				//startServer.Abort();
			}
		   //Will keep looping until thread has completed execution
		   while(startServer.IsAlive)
		   { //Do not need any code here }

			try
			{			  
			   
				startServer2 = new Thread(new ThreadStart(start_server2));				
				startServer2.Start();
			}
			catch (Exception e)
			{
				Console.WriteLine(e.Message);
				//startServer2.Abort();
			}

		} public static void start_server()
		{
		   
				IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10001);
			try
			{
				Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
				newsock.Bind(ipep);
				Console.WriteLine("Waiting for a client...");

				while (true)
				{  //IPEndPoint sender = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8050);
						EndPoint tmpRemote = (EndPoint)ipep;
						// EndPoint tmpRemote = (sender);

						byte[] data = new byte[1024];
						Console.WriteLine("hai");

						int recv = newsock.ReceiveFrom(data, 0, data.Length, SocketFlags.None, ref tmpRemote);
					   
						Console.WriteLine("Message received from {0}:", tmpRemote.ToString());
						Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));//hello is received  

						data = new byte[1024];
						string ss = "Welcome to the Server";
						data = Encoding.ASCII.GetBytes(ss);
						newsock.SendTo(data, 0, data.Length, SocketFlags.None, tmpRemote);

						Console.WriteLine("\nSent Acknowledgement");
			   

					}
			}
					catch (SocketException e)
					{
						Console.WriteLine(e.Message);
					}


					//start_server();
					//startServer.Start();			

		}
public static void start_server2()
{//same as above
}

Was This Post Helpful? 0
  • +
  • -

#5 dotINSolution  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 16
  • Joined: 25-September 12

Re: Thread.Sleep()

Posted 28 September 2012 - 11:38 PM

Many ways, you can use Wait and Pulse, Manual/Auto Reset Events, CountdownEvent for waiting until a specified work is done.
You can also use Thread.Join to wait if you want to do simply by thread class only.

.NET Framework 4 Task Paralllel Library (TPL) also introduces many new ways of queuing threads, so i would suggest you to take a look at it.
Was This Post Helpful? 0
  • +
  • -

#6 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 170
  • View blog
  • Posts: 637
  • Joined: 07-October 08

Re: Thread.Sleep()

Posted 01 October 2012 - 01:08 AM

View PostdotINSolution, on 29 September 2012 - 07:38 AM, said:

Many ways, you can use Wait and Pulse, Manual/Auto Reset Events, CountdownEvent for waiting until a specified work is done.
You can also use Thread.Join to wait if you want to do simply by thread class only.

.NET Framework 4 Task Paralllel Library (TPL) also introduces many new ways of queuing threads, so i would suggest you to take a look at it.

Although this is all correct, this is a really old thread, the last post was 2009 so I doubt the OP is still stuck on or will even see this.

.Net 4 wasn't even out when he asked the question!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1