DataSeriesExportingDialog.xaml.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.Shapes;
  13. using Aitex.UI.Charting.ViewModel;
  14. using System.Threading;
  15. using DataAnalysisControl.Core;
  16. using Aitex.UI.Charting.Model;
  17. namespace Aitex.UI.Charting.View
  18. {
  19. /// <summary>
  20. /// Interaction logic for DataSourceExportingDialog.xaml
  21. /// </summary>
  22. public partial class DataSeriesExportingDialog : Window
  23. {
  24. public DataSeriesExportingDialog(string uniqueDataId, string fileName)
  25. {
  26. InitializeComponent();
  27. _uniqueDataId = uniqueDataId;
  28. _viewModel = CommonViewModel.Instance;
  29. _line = _viewModel.GetDataLineSeries(uniqueDataId);
  30. if (_line == null)
  31. {
  32. MessageBox.Show(string.Format(DataAnalysisControl.Properties.Resource.DataSeriesExportingDialog_DataSeriesExportingDialog_zero_not_exist, uniqueDataId));
  33. }
  34. else
  35. {
  36. txt_Info.Content = string.Format(DataAnalysisControl.Properties.Resource.DataSeriesExportingDialog_DataSeriesExportingDialog_exporting,
  37. uniqueDataId,
  38. _line.DataSource.BeginTime.ToString("yyyy/MM/dd HH:mm:ss"),
  39. _line.DataSource.EndTime.ToString("yyyy/MM/dd HH:mm:ss"));
  40. txt_Completed.Content = "50%";
  41. progressBar1.Value = 50;
  42. _archieveFilePath = fileName;
  43. _thread = new Thread(new ThreadStart(Task_Export));
  44. _thread.Name = "数据集导出线程";
  45. _thread.IsBackground = true;
  46. _thread.Start();
  47. }
  48. }
  49. CommonViewModel _viewModel;
  50. string _uniqueDataId;
  51. string _archieveFilePath;
  52. Thread _thread;
  53. MyLineSeries _line;
  54. private void Task_Export()
  55. {
  56. try
  57. {
  58. var source = _line.DataSource;
  59. DataItem datas;
  60. var ret = source.GetData(_line.SeriesName, source.BeginTime, source.EndTime, out datas);
  61. if (ret)
  62. {
  63. using (System.IO.StreamWriter fs = new System.IO.StreamWriter(_archieveFilePath))
  64. {
  65. fs.Write("Time");
  66. fs.Write(",");
  67. fs.WriteLine(datas.DataName);
  68. for (int i = 0; i < datas.RawData.Count; i++)
  69. {
  70. fs.Write(datas.TimeStamp[i].ToString("yyyy/MM/dd HH:mm:ss"));
  71. fs.Write(",");
  72. fs.WriteLine(datas.RawData[i].ToString());
  73. }
  74. }
  75. MessageBox.Show(string.Format(DataAnalysisControl.Properties.Resource.DataSeriesExportingDialog_Task_Export_export_ok, _uniqueDataId, _archieveFilePath), "DataExport",
  76. MessageBoxButton.OK, MessageBoxImage.Information);
  77. }
  78. else
  79. {
  80. MessageBox.Show(string.Format(DataAnalysisControl.Properties.Resource.DataSeriesExportingDialog_Task_Export_export_failed, _uniqueDataId), "DataExport", MessageBoxButton.OK, MessageBoxImage.Error);
  81. }
  82. }
  83. catch (Exception ex)
  84. {
  85. CONTEXT.WriteLog(ex, string.Format("导出数据{0}发生异常", _uniqueDataId));
  86. MessageBox.Show(string.Format(DataAnalysisControl.Properties.Resource.DataSeriesExportingDialog_Task_Export_export_error, ex.Message), "DataExport", MessageBoxButton.OK, MessageBoxImage.Error);
  87. }
  88. finally
  89. {
  90. this.Dispatcher.BeginInvoke(new Action(() => { Close(); }));
  91. }
  92. }
  93. private void btnAbort_Click(object sender, RoutedEventArgs e)
  94. {
  95. if(_thread != null)
  96. _thread.Abort();
  97. Close();
  98. }
  99. }
  100. }