Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Advertising

Untitled
Friday, May 11th, 2012 at 1:53:23am MDT 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using System.Threading;
  15. using System.Reflection;
  16.  
  17. namespace WpfApplication1
  18. {
  19.     /// <summary>
  20.     /// Interaction logic for MainWindow.xaml
  21.     /// </summary>
  22.     public partial class MainWindow : Window
  23.     {
  24.         private static int _shIdx;// какой свет горит
  25.         private static Semaphore _s;
  26.         public MainWindow()
  27.         {
  28.             InitializeComponent();
  29.             Random rnd = new Random();
  30.          //   _shIdx = rnd.Next(3);
  31.             _shIdx = 0;
  32.             _s = new Semaphore(0, 1);
  33.             _s.Release();
  34.             Thread t = new Thread(new ThreadStart(updateView));// поток, обновляющий интерфейс
  35.             t.Start();
  36.             Thread w = new Thread(new ThreadStart(updateIdx));// поток-счетчик
  37.             w.Start();
  38.  
  39.            
  40.         }
  41.         private void updateView()
  42.         {
  43.             while (true)
  44.             {
  45.                 _s.WaitOne();
  46.                 setEllipse();
  47.                 Thread.Sleep(500);// обновление раз в пол-секунды
  48.                 _s.Release();
  49.                 Thread.Sleep(50); // чтобы дошло сообщение release до updateIdx
  50.             }
  51.         }
  52.         private delegate void work();
  53.         private void setEllipse()
  54.         {
  55.             if (Dispatcher.Thread != Thread.CurrentThread)
  56.             {
  57.                 Dispatcher.Invoke(new work(setEllipse), null);
  58.                 return;
  59.             }
  60.             if (_shIdx == 0)
  61.             {
  62.                 ellipse1.Fill = Brushes.Red;
  63.                 ellipse2.Fill = null;
  64.                 ellipse3.Fill = null;
  65.             }
  66.             if (_shIdx == 1)
  67.             {
  68.                 ellipse1.Fill = null;
  69.                 ellipse2.Fill = Brushes.Yellow;
  70.                 ellipse3.Fill = null;
  71.             }
  72.             if (_shIdx == 2)
  73.             {
  74.                 ellipse1.Fill = null;
  75.                 ellipse2.Fill = null;
  76.                 ellipse3.Fill = Brushes.Green;
  77.             }
  78.         }
  79.         private static void updateIdx()
  80.         {
  81.             bool inv = false;
  82.             while (true)
  83.             {
  84.                 _s.WaitOne();
  85.                 if (inv)
  86.                 {
  87.                     _shIdx -= 1;
  88.                     if (_shIdx == 0)
  89.                         inv = !inv;
  90.                 }
  91.                 else
  92.                 {
  93.                     _shIdx += 1;
  94.                     if (_shIdx == 2)
  95.                         inv = !inv;
  96.                 }
  97.                 _s.Release();
  98.                 Thread.Sleep(100); // чтобы сообщение release дошло до updateView
  99.             }
  100.         }
  101.     }
  102.    
  103. }

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.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



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.

fantasy-obligation
fantasy-obligation