Need help on time out loop after certain time.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • joaotsetsemoita@gmail.com

    #1

    Need help on time out loop after certain time.

    hello everyone.

    Im trying to time out a loot after a certain time. Probably 5 to 10
    minutes.

    I have the following function

    Private Sub processFileCrea tion(ByVal source As Object, ByVal e As
    System.IO.FileS ystemEventArgs)
    Dim strFilename As String = "c:\web\example .mdb"
    Do
    Loop While System.IO.File. Exists(strFilen ame) And
    isFileOpen(strF ilename)
    writeDatabase()

    End Sub

    This function is being called when it fires the event "created" of a
    filewatcher. The file "example.md b" is being FTPed to the folder. The
    loop is to verify if the file is completed written on disk to avoid
    exceptions. Is a big file that could take 5 minutes to FTP it that
    why I need this loop. Im trying to avoid an infinite loop if some user
    has the file open, or if the file is being used by any other software,
    so I need to time out the loop.

    I've tried to research the web how to use a timer to achieve this but
    none of the examples I found could help, plus im kind newb on vb and
    vb.net. Lets say this is my first more serious vb.net program :)

    Any sugestions?? Any help would be highly appreciated.

    Thanks in advance

    Joao

  • rowe_newsgroups

    #2
    Re: Need help on time out loop after certain time.

    On Oct 4, 7:33 am, joaotsetsemo... @gmail.com wrote:
    hello everyone.
    >
    Im trying to time out a loot after a certain time. Probably 5 to 10
    minutes.
    >
    I have the following function
    >
    Private Sub processFileCrea tion(ByVal source As Object, ByVal e As
    System.IO.FileS ystemEventArgs)
    Dim strFilename As String = "c:\web\example .mdb"
    Do
    Loop While System.IO.File. Exists(strFilen ame) And
    isFileOpen(strF ilename)
    writeDatabase()
    >
    End Sub
    >
    This function is being called when it fires the event "created" of a
    filewatcher. The file "example.md b" is being FTPed to the folder. The
    loop is to verify if the file is completed written on disk to avoid
    exceptions. Is a big file that could take 5 minutes to FTP it that
    why I need this loop. Im trying to avoid an infinite loop if some user
    has the file open, or if the file is being used by any other software,
    so I need to time out the loop.
    >
    I've tried to research the web how to use a timer to achieve this but
    none of the examples I found could help, plus im kind newb on vb and
    vb.net. Lets say this is my first more serious vb.net program :)
    >
    Any sugestions?? Any help would be highly appreciated.
    >
    Thanks in advance
    >
    Joao
    Probably the simplest way would be to execute the method that does the
    loop in a seperate thread, and then use the Join method to wait for it
    to finish or a timeout to expire.

    Here's a sample console project demonstrating the process:

    /////////////////////
    Imports System.Threadin g

    Module Module1

    Sub Main()

    Console.WriteLi ne("Starting thread...")

    Dim t As New Thread(AddressO f DoTheWork)
    t.Start()

    Console.WriteLi ne("Thread starting successfully")

    '// Do anything else you might need to do
    '// while the thread is running
    Console.WriteLi ne("Other work is being done...")

    '// Block until thread completion or timeout (in milliseconds)
    t.Join(2000)

    '// Check to see if the timeout expired or
    '// the thread finished
    If t.IsAlive Then
    Console.WriteLi ne("Timeout expired, aborting thread...")
    '// Kill the thread
    t.Abort()
    Else
    Console.WriteLi ne("Thread completed successfully")
    End If

    Console.Read()

    End Sub

    Public Sub DoTheWork()
    Try
    '// Create an infinite loop
    While True
    Console.WriteLi ne(" Thread is working...")
    '// Pretend we are working
    Thread.Sleep(25 0)
    End While
    Catch ex As ThreadAbortExce ption
    '// Do any cleanup here
    Console.WriteLi ne("Thread Aborted")
    End Try
    End Sub

    End Module
    /////////////////////

    Thanks,

    Seth Rowe

    Comment

    • Miro

      #3
      Re: Need help on time out loop after certain time.



      joaotsetsemoita @gmail.com wrote:
      hello everyone.
      >
      Im trying to time out a loot after a certain time. Probably 5 to 10
      minutes.
      >
      I have the following function
      >
      Private Sub processFileCrea tion(ByVal source As Object, ByVal e As
      System.IO.FileS ystemEventArgs)
      Dim strFilename As String = "c:\web\example .mdb"
      Do
      Loop While System.IO.File. Exists(strFilen ame) And
      isFileOpen(strF ilename)
      writeDatabase()
      >
      End Sub
      >
      This function is being called when it fires the event "created" of a
      filewatcher. The file "example.md b" is being FTPed to the folder. The
      loop is to verify if the file is completed written on disk to avoid
      exceptions. Is a big file that could take 5 minutes to FTP it that
      why I need this loop. Im trying to avoid an infinite loop if some user
      has the file open, or if the file is being used by any other software,
      so I need to time out the loop.
      >
      I've tried to research the web how to use a timer to achieve this but
      none of the examples I found could help, plus im kind newb on vb and
      vb.net. Lets say this is my first more serious vb.net program :)
      >
      Any sugestions?? Any help would be highly appreciated.
      >
      Thanks in advance
      >
      Joao
      >
      If you dont want to use a thread, maybe just create a timer, set it to
      10 min or whatever on the interval, and do something like this
      <code written in notepad>

      dim strfilename as .....
      dim myTimeOutTimer as new timer
      myTimeOutTimer. Interval = 10000 '(Example Time - change it)
      myTimeOutTimer. Enabled = True

      do
      <your code here>
      loop while <all ur stuffand myTimeOutTimer. Enabled = True


      and in the timer event...just put myTimerOutTimer .Enabled = false.


      fyi im a newbie.

      M.

      Comment

      • joaotsetsemoita@gmail.com

        #4
        Re: Need help on time out loop after certain time.

        On Oct 4, 2:44 pm, Miro <miron...@beero .netwrote:
        joaotsetsemo... @gmail.com wrote:
        hello everyone.
        >
        Im trying to time out a loot after a certain time. Probably 5 to 10
        minutes.
        >
        I have the following function
        >
        Private Sub processFileCrea tion(ByVal source As Object, ByVal e As
        System.IO.FileS ystemEventArgs)
        Dim strFilename As String = "c:\web\example .mdb"
        Do
        Loop While System.IO.File. Exists(strFilen ame) And
        isFileOpen(strF ilename)
        writeDatabase()
        >
        End Sub
        >
        This function is being called when it fires the event "created" of a
        filewatcher. The file "example.md b" is being FTPed to the folder. The
        loop is to verify if the file is completed written on disk to avoid
        exceptions. Is a big file that could take 5 minutes to FTP it that
        why I need this loop. Im trying to avoid an infinite loop if some user
        has the file open, or if the file is being used by any other software,
        so I need to time out the loop.
        >
        I've tried to research the web how to use a timer to achieve this but
        none of the examples I found could help, plus im kind newb on vb and
        vb.net. Lets say this is my first more serious vb.net program :)
        >
        Any sugestions?? Any help would be highly appreciated.
        >
        Thanks in advance
        >
        Joao
        >
        If you dont want to use a thread, maybe just create a timer, set it to
        10 min or whatever on the interval, and do something like this
        <code written in notepad>
        >
        dim strfilename as .....
        dim myTimeOutTimer as new timer
        myTimeOutTimer. Interval = 10000 '(Example Time - change it)
        myTimeOutTimer. Enabled = True
        >
        do
        <your code here>
        loop while <all ur stuffand myTimeOutTimer. Enabled = True
        >
        and in the timer event...just put myTimerOutTimer .Enabled = false.
        >
        fyi im a newbie.
        >
        M.- Hide quoted text -
        >
        - Show quoted text -
        I would like to thank to both of you who posted messages to help me.

        I follow Seth's sugestion and fitted perfectly for what I wanted. Just
        finished now my first windows service.

        thanks once again

        Joao

        Comment

        • Miro

          #5
          Re: Need help on time out loop after certain time.

          joaotsetsemoita @gmail.com wrote:
          On Oct 4, 2:44 pm, Miro <miron...@beero .netwrote:
          >joaotsetsemo.. .@gmail.com wrote:
          >>hello everyone.
          >>Im trying to time out a loot after a certain time. Probably 5 to 10
          >>minutes.
          >>I have the following function
          >>Private Sub processFileCrea tion(ByVal source As Object, ByVal e As
          >>System.IO.Fil eSystemEventArg s)
          >> Dim strFilename As String = "c:\web\example .mdb"
          >> Do
          >> Loop While System.IO.File. Exists(strFilen ame) And
          >>isFileOpen(st rFilename)
          >> writeDatabase()
          >>End Sub
          >>This function is being called when it fires the event "created" of a
          >>filewatcher . The file "example.md b" is being FTPed to the folder. The
          >>loop is to verify if the file is completed written on disk to avoid
          >>exceptions. Is a big file that could take 5 minutes to FTP it that
          >>why I need this loop. Im trying to avoid an infinite loop if some user
          >>has the file open, or if the file is being used by any other software,
          >>so I need to time out the loop.
          >>I've tried to research the web how to use a timer to achieve this but
          >>none of the examples I found could help, plus im kind newb on vb and
          >>vb.net. Lets say this is my first more serious vb.net program :)
          >>Any sugestions?? Any help would be highly appreciated.
          >>Thanks in advance
          >>Joao
          >If you dont want to use a thread, maybe just create a timer, set it to
          >10 min or whatever on the interval, and do something like this
          ><code written in notepad>
          >>
          >dim strfilename as .....
          >dim myTimeOutTimer as new timer
          >myTimeOutTimer .Interval = 10000 '(Example Time - change it)
          >myTimeOutTimer .Enabled = True
          >>
          >do
          ><your code here>
          >loop while <all ur stuffand myTimeOutTimer. Enabled = True
          >>
          >and in the timer event...just put myTimerOutTimer .Enabled = false.
          >>
          >fyi im a newbie.
          >>
          >M.- Hide quoted text -
          >>
          >- Show quoted text -
          >
          I would like to thank to both of you who posted messages to help me.
          >
          I follow Seth's sugestion and fitted perfectly for what I wanted. Just
          finished now my first windows service.
          >
          thanks once again
          >
          Joao
          >

          Seth has been using vb waaaay longer than me. Take his examples over
          mine anyday!

          cheers'

          M.

          Comment

          Working...