App crashes when reading file
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: App crashes when reading file

  1. #1
    Join Date
    May 2014
    Posts
    6

    App crashes when reading file

    Hi, all.

    I have an Excel macro (in VBA) that has data that changes. When the data changes (data being written to a cell), then it is saved and duplicated to a .csv file. Works great!

    The problem I am having is; in my C# progarm, I have a timer that checks the data every 10 seconds, and by chance, if a file is being saved/written, and at the same time my C# program is trying to read...it crashes.

    Is there something to prevent me from crashing? A way to read the file only even if it's being written to? Open it in read only, or? Some kind of help or guidance would be great!

    Code:
            public MainWindow()
            {
                this.InitializeComponent();
                timer = new DispatcherTimer();
                timer.Interval = new TimeSpan(0, 0, 0, 11);
                timer.Tick += timer_Tick;
                timer.Start();
    
                Fuel = 0;
                Speed = 0;
                RPM = 0;
                Temperature = 0;
                _torque = 0;
                Torque = 0;
                this.DataContext = this;
            }
    
            void timer_Tick(object sender, object e)
            {
                // Read data from C:\DesalData\
    
                string line;
                System.IO.StreamReader file = new System.IO.StreamReader("C:\\DesalData\\Data.csv");
    
                // Read first line and ignore
                line = file.ReadLine();
                // Data
                line = file.ReadLine();
                List<string> data = line.Split(',').ToList<string>();
                file.Close();
    
                // End data from C:\Desal\Data
    
                // Read data from C:\DesalData\Voltage
    
                string line2;
                System.IO.StreamReader file2 = new System.IO.StreamReader("C:\\DesalData\\Voltage.csv");
    
                // Read first line and ignore
                line2 = file2.ReadLine();
                // Data
                line2 = file2.ReadLine();
                List<string> data2 = line2.Split(',').ToList<string>();
                file2.Close();
    
                // End data from C:\Desal\Voltage
    
                // MessageBox.Show(data[1]);
    
                Fuel = Convert.ToDouble(data2[1]);
                Speed = Convert.ToDouble(data[1]);
                RPM = Convert.ToDouble(data[5]);
                Temperature = Convert.ToDouble(data[3]);
                Torque = Convert.ToDouble(data[7]);
            }

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,101

    Re: App crashes when reading file

    Maybe this could help :

    Code:
    FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, System.IO.FileShare.ReadWrite).
    This allows a file being written to whilst being read by another outside process via the use of FileShare

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center