Advertising
- Untitled
- Friday, May 11th, 2012 at 1:53:23am MDT
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.Threading;
- using System.Reflection;
- namespace WpfApplication1
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- private static int _shIdx;// какой свет горит
- private static Semaphore _s;
- public MainWindow()
- {
- InitializeComponent();
- Random rnd = new Random();
- // _shIdx = rnd.Next(3);
- _shIdx = 0;
- _s = new Semaphore(0, 1);
- _s.Release();
- Thread t = new Thread(new ThreadStart(updateView));// поток, обновляющий интерфейс
- t.Start();
- Thread w = new Thread(new ThreadStart(updateIdx));// поток-счетчик
- w.Start();
- }
- private void updateView()
- {
- while (true)
- {
- _s.WaitOne();
- setEllipse();
- Thread.Sleep(500);// обновление раз в пол-секунды
- _s.Release();
- Thread.Sleep(50); // чтобы дошло сообщение release до updateIdx
- }
- }
- private delegate void work();
- private void setEllipse()
- {
- if (Dispatcher.Thread != Thread.CurrentThread)
- {
- Dispatcher.Invoke(new work(setEllipse), null);
- return;
- }
- if (_shIdx == 0)
- {
- ellipse1.Fill = Brushes.Red;
- ellipse2.Fill = null;
- ellipse3.Fill = null;
- }
- if (_shIdx == 1)
- {
- ellipse1.Fill = null;
- ellipse2.Fill = Brushes.Yellow;
- ellipse3.Fill = null;
- }
- if (_shIdx == 2)
- {
- ellipse1.Fill = null;
- ellipse2.Fill = null;
- ellipse3.Fill = Brushes.Green;
- }
- }
- private static void updateIdx()
- {
- bool inv = false;
- while (true)
- {
- _s.WaitOne();
- if (inv)
- {
- _shIdx -= 1;
- if (_shIdx == 0)
- inv = !inv;
- }
- else
- {
- _shIdx += 1;
- if (_shIdx == 2)
- inv = !inv;
- }
- _s.Release();
- Thread.Sleep(100); // чтобы сообщение release дошло до updateView
- }
- }
- }
- }
advertising
Update the Post
Either update this post and resubmit it with changes, or make a new post.
You may also comment on this post.
Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.