示例#1
0
 private void Button_Click(object sender, RoutedEventArgs e) {
    var objCustomThread = new CustomThread(Operation);
    listBox3.Items.Add(new ListBoxItem() {
       Tag = objCustomThread,
       Content = $"Thread {objCustomThread.CurrentNumber}--> created"
    });
 }
示例#2
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var objCustomThread = new CustomThread(Operation);

            listBox3.Items.Add(new ListBoxItem()
            {
                Tag     = objCustomThread,
                Content = $"Thread {objCustomThread.CurrentNumber}--> created"
            });
        }
示例#3
0
        private void Operation(object arg)
        {
            CustomThread objCustomThread = arg as CustomThread;

            int iCount = 0;

            if (!Semaphore.TryOpenExisting("MY_SEMAPHORE", out objSemaphore))
            {
                objSemaphore = new Semaphore(iThreadsCounter, iThreadsCounter, "MY_SEMAPHORE");
            }

            ListBoxItem objListBoxItem = null;

            try {
                objSemaphore.WaitOne();

                Dispatcher.BeginInvoke(new ThreadStart(() => {
                    foreach (ListBoxItem item in listBox2.Items)
                    {
                        if ((item.Tag as CustomThread) == objCustomThread)
                        {
                            listBox2.Items.Remove(item);
                            break;
                        }
                    }

                    objListBoxItem = new ListBoxItem()
                    {
                        Content = String.Format("Thread {0}--> {1}", objCustomThread.CurrentNumber, iCount),
                        Tag     = objCustomThread
                    };
                    listBox1.Items.Add(objListBoxItem);
                }));

                while (iCount < 100)
                {
                    ++iCount;
                    Thread.Sleep(100);
                    Dispatcher.BeginInvoke(new ThreadStart(() => { objListBoxItem.Content = String.Format("Thread {0}--> {1}", objCustomThread.CurrentNumber, iCount); }));
                }
                Dispatcher.BeginInvoke(new ThreadStart(() => { listBox1.Items.Remove(objListBoxItem); }));
            }
            finally {
                objSemaphore.Release();
            }
        }