Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 296
  • Last Modified:

Trapping for IO Exception

Hello Experts,

I have a pocket pc app that moves files to a specific folder on the network for batch printing. After the files are printed they get renamed and moved to another folder. The possibility exists the another user may try to print the same file by moving it to the print folder before the process can rename it. The files are set to read-only after printing and before the move. If that happens I get the standard message of "an unhandled exception of type IO.IOExceptions has occured in mscorlib". Is there anyway to trap for that and pause the execution of the IO.Move for a specified amount of time - say 15 seconds?

Thanks,

Jack
0
JackW9653
Asked:
JackW9653
  • 4
  • 3
  • 2
2 Solutions
 
omegaomegaDeveloperCommented:
Hello, Jack,

Are you looking for something like this?

Imports System.Threading

Public Class Form1
    Inherits System.Windows.Forms.Form

    . . .

    Private Sub Button1_Click(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) _
                                        Handles Button1.Click
        Dim booSuccess As Boolean = False

        Do
            Try
                ' Do whatever needs to be done

                Throw New IO.IOException("This is just to fake an IO error")

                booSuccess = True

            Catch ex As IO.IOException
                ' Include some sort of time-out or test
                ' here to be to be sure that IOException
                ' is not a permanent condition.
                Dim mbrAnswer As MsgBoxResult
                mbrAnswer = MsgBox("Do you want to keep trying?", _
                                    MsgBoxStyle.OKCancel + _
                                    MsgBoxStyle.Question)
                If (mbrAnswer = MsgBoxResult.Cancel) Then Exit Do

                Thread.Sleep(15000)

            Catch ex As Exception
                ' Pass other exceptions to the next level of handler.
                Throw ex

            End Try
        Loop Until (booSuccess)


    End Sub
End Class

Cheers,
Randy
0
 
JackW9653Author Commented:
Hello Omega,

Yes this is what I am looking for but there are a couple issues with your routine. What namespace is thread.sleep need? I had to comment it out to get it to compile. And after success I have to hit cancel to exit the loop. But we are close.

Thanks for your help.
0
 
Bob LearnedCommented:
System.Threading.Thread.Sleep()

Bob
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
JackW9653Author Commented:
Omega, imported System.Threading and the sleep function worked, but still need to exit the loop when the file is copied successfully. Thanks.
0
 
Bob LearnedCommented:
In order to exit the loop, you need to set booSuccess = True.

Bob
0
 
JackW9653Author Commented:
Where in the code above would that go?
0
 
Bob LearnedCommented:
I already see it in the code above, but it will never be reached:

        Try
     
          ' Do whatever needs to be done

         Throw New IO.IOException("This is just to fake an IO error")

         booSuccess = True

After the 'Throw New' line is reached it will never go past that line to the line below it.  I don't know why the 'Throw' line is even there.  I don't believe that you can take this code literally--it is just an example.  You have to modify it to fit your requirements.


Bob
0
 
JackW9653Author Commented:
Thanks Bob, I removed that line and tweaked a couple of other things and it worked.

Jack
0
 
omegaomegaDeveloperCommented:
Hello, Bob,

Many thanks for your all your assistance to the OP with this.  I was away from my bench all day.  

As you guessed, my code was intended only as an example.  The "Throw" line was there because I needed to generate an I/O error for the example, and didn't want to bother with setting up some real I/O that would fail.  

Cheers.
Randy
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now