Advertisement
Guest User

Untitled

a guest
Oct 12th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.45 KB | None | 0 0
  1. BackgroundWorker backgroundWorkerA, backgroundWorkerB;
  2.         System.Threading.ManualResetEvent manualReset;
  3.         List<string> _shardList = new List<string>(0);
  4.  
  5.         public UserControl1()
  6.         {
  7.            
  8.             InitializeComponent();
  9.             manualReset = new System.Threading.ManualResetEvent(false);
  10.             backgroundWorkerA = new BackgroundWorker();
  11.             backgroundWorkerA.WorkerSupportsCancellation = true;
  12.             backgroundWorkerA.DoWork += BackgroundWorkerA_DoWork;
  13.            
  14.             backgroundWorkerB = new BackgroundWorker();
  15.             backgroundWorkerB.WorkerSupportsCancellation = true;
  16.             backgroundWorkerB.DoWork += BackgroundWorkerB_DoWork;
  17.            
  18.            
  19.             this.HandleCreated += UserControl1_HandleCreated;
  20.            
  21.         }
  22.        
  23.  
  24.         private void UserControl1_HandleCreated(object sender, EventArgs e)
  25.         {
  26.             backgroundWorkerA.RunWorkerAsync(_shardList);
  27.             backgroundWorkerB.RunWorkerAsync(_shardList);
  28.             manualReset.Set();
  29.         }
  30.  
  31.         private void BackgroundWorkerB_DoWork(object sender, DoWorkEventArgs e)
  32.         {
  33.             List<string> _shardList = (List<string>)e.Argument;
  34.             manualReset.WaitOne();
  35.             int _i = 0;
  36.             while(!this.backgroundWorkerB.CancellationPending)
  37.             {
  38.                 _shardList.Add("b" + _i++.ToString());
  39.                 System.Diagnostics.Debug.WriteLine("b is running");
  40.                
  41.             }
  42.  
  43.             thread2.Invoke(new MethodInvoker(delegate { thread2.Text = string.Join(System.Environment.NewLine, _shardList.ToArray()); }));
  44.         }
  45.  
  46.         private void button1_Click(object sender, EventArgs e)
  47.         {
  48.             backgroundWorkerA.CancelAsync();
  49.             backgroundWorkerB.CancelAsync();
  50.         }
  51.  
  52.         private void BackgroundWorkerA_DoWork(object sender, DoWorkEventArgs e)
  53.         {
  54.             List<string> _shardList = (List<string>)e.Argument;
  55.             manualReset.WaitOne();
  56.             int _i = 0;
  57.             while (!this.backgroundWorkerA.CancellationPending)
  58.             {
  59.                 _shardList.Add("a" + _i++.ToString());
  60.                 System.Diagnostics.Debug.WriteLine("a is running");
  61.                
  62.             }
  63.  
  64.             thread1.Invoke(new MethodInvoker(delegate { thread1.Text = string.Join(System.Environment.NewLine, _shardList.ToArray()); }));
  65.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement