Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- BackgroundWorker backgroundWorkerA, backgroundWorkerB;
- System.Threading.ManualResetEvent manualReset;
- List<string> _shardList = new List<string>(0);
- public UserControl1()
- {
- InitializeComponent();
- manualReset = new System.Threading.ManualResetEvent(false);
- backgroundWorkerA = new BackgroundWorker();
- backgroundWorkerA.WorkerSupportsCancellation = true;
- backgroundWorkerA.DoWork += BackgroundWorkerA_DoWork;
- backgroundWorkerB = new BackgroundWorker();
- backgroundWorkerB.WorkerSupportsCancellation = true;
- backgroundWorkerB.DoWork += BackgroundWorkerB_DoWork;
- this.HandleCreated += UserControl1_HandleCreated;
- }
- private void UserControl1_HandleCreated(object sender, EventArgs e)
- {
- backgroundWorkerA.RunWorkerAsync(_shardList);
- backgroundWorkerB.RunWorkerAsync(_shardList);
- manualReset.Set();
- }
- private void BackgroundWorkerB_DoWork(object sender, DoWorkEventArgs e)
- {
- List<string> _shardList = (List<string>)e.Argument;
- manualReset.WaitOne();
- int _i = 0;
- while(!this.backgroundWorkerB.CancellationPending)
- {
- _shardList.Add("b" + _i++.ToString());
- System.Diagnostics.Debug.WriteLine("b is running");
- }
- thread2.Invoke(new MethodInvoker(delegate { thread2.Text = string.Join(System.Environment.NewLine, _shardList.ToArray()); }));
- }
- private void button1_Click(object sender, EventArgs e)
- {
- backgroundWorkerA.CancelAsync();
- backgroundWorkerB.CancelAsync();
- }
- private void BackgroundWorkerA_DoWork(object sender, DoWorkEventArgs e)
- {
- List<string> _shardList = (List<string>)e.Argument;
- manualReset.WaitOne();
- int _i = 0;
- while (!this.backgroundWorkerA.CancellationPending)
- {
- _shardList.Add("a" + _i++.ToString());
- System.Diagnostics.Debug.WriteLine("a is running");
- }
- thread1.Invoke(new MethodInvoker(delegate { thread1.Text = string.Join(System.Environment.NewLine, _shardList.ToArray()); }));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement