Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void importDataToolStripMenuItem_Click(object sender, EventArgs e)
- {
- openFileDialog.Filter = datFilter;
- if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- data = new DataTable();
- ThreadStart s = delegate { getData(openFileDialog.FileName); };
- Thread t = new Thread(s);
- t.Start();
- }
- }
- private void updateProgressBar(object[] args)
- {
- try
- {
- if (this.InvokeRequired)
- {
- DelegateMethod delegateMethod = new DelegateMethod(updateProgressBar);
- this.Invoke(delegateMethod, new object[] { args });
- }
- else
- {
- if ((int)args[0] > 0)
- {
- toolStripProgressBar.Value = (int)args[0];
- }
- else
- {
- toolStripProgressBar.Value = toolStripProgressBar.Maximum;
- }
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, CONST_STRINGS.MSGBOX_HEADER_ERROR);
- }
- }
- private void getData(string filepath)
- {
- try
- {
- if (File.Exists(filepath))
- {
- dataReadStopWatch.Start();
- BinaryReader bReader = new BinaryReader(File.Open(filepath, FileMode.Open));
- setupProgressBar(new object[] { 0, (int)bReader.BaseStream.Length });
- int columnCount = bReader.ReadInt32();
- data.Columns.Add(CONST_STRINGS.DATA_COLUMN_HEADER_DATETIME);
- for (int i = 0; i < columnCount; i++)
- {
- data.Columns.Add(new string(bReader.ReadChars(bReader.ReadInt32())));
- updateProgressBar(new object[] { (int)bReader.BaseStream.Position });
- }
- bool eof = false;
- while (!eof)
- {
- try
- {
- List<object> RowData = new List<object>();
- RowData.Add(bReader.ReadInt32());
- for (int i = 0; i < columnCount; i++)
- {
- RowData.Add(bReader.ReadSingle().ToString());
- }
- data.Rows.Add(RowData.ToArray());
- updateProgressBar(new object[] { (int)bReader.BaseStream.Position });
- }
- catch (Exception ex)
- {
- eof = true;
- }
- }
- }
- else
- {
- throw new Exception(CONST_STRINGS.ERROR_IMPORT_FILEPATH_INVALID);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, CONST_STRINGS.MSGBOX_HEADER_ERROR);
- }
- finally
- {
- updateProgressBar(new object[] { -1 });
- dataReadStopWatch.Stop();
- Application.DoEvents();
- dataReadExecutionTime = ((float)dataReadStopWatch.ElapsedMilliseconds / 1000);
- MessageBox.Show(String.Format(CONST_STRINGS.DATA_IMPORT_SUCCESSFUL, data.Rows.Count, dataReadExecutionTime), CONST_STRINGS.MSGBOX_HEADER_DI);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement