Copy program crashes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TwistedPair

    #1

    Copy program crashes

    All,
    Been trying to get a little program to work, and I think I'm really close,
    but I'm afraid I need to be pointed in the right direction. The code below
    will watch a directory for files created. If a file is created, it will
    copy it off to another directory. This even works if a bunch of files are
    created all at once, but after a few copies, the program dies for some
    reason. I suspect it's an IO issue, but I wanted to check with the experts
    here. Any ideas?

    Imports System.ServiceP rocess
    Imports System.Threadin g

    Public Class Service1
    Inherits System.ServiceP rocess.ServiceB ase
    Dim DestDirectory As String
    Dim WatchDirectory As String
    Dim SourceFilePath As String
    Dim SourceFileName As String
    Dim MyQueue As New Queue
    Dim MyRetValue As New Queue
    Private CopyF As New Thread(New System.Threadin g.ThreadStart(A ddressOf
    CopyFile))
    Private Watchservice As New Thread(New
    System.Threadin g.ThreadStart(A ddressOf StartMonitor))

    #Region " Component Designer generated code "

    Public Sub New()
    MyBase.New()

    ' This call is required by the Component Designer.
    InitializeCompo nent()

    ' Add any initialization after the InitializeCompo nent() call

    End Sub

    'UserService overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
    If Not (components Is Nothing) Then
    components.Disp ose()
    End If
    End If
    MyBase.Dispose( disposing)
    End Sub

    ' The main entry point for the process
    <MTAThread()_
    Shared Sub Main()
    Dim ServicesToRun() As System.ServiceP rocess.ServiceB ase

    ' More than one NT Service may run within the same process. To add
    ' another service to this process, change the following line to
    ' create a second service object. For example,
    '
    ' ServicesToRun = New System.ServiceP rocess.ServiceB ase () {New
    Service1, New MySecondUserSer vice}
    '
    ServicesToRun = New System.ServiceP rocess.ServiceB ase() {New
    Service1}

    System.ServiceP rocess.ServiceB ase.Run(Service sToRun)
    End Sub

    'Required by the Component Designer
    Private components As System.Componen tModel.IContain er

    ' NOTE: The following procedure is required by the Component Designer
    ' It can be modified using the Component Designer.
    ' Do not modify it using the code editor.
    <System.Diagnos tics.DebuggerSt epThrough()Priv ate Sub
    InitializeCompo nent()
    components = New System.Componen tModel.Containe r()
    Me.ServiceName = "Service1"
    End Sub

    #End Region

    Protected Overrides Sub OnStart(ByVal args() As String)
    ' Start the thread.
    Watchservice.St art()
    CopyF.Start()

    End Sub

    Protected Overrides Sub OnStop()
    ' Add code here to perform any tear-down necessary to stop your
    service.
    Watchservice.Ab ort()
    CopyF.Abort()
    End Sub
    Private Sub StartMonitor()

    WatchDirectory =
    My.Computer.Reg istry.GetValue( "HKEY_LOCAL_MAC HINE\Software\C opyMon",
    "Source", Nothing)
    DestDirectory =
    My.Computer.Reg istry.GetValue( "HKEY_LOCAL_MAC HINE\Software\C opyMon",
    "Destinatio n", Nothing)

    Dim watcher As New System.IO.FileS ystemWatcher(Wa tchDirectory)
    'Dim result
    'result = Nothing
    AddHandler watcher.Created , AddressOf logchange
    'Dim SourceFileNameO bject As IO.WaitForChang edResult
    'Dim SourceFileName As String
    'SourceFileName = SourceFileNameO bject.Name
    'MsgBox(SourceF ileName)
    watcher.Interna lBufferSize.Equ als(4000960)
    watcher.EnableR aisingEvents = True
    'result =
    watcher.WaitFor Changed(System. IO.WatcherChang eTypes.Created)

    End Sub
    Private Sub logchange(ByVal source As Object, ByVal e As
    System.IO.FileS ystemEventArgs)
    If e.ChangeType = IO.WatcherChang eTypes.Created Then
    SourceFilePath = e.FullPath
    SourceFileName = e.Name

    'MsgBox(SourceF ilePath)
    'MsgBox(DestDir ectory & "\" & SourceFileName)
    'Add files to queue to be picked off in a new sub below


    MyQueue.Enqueue (SourceFilePath )
    'MyRetValue = queue.Synchroni zed(MyQueue)
    'MsgBox(MyQueue .Count)
    End If
    End Sub
    Private Sub CopyFile()
    Do
    If MyQueue.Count 0 Then
    'MsgBox("Starti ng to copy files hopefully")
    'get files from the queue, and copy them to destination path
    Dim DestPath As String
    Dim DQedSrceFilePat h As String
    DQedSrceFilePat h = MyQueue.Dequeue ()
    DestPath = DestDirectory & "\" & SourceFileName
    My.Computer.Fil eSystem.CopyFil e(DQedSrceFileP ath, DestPath)
    End If
    Loop
    End Sub
    End Class


  • Chris Dunaway

    #2
    Re: Copy program crashes

    On Apr 1, 9:24 pm, "TwistedPai r" <twistedp...@ma il.comwrote:
    created all at once, but after a few copies, the program dies for some
    reason. I suspect it's an IO issue, but I wanted to check with the experts
    What does this mean? Are you getting an exception? If so, what is
    it? Without knowing what the exception is, it will be very difficult
    to help you.

    Chris

    Comment

    • TwistedPair

      #3
      Re: Copy program crashes

      I know, that's the problem. It just dies. I didn't think to check the
      event log until now though, and turns out that the error does show there:

      EventType clr20r3, P1 watchdirservice .exe, P2 1.0.0.0, P3 46106804, P4
      mscorlib, P5 2.0.0.0, P6 4333ab80, P7 32f8, P8 254, P9
      system.io.ioexc eption, P10 NIL.

      So it looks like the problem is IO related after all?


      "Chris Dunaway" <dunawayc@gmail .comwrote in message
      news:1175520407 .627558.307360@ b75g2000hsg.goo glegroups.com.. .
      On Apr 1, 9:24 pm, "TwistedPai r" <twistedp...@ma il.comwrote:
      >created all at once, but after a few copies, the program dies for some
      >reason. I suspect it's an IO issue, but I wanted to check with the
      >experts
      >
      What does this mean? Are you getting an exception? If so, what is
      it? Without knowing what the exception is, it will be very difficult
      to help you.
      >
      Chris
      >

      Comment

      • Chris Dunaway

        #4
        Re: Copy program crashes

        On Apr 2, 11:20 am, "TwistedPai r" <twistedp...@ma il.comwrote:
        I know, that's the problem. It just dies. I didn't think to check the
        event log until now though, and turns out that the error does show there:
        >
        EventType clr20r3, P1 watchdirservice .exe, P2 1.0.0.0, P3 46106804, P4
        mscorlib, P5 2.0.0.0, P6 4333ab80, P7 32f8, P8 254, P9
        system.io.ioexc eption, P10 NIL.
        >
        So it looks like the problem is IO related after all?
        >
        "Chris Dunaway" <dunaw...@gmail .comwrote in message
        >
        news:1175520407 .627558.307360@ b75g2000hsg.goo glegroups.com.. .
        >
        On Apr 1, 9:24 pm, "TwistedPai r" <twistedp...@ma il.comwrote:
        created all at once, but after a few copies, the program dies for some
        reason. I suspect it's an IO issue, but I wanted to check with the
        experts
        >
        What does this mean? Are you getting an exception? If so, what is
        it? Without knowing what the exception is, it will be very difficult
        to help you.
        >
        Chris
        Well, that's something, but even that doesn't help very much. A
        better way to get the exception is to add a global exception handler
        either by putting a try catch around the code in your main method or
        by handling the AppDoman.Unhand ledException event. Then you can log
        the exception to a file and find out exactly what and where it is.

        Good luck

        Chris

        Comment

        • TwistedPair

          #5
          Re: Copy program crashes

          Got it, thanks Chris, I'll give that a try. (-:

          "Chris Dunaway" <dunawayc@gmail .comwrote in message
          news:1175542943 .817245.75310@y 80g2000hsf.goog legroups.com...
          On Apr 2, 11:20 am, "TwistedPai r" <twistedp...@ma il.comwrote:
          >I know, that's the problem. It just dies. I didn't think to check the
          >event log until now though, and turns out that the error does show there:
          >>
          >EventType clr20r3, P1 watchdirservice .exe, P2 1.0.0.0, P3 46106804, P4
          >mscorlib, P5 2.0.0.0, P6 4333ab80, P7 32f8, P8 254, P9
          >system.io.ioex ception, P10 NIL.
          >>
          >So it looks like the problem is IO related after all?
          >>
          >"Chris Dunaway" <dunaw...@gmail .comwrote in message
          >>
          >news:117552040 7.627558.307360 @b75g2000hsg.go oglegroups.com. ..
          >>
          On Apr 1, 9:24 pm, "TwistedPai r" <twistedp...@ma il.comwrote:
          >created all at once, but after a few copies, the program dies for some
          >reason. I suspect it's an IO issue, but I wanted to check with the
          >experts
          >>
          What does this mean? Are you getting an exception? If so, what is
          it? Without knowing what the exception is, it will be very difficult
          to help you.
          >>
          Chris
          >
          Well, that's something, but even that doesn't help very much. A
          better way to get the exception is to add a global exception handler
          either by putting a try catch around the code in your main method or
          by handling the AppDoman.Unhand ledException event. Then you can log
          the exception to a file and find out exactly what and where it is.
          >
          Good luck
          >
          Chris
          >

          Comment

          Working...