Wednesday 14 November 2018

Multi process download

To fast up the process split up two different threads
   private static void Writer()
   {            
   try
      {               
         WebClient client = new WebClient();
         string status = client.DownloadString
  ("https://speckyboy.com/25-modern-content-heavy-websites/");
 
                 using (FileStream fs = File.Open("D:\\import\\111.txt", FileMode.Create,
   FileAccess.Write, FileShare.ReadWrite))
                 {
                     byte[] bytes = Encoding.ASCII.GetBytes(status);
                     int bytelen = bytes.Length / 2;
 
                    Thread t = new Thread(()=> NewMethod2(status, fs,0, bytelen));
                     t.Start();
 
                     Thread t1 = new Thread(()=> NewMethod2(status, fs, bytelen, bytes.Length));
                     t1.Start();
                    
                     while (true)
                     {
 
                         if (!t.IsAlive && !t1.IsAlive)
                         {
                             fs.Close();
                             break;
                         }
                     }
                     Console.Write("Write:" + status + Environment.NewLine);
                 }
                 System.Threading.Thread.Sleep(10);
               
             }
             catch (Exception ex)
             {
                 Console.Write("write" + ex.ToString());
                 Console.Read();
             }
         }
 
   private static void NewMethod2(string status, FileStream fs,int start, int bytelen)
          {
              for (int i = start; i < bytelen; i++)
              {
                  fs.Write(Encoding.ASCII.GetBytes(status[i].ToString()), 0,
   Encoding.ASCII.GetBytes(status[i].ToString()).Length);
  
              }
          }