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
JackW9653Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.