SECI  1
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events
McrNews.xaml.cs
Go to the documentation of this file.
1 using System;
2 using System.Windows;
3 using System.Windows.Controls;
4 using System.ComponentModel;
5 using System.Net;
6 using System.IO;
7 
8 namespace SeciControls
9 {
13  public partial class McrNews : UserControl
14  {
15  String _url = "http://dataweb.isis.rl.ac.uk/mcr/mcrnews.txt";
16  BackgroundWorker _worker = new BackgroundWorker();
17  System.Timers.Timer _timer;
18  int _updateRateSecs = 30;
19 
20  public McrNews()
21  {
22  InitializeComponent();
23  }
24 
25  private void UserControl_Loaded(object sender, RoutedEventArgs e)
26  {
27  _worker.DoWork += new DoWorkEventHandler(_worker_DoWork);
28  _worker.RunWorkerAsync();
29 
30  _timer = new System.Timers.Timer();
31  _timer.Interval = _updateRateSecs * 1000;
32  _timer.Elapsed += new System.Timers.ElapsedEventHandler(_timer_Elapsed);
33  _timer.Start();
34  }
35 
36  void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
37  {
38  if (!_worker.IsBusy)
39  {
40  _worker.RunWorkerAsync();
41  }
42  }
43 
44  void _worker_DoWork(object sender, DoWorkEventArgs e)
45  {
46  Action action = delegate()
47  {
48  String text = GetLatestInformation();
49 
50  if (!String.IsNullOrEmpty(text) && text != txtNews.Text)
51  {
52  //Update the data
53  txtNews.Text = text;
54  }
55  };
56 
57  this.Dispatcher.Invoke(action);
58  }
59 
60  public String GetLatestInformation()
61  {
62  try
63  {
64  if (String.IsNullOrEmpty(_url)) return "";
65 
66  String data;
67  WebRequest wr = WebRequest.Create(_url);
68 
69  using (WebResponse res = wr.GetResponse())
70  using (Stream s = res.GetResponseStream())
71  using (StreamReader sr = new StreamReader(s))
72  data = sr.ReadToEnd();
73 
74  return data;
75  }
76  catch
77  {
78  return "";
79  }
80  }
81  }
82 }
System.Timers.Timer _timer
Definition: McrNews.xaml.cs:17
void _worker_DoWork(object sender, DoWorkEventArgs e)
Definition: McrNews.xaml.cs:44
String GetLatestInformation()
Definition: McrNews.xaml.cs:60
void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
Definition: McrNews.xaml.cs:36
BackgroundWorker _worker
Definition: McrNews.xaml.cs:16
void UserControl_Loaded(object sender, RoutedEventArgs e)
Definition: McrNews.xaml.cs:25
Interaction logic for McrNews.xaml
Definition: McrNews.xaml.cs:13