Advertisement
Guest User

Untitled

a guest
Jan 7th, 2016
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.71 KB | None | 0 0
  1. public delegate void TestDeleg(string message);
  2.     public class Data
  3.     {
  4.         public TestDeleg deleg;
  5.         public string mess;
  6.     }
  7.    
  8.     class test
  9.     {
  10.        
  11.         public List<Thread> threads = new List<Thread>();
  12.         public int nThreads = 0;
  13.         public int maxThreads = 5;
  14.        
  15.         public void DoWork(object data)
  16.         {
  17.             Data d = (Data)data;
  18.             d.deleg(d.mess);
  19.             //string message = (string)data;
  20.             //MessageBox.Show(d.mess);      
  21.             return;
  22.            
  23.                      
  24.         }
  25.  
  26.         public void CreateThread(object data)
  27.         {
  28.             if (nThreads >= maxThreads)
  29.                 return;
  30.             Thread newThread = new Thread(DoWork);
  31.             threads.Add(newThread);
  32.             newThread.IsBackground = true;
  33.             newThread.Start(data);
  34.             nThreads++;
  35.            
  36.  
  37.         }
  38.  
  39.         public void WindUpThreads()
  40.         {
  41.             //MessageBox.Show("count: " + nThreads.ToString());
  42.             for(int i = 0; i < threads.Count; i++)
  43.             {
  44.                 if (threads[i].IsAlive == false)
  45.                 {
  46.                     threads[i].Abort();
  47.                     threads.RemoveAt(i);
  48.                    //MessageBox.Show("removing at " + i.ToString());
  49.                 }
  50.  
  51.             }
  52.  
  53.             nThreads = threads.Count;
  54.         }
  55.        
  56.     }
  57.  
  58.  
  59.  
  60.  
  61. private void button1_Click(object sender, EventArgs e)
  62. {
  63.     button1.Enabled = false;
  64.     test thTest = new test();
  65.     string[] strings;
  66.     try
  67.     {
  68.  
  69.         strings = File.ReadAllLines("C:\\users\\alex\\desktop\\test.txt");
  70.     }
  71.  
  72.     catch (Exception ex)
  73.     {
  74.         MessageBox.Show(ex.Message);
  75.         return;
  76.     }
  77.                
  78.     bool flag = true;
  79.     int counter = 0;
  80.     int dataCount = strings.Length;
  81.  
  82.     while (flag == true)
  83.     {
  84.         if (counter >= dataCount)
  85.         {
  86.             flag = false;
  87.         }
  88.  
  89.         while (thTest.nThreads < thTest.maxThreads)
  90.         {
  91.             if (flag == false)
  92.                 break;
  93.            
  94.             //thTest.CreateThread(strings[counter]);
  95.             Data d = new Data();
  96.             d.deleg = AddItem;
  97.             d.mess = strings[counter];
  98.             thTest.CreateThread((object)d);
  99.             //MessageBox.Show(counter.ToString());
  100.             counter++;
  101.         }
  102.  
  103.         thTest.WindUpThreads();
  104.  
  105.         if (flag == false)
  106.         {
  107.             do
  108.             {
  109.                 thTest.WindUpThreads();
  110.  
  111.             } while (thTest.nThreads != 0);
  112.         }
  113.        
  114.  
  115.  
  116.     }
  117.    
  118.     listBox1.Items.Add("Done");
  119.  
  120. }
  121.  
  122. public void AddItem(string message)
  123. {
  124.                
  125.     lock(obj)    
  126.     {
  127.        
  128.         if(listBox1.InvokeRequired)
  129.            Invoke( (Action)( () => { listBox1.Items.Add(message); } ) );                    
  130.             //listBox1.BeginInvoke(new Action <string>(AddItem), message);
  131.         else                
  132.             listBox1.Items.Add(message);
  133.     }
  134.  
  135.    
  136. }
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement