SECI  1
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events
JournalControl.xaml.cs
Go to the documentation of this file.
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.Xml;
15 using System.Data;
16 using System.Collections.ObjectModel;
17 using System.ComponentModel;
18 using System.Timers;
19 using System.IO;
20 using System.Diagnostics;
21 
22 namespace SeciControls
23 {
27  public partial class JournalControl : UserControl
28  {
29  public Boolean OkayToUpdate { get; set; }
31  String _basedir = Environment.CurrentDirectory;
32  BackgroundWorker _worker = new BackgroundWorker();
33  Timer _timer;
34  delegate void UpdateTableDelegate(List<Entry> entries);
35  delegate void UpdateErrorDelegate(String message);
36 
37  public JournalControl()
38  {
39  InitializeComponent();
40 
41  if (Seci.Definitions.Status.IsMuonInstrument)
42  {
43  colMev.Visibility = System.Windows.Visibility.Visible;
44  colUamps.Visibility = System.Windows.Visibility.Collapsed;
45  }
46  }
47 
48  private void UserControl_Loaded(object sender, RoutedEventArgs e)
49  {
50  //This method is called whenever the control is added to the visual tree.
51  //This usually happens once when Seci is started and once when the
52  //journal viewer is displayed for the first time.
53  //We only want this code to run once.
54  if (_timer == null)
55  {
56  _basedir = Seci.Definitions.Status.JournalLocation;
57  //Run it once to get initial entries
58  _worker.DoWork += new DoWorkEventHandler(_worker_DoWork);
59  _worker.RunWorkerAsync();
60  _timer = new Timer(10000);
61  _timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
62  _timer.Start();
63  //No more updates until instructed
64  OkayToUpdate = false;
65  }
66  }
67 
68  void _worker_DoWork(object sender, DoWorkEventArgs e)
69  {
70  try
71  {
72  List<Entry> entries = new List<Entry>();
73  if (_dm.GetCurrentCycleData(_basedir, @"\journal_main.xml", out entries))
74  {
75  Dispatcher.Invoke(new UpdateTableDelegate(updateTable), entries);
76  }
77  }
78  catch (FileNotFoundException err)
79  {
80  Dispatcher.Invoke(new UpdateErrorDelegate(showErrorMessage), err.Message);
81  }
82  catch(Exception err)
83  {
84  Dispatcher.Invoke(new UpdateErrorDelegate(showErrorMessage), err.Message);
85  }
86  }
87 
88  void _timer_Elapsed(object sender, ElapsedEventArgs e)
89  {
90  if (!_worker.IsBusy && OkayToUpdate)
91  {
92  _worker.RunWorkerAsync();
93  }
94  }
95 
96  private void updateTable(List<Entry> entries)
97  {
98  try
99  {
100  ObservableCollection<Entry> coll = new ObservableCollection<Entry>(entries);
101  dgJournal.ItemsSource = coll;
102  showErrorMessage(null);
103  }
104  catch (Exception err)
105  {
106  Console.WriteLine(err.Message);
107  }
108  }
109 
110  private void showErrorMessage(String message)
111  {
112  try
113  {
114  if (String.IsNullOrEmpty(message))
115  {
116  txtError.Visibility = System.Windows.Visibility.Collapsed;
117  txtError.Text = "";
118  }
119  else
120  {
121  txtError.Visibility = System.Windows.Visibility.Visible;
122  txtError.Text = "ERROR: " + message;
123  }
124  }
125  catch
126  {
127  }
128  }
129 
130  private void lnkJournal_Click(object sender, RoutedEventArgs e)
131  {
132  System.Diagnostics.Process.Start(_basedir + @"\journal_main.html");
133  e.Handled = true;
134  }
135  }
136 
137 
138 }
139 
Boolean GetCurrentCycleData(String journalDir, String mainFileName, out List< Entry > entries)
delegate void UpdateErrorDelegate(String message)
void showErrorMessage(String message)
void lnkJournal_Click(object sender, RoutedEventArgs e)
void UserControl_Loaded(object sender, RoutedEventArgs e)
Interaction logic for JournalViewer.xaml
void _worker_DoWork(object sender, DoWorkEventArgs e)
void updateTable(List< Entry > entries)
delegate void UpdateTableDelegate(List< Entry > entries)
void _timer_Elapsed(object sender, ElapsedEventArgs e)