News




Aborting a thread




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using System;
using System.Threading;
 
namespace Threading
{
  public class Program
  {
 
    protected static void threadFunction()
    {
      Console.WriteLine("Inside the thread. ");
      while (true)
      {
        Console.Write("." );
        Thread.Sleep(400);
      }
    }
 
 
    static void Main(string[] args)
    {
      Thread lThread = new Thread(new ThreadStart(threadFunction));
 
      lThread.Start();
      Thread.Sleep(5000);
      lThread.Abort();
      lThread.Join();
    }
  }
}


Stopping a thread




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using System;
using System.Threading;
 
namespace Threading
{
  public class Program
  {
    static bool mStopThread = false;
 
    protected static void threadFunction()
    {
 
      Console.WriteLine("Inside the thread. ");
      while (mStopThread == false)
      {
        Console.Write("." );
        Thread.Sleep(400);
      }
    }
 
 
    static void Main(string[] args)
    {
      Thread lThread = new Thread(new ThreadStart(threadFunction));
 
      lThread.Start();
      Thread.Sleep(5000);
      mStopThread = true;
      lThread.Join();
    }
  }
}


Creating a thread




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using System;
using System.Threading;
 
namespace Threading
{
  public class Program
  {
 
    protected static void threadFunction()
    {
      int lCounter;
 
      for (lCounter = 0; lCounter < 10; lCounter++)
      {
        Console.WriteLine(lCounter);
        Thread.Sleep(400);
      }
    }
 
    static void Main(string[] args)
    {
      Thread lThread = new Thread(new ThreadStart(threadFunction));
 
      lThread.Start();
      lThread.Join();
    }
  }
}


Get local IP addresses




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
 
namespace HostName
{
  public class Program
  {
    static void Main(string[] args)
    {
      String lComputerName = Dns.GetHostName();
      IPAddress[] localIPs = Dns.GetHostAddresses(lComputerName);
 
      if (localIPs.Length > 0)
        foreach (IPAddress lAddress in localIPs)
          Console.WriteLine(lAddress);
    }
  }
}


Get local host name




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
 
namespace HostName
{
  public class Program
  {
    static void Main(string[] args)
    {
      string lHostName = Dns.GetHostName();
      Console.WriteLine("Host name : " + lHostName);
    }
  }
}





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
using System;
using System.Text;
using System.Net;
 
namespace HostName2IP
{
    class Program
    {
        static void Main(string[] args)
        {
            IPHostEntry lHost = Dns.GetHostByName("www.google.com");
            IPAddress lIPAddr = lHost.AddressList[0];
            Console.WriteLine(lIPAddr);
        }
    }
}


« Newer posts - Older posts »