PageRenderTime 3ms CodeModel.GetById 16ms app.highlight 7ms RepoModel.GetById 16ms app.codeStats 0ms

/FromEvent/FromEvent/Form1.cs

https://github.com/M201370367/myGithub
C# | 68 lines | 55 code | 11 blank | 2 comment | 0 complexity | aa4f856af551e56449ad506bb9b3ea3f MD5 | raw file
 1using System;
 2using System.Collections.Generic;
 3using System.ComponentModel;
 4using System.Data;
 5using System.Drawing;
 6using System.Linq;
 7using System.Text;
 8using System.Threading.Tasks;
 9using System.Windows.Forms;
10using System.Reactive;
11using System.Reactive.Linq;
12using System.Reactive.Threading;
13using System.Reactive.Concurrency;
14using System.Reactive.Disposables;
15using System.Windows;
16using System.Windows.Threading;
17using System.Reactive.Joins;
18using System.Reactive.PlatformServices;
19using System.Reactive.Subjects;
20using Microsoft.VisualBasic;
21using Microsoft.VisualBasic.Devices;
22using System.Threading;
23
24
25
26
27namespace FromEvent
28{
29    public partial class Form1 : Form
30    {
31        public Form1()
32        {
33            InitializeComponent();
34        }
35        
36
37        private void Form1_Load(object sender, EventArgs e)
38        {
39
40            var start = Observable.FromEventPattern<EventArgs>(button1, "Click");
41            var stop = Observable.FromEventPattern<EventArgs>(button2, "Click");
42
43            
44            var clock2 = Observable.Interval(TimeSpan.FromSeconds(1)).SubscribeOn(Scheduler.NewThread).TakeUntil(stop);
45            start.Subscribe(x =>{
46                var clock = Observable.Interval(TimeSpan.FromSeconds(1)).Select((t, index) => Thread.CurrentThread.ManagedThreadId).TakeUntil(stop);
47                clock.SubscribeOn(Scheduler.NewThread).ObserveOn(DispatcherScheduler.Instance).Subscribe(timeIndex =>
48                label1.Text = String.Format("{0}", timeIndex.ToString()));
49                clock2.ObserveOn(this).Subscribe(timeIndex =>
50                label2.Text = String.Format("{0}", timeIndex.ToString()));
51            });
52
53            //clock.ObserveOn(DispatcherScheduler.Instance).Subscribe(timeIndex =>
54               // label1.Text = String.Format("{0}" , timeIndex.Time));
55            
56        }
57    }
58    class TimeIndex
59    {
60        public TimeIndex(int index, DateTimeOffset time)
61        {
62            Index = index;
63            Time = time;
64        }
65        public int Index { get; set; }
66        public DateTimeOffset Time { get; set; }
67    }
68}