Exemplo n.º 1
0
        private void Threading(object obj)
        {
            int    counter = 0;
            Random rdm     = new Random();

            if (obj is int max)
            {
                //the thread loop, loop until Stop pressed or max value
                while (_running && counter <= max)
                {
                    //invoke might throw ObjectDisposedException
                    try
                    {
                        //invoke on the progressbar to update with loop count
                        DataBox.Invoke(new DelVoidInt(AddListBox), rdm.Next(0, 10001));
                        progressBar1.Invoke(new DelVoidInt(cbUpdateProgress), counter);
                    }
                    catch (ThreadStateException ex)
                    {
                        MessageBox.Show(ex.Message, "Using Invoke");
                    }

                    //update the loop count
                    ++counter;

                    //brief delay to slow down the thread
                    Thread.Sleep(2);
                }
            }
            //argument was not type int, exit the thread
            else
            {
                MessageBox.Show("Can't run the thread!", "Using Invoke");
            }
        }