Exemplo n.º 1
0
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //var c = SearchEngine.GetCurrentEngines();
                //SearchEngine.AddNewSearchEngine(new SearchEngine { Name = "google" });
                t = new TaskThreadManager(10);
                button1.Enabled = false;
                listView1.Items.Clear();

                //  GetResults("Youtube", textBox1.Text, 3);
                t.DoWork(() => GetResults("Google", textBox1.Text));
                //t.DoWork(() => GetResults("Yahoo", textBox1.Text, 1));
                //t.DoWork(() => GetResults("Bing", textBox1.Text, 2));
                //t.DoWork(() => GetResults("Baidu", textBox1.Text, 3));
                //t.DoWork(() => GetResults("Yandex", textBox1.Text));



                // GetResults("yahoo", textBox1.Text, 0);
            }
            catch (Exception er)
            {
                Text = er.Message;
            }
            finally { Text = "Working.."; }
        }
        public void DoWork_TokenCanceled_WorkNeverCompleted()
        {
            CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
            TaskThreadManager       uut = new TaskThreadManager(10, cancellationTokenSource);

            int count = 0;

            uut.DoWork(() => { System.Threading.Thread.Sleep(50); Interlocked.Increment(ref count); });
            uut.DoWork(() => { System.Threading.Thread.Sleep(50); Interlocked.Increment(ref count); });
            uut.DoWork(() => { System.Threading.Thread.Sleep(50); Interlocked.Increment(ref count); });
            uut.DoWork(() => { System.Threading.Thread.Sleep(50); Interlocked.Increment(ref count); });
            uut.DoWork(() => { System.Threading.Thread.Sleep(50); Interlocked.Increment(ref count); });

            cancellationTokenSource.Cancel();

            System.Threading.Thread.Sleep(250);
            Assert.IsTrue(count < 5);
        }
        public void DoWork_TokenCanceled_WorkNeverCompleted()
        {
            var cancellationTokenSource = new CancellationTokenSource();
            var uut = new TaskThreadManager(10, cancellationTokenSource);

            var count = 0;

            uut.DoWork(() => { System.Threading.Thread.Sleep(50); Interlocked.Increment(ref count); });
            uut.DoWork(() => { System.Threading.Thread.Sleep(50); Interlocked.Increment(ref count); });
            uut.DoWork(() => { System.Threading.Thread.Sleep(50); Interlocked.Increment(ref count); });
            uut.DoWork(() => { System.Threading.Thread.Sleep(50); Interlocked.Increment(ref count); });
            uut.DoWork(() => { System.Threading.Thread.Sleep(50); Interlocked.Increment(ref count); });
            uut.DoWork(() => { System.Threading.Thread.Sleep(50); Interlocked.Increment(ref count); });
            uut.DoWork(() => { System.Threading.Thread.Sleep(50); Interlocked.Increment(ref count); });
            uut.DoWork(() => { System.Threading.Thread.Sleep(50); Interlocked.Increment(ref count); });
            uut.DoWork(() => { System.Threading.Thread.Sleep(50); Interlocked.Increment(ref count); });
            uut.DoWork(() => { System.Threading.Thread.Sleep(50); Interlocked.Increment(ref count); });

            cancellationTokenSource.Cancel();

            System.Threading.Thread.Sleep(600);
            Assert.IsTrue(count < 10, "Completed all 10 tasks but should not have");
        }