none
reading file RRS feed

  • Question

  • I am reading a large binary file and converting it to ushorts.

    The program breaks before it is finished and i have click continue.

    Is there any way to instruct program to finish without break?

    I also want to add a popup progessbar that tells user file is reading and gives option to cancel request?

    code:

               byte[] bytes = File.ReadAllBytes(filePath);

               int  maximum =  bytes.Length;

               
                progressBar1.Minimum = 0;
                progressBar1.Maximum = maximum;

                for (int i = 0; i < (bytes.Length); i= i+2)
                {

                        

                    ushort value = BAToInt16(bytes, i);

                    textData = textData + value.ToString() + " ";

                    progressBar1.Value = i;//progressbar on form want to replace with popup


                }

    Thursday, October 17, 2019 1:49 PM

All replies

  • What do you mean by "it breaks"? Do you mean it throws an exception? If so then fix the error so it can continue. If errors are expected, but your app can handle them, then use a try-catch to capture the exception and programmatically handle it in your code.

    I might also recommend that you use a BinaryReader instead of doing this manually. It has a method that reads strongly typed values directly. No need to do this by hand.


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, October 17, 2019 2:10 PM
    Moderator
  • You should do the read in a BackgroundWorker and report progress from it to the window. It supports cancellation too. If you want to you can create a separate form that is shown as a modal form (using ShowDialog) in which you create the BackgroundWorker that has a progress bar and a cancel button.



    Sam Hobbs
    SimpleSamples.Info

    Thursday, October 17, 2019 3:59 PM
  • I am reading a large binary file and converting it to ushorts.

    The program breaks before it is finished and i have click continue.


               byte[] bytes = File.ReadAllBytes(filePath);

               int  maximum =  bytes.Length;


                for (int i = 0; i < (bytes.Length); i= i+2)
                {                   

                    ushort value = BAToInt16(bytes, i);

      

    You haven't shown the code in BAToInt16 but I suspect that you are converting
    the bytes at i and i + 1 to a ushort, based on the increment in the for
    statement of i+2. If so you must ensure that there actually *is* a position of
    i+1 in the bytes array. If bytes.Length is an odd number then there won't be a 
    pair of bytes at the end of the array, only a single byte. You must handle the
    case of the file having an odd length, as well as when it has an even length.

    - Wayne

    P.S. - When posting code please use the "Insert Code Block" feature of the
    forum editor.

    Thursday, October 17, 2019 5:02 PM
  • I add if statement to take of odd bytes.

    the file is  2*  1.09 MB (1,152,053 bytes).

    It is the size.

    It doesn't finish before asks for break.

    Monday, October 21, 2019 4:04 PM
  • you got multiple answers, but ignore them.
    So what do you expect from us?
    Monday, October 21, 2019 6:42 PM
  • It doesn't finish before asks for break.

    You need to be specific. Just saying "it doesn't finish", or "it breaks" gives
    us no information to analyze. It must give you a message each time - so tell
    us what it says. If possible give us the *complete* and *exact* message(s).

    If the message is in a console window, use Windows' copy & paste to capture the
    text of the message.

    If it is a popup message window then capture the image from the screen and
    include it in a post here by using the "Insert image" button on the menu bar
    of the forum editor. It should be the button at the right end of the buttons.

    - Wayne

    Monday, October 21, 2019 7:22 PM
  • Tuesday, October 22, 2019 3:27 PM
  • the progressbar is at about 80%.
    Tuesday, October 22, 2019 3:28 PM
  • Your app is throwing an exception. You aren't really providing us the information about what that exception is so we aren't going to be able to help you.

    Here's updated code.

    try
    {
    byte[] bytes = File.ReadAllBytes(filePath);
    
               int  maximum =  bytes.Length;
    
               
                progressBar1.Minimum = 0;
                progressBar1.Maximum = maximum;
    
                for (int i = 0; i < (bytes.Length); i= i+2)
                {
    
                        
    
                    ushort value = BAToInt16(bytes, i);
    
                    textData = textData + value.ToString() + " ";
    
                    progressBar1.Value = i;//progressbar on form want to replace with popup
                }
    } catch (Exception e)
    {
       MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    };
    This puts a try-catch around your code. Then set a breakpoint on that MessageBox.Show call. Then run your app and replicate the issue. When the breakpoint gets hit look at `e`. Post the Message, StackTrace and type of the exception. This will tell you exactly what is going wrong and where.


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, October 22, 2019 6:15 PM
    Moderator
  • The app is not throwing an exception.

    Break Mode offer  3 possibilities.

    When I click on continue execution the app finishes.

    Wednesday, October 23, 2019 2:07 PM
  • Break can only happen if an exception occurs (whether it is handled or not), you set a breakpoint or an assertion is fired. You don't seem to have the latter 2 in your code so the first is the only option. If this code were to be called in, say OnLoad, then the exception would be silently eaten by the runtime. 

    Show the callstack window when you're in this "break" state.


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, October 23, 2019 2:14 PM
    Moderator
  • You're getting a debug break in native code.  That means there was an assertion failure.  Doesn't the "Output" window show you a debug message?

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Wednesday, October 23, 2019 10:37 PM