2012-11-11 3 views
1

이것은 Windows 서비스 응용 프로그램을 처음 작성한 것입니다. Windows 서비스 응용 프로그램을 사용하여 한 폴더에서 다른 폴더로 파일을 이동하려고합니다. 10 초마다 그렇게 할 것입니다. 이것은 내가 사용하고있는 코드입니다. 그것은 Windows 양식 응용 프로그램에서 사용할 때 작동하지만 Windows 서비스 응용 프로그램에서 사용할 때는 작동하지 않습니다.Windows 서비스의 파일 이동

Timer1_Tick의 코드는 OnStart에서 사용하면 작동합니다. 그러나 타이머에서 작동하지 않습니다.

Protected Overrides Sub OnStart(ByVal args() As String) 
     Timer1.Enabled = True 
    End Sub 

    Protected Overrides Sub OnStop() 
     Timer1.Enabled = False 
    End Sub 

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick 
     Dim FileToMove As String 
     Dim MoveLocation As String 
     Dim count1 As Integer = 0 
     Dim files() As String = Directory.GetFiles("C:\Documents and Settings\dmmc.operation\Desktop\Q8") 
     Dim pdfFiles(100) As String 

     For i = 0 To files.Length - 1 
      If Path.GetExtension(files(i)) = ".pdf" Then 
       pdfFiles(count1) = files(i) 
       count1 += 1 
      End If 
     Next 

     For i = 0 To pdfFiles.Length - 1 
      If pdfFiles(i) <> "" Then 
       FileToMove = pdfFiles(i) 
       MoveLocation = "C:\Documents and Settings\dmmc.operation\Desktop\Output\" + Path.GetFileName(pdfFiles(i)) 
       If File.Exists(FileToMove) = True Then 
        File.Move(FileToMove, MoveLocation) 
       End If 
      End If 
     Next 

    End Sub 

답변

1

폼이 인스턴스화되지 않으면 Windows.Forms.Timer가 작동하지 않습니다. 대신 System.Timers.Timer를 사용해야합니다.

Private WithEvents m_timer As System.Timers.Timer 

Protected Overrides Sub OnStart(ByVal args() As String) 
    m_timer = New System.Timers.Timer(1000) ' 1 second 
    m_timer.Enabled = True 
End Sub 

Private Sub m_timer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles m_timer.Elapsed 
    m_timer.Enabled = False 

    'Do your stuff here 

    m_timer.Enabled = True 
End Sub 
+0

사용 권한을 검사했는데 모두 정확합니다. 그런 다음 타이머없이 코드를 실행 해 보았습니다. 코드에 오류가 있다고 생각합니까? – user1648225

+0

Windows.Forms.Timer를 사용하고있는 것처럼 들립니다. Windows.Forms.Timer는 Windows 서비스에서 작동하지 않습니다 (인스턴스가 만들어지지 않습니다). System.Timers.Timer를 사용해야합니다. 샘플을 사용하여 답변을 수정하겠습니다. –

+0

코드 주셔서 감사합니다. – user1648225