Solved

Help with stop - start of a loop

Posted on 2008-06-14
6
217 Views
Last Modified: 2010-04-24
Hi All

I have the following attached loop that runs through 300+ mil loops generated from 3 Lists of string.

District, inner & ending

Is there any way to save the current position from the "For each .. as string in .." and then load the position back in ? Just that I have an estimated run time of 500+ hours and would like to be able to resume from a stored position should I have to.

I am also opened to other ideas aswell :-)

Many Thanks
Steve

Dim District, Inner, Ending As New List(Of String)

 

        District = DistrictList()

        Inner = InnerList()

        Ending = EndingList()

 

        For Each d1 As String In District

            s1 = d1

            For Each i1 As String In Inner

                s2 = s1 & i1

                For Each e1 As String In Ending

                    s3 = s2 & " " & e1

 

                    While pauseLoop

                        System.Threading.Thread.Sleep(100)

                        Application.DoEvents()

                    End While

 

                    Application.DoEvents()

 

                Next

            Next

        Next

Open in new window

0
Comment
Question by:Stephen Manderson
  • 3
  • 2
6 Comments
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 100 total points
ID: 21785330
let's assume that the Distrinctlist, InnerList and EndingList are sorted (if not, that will be "required")

then, you have only to check if the loop's control variable is after the starting value OR if the outer loops are on the next level (in which case all inner loops have to be processed)
Dim District, Inner, Ending As List(Of String)
 

Dim startDistrict As String = "" 'set the district to start processing from, leave empty to do it all

Dim startInner As String = "" 'idem

Dim startEnding As String = "" 'idem

 

        District = DistrictList()

        Inner = InnerList()

        Ending = EndingList()

 

        For Each d1 As String In District

         If d1 >= startDistrict Then

            s1 = d1

            For Each i1 As String In Inner

              if i1 >= startInner OR d1 > startDistrict then

                s2 = s1 & i1

                For Each e1 As String In Ending

                  if e1 >= startEnding or d1 > startDistrict or i1 > startInner then

                    s3 = s2 & " " & e1

 

                    While pauseLoop

                        System.Threading.Thread.Sleep(100)

                        Application.DoEvents()

                    End While

 

                    Application.DoEvents()

                  end if 'if e1 >= startEnding or d1 > startDistrict or i1 > startInner then

                Next 'e1 As String In Ending

              End if ' if i1 >= startInner OR d1 > startDistrict then

            Next 'i1 As String In Inner

          End if ' If d1 >= startDistrict Then

        Next 'd1 As String In District

Open in new window

0
 
LVL 34

Accepted Solution

by:
Sancler earned 400 total points
ID: 21786472
Or use an index rather than for each and store the indices.  Something like this

Private Sub ProcessLists(dStart As Long, iStart As Long, eStart As Long)
  Dim dIndex, iIndex, eIndex, dEnd, iEnd, eEnd As Long
  dEnd = District.Count - 1
  iEnd = Inner.Count - 1
  eEnd = Ending.Count - 1
  For dIndex = dStart to dEnd
      s1 = District(dIndex)
      For iIndex = iStart to iEnd
          s2 = s1 & Inner(iIndex)
          For eIndex = eStart to eEnd
              s3 = s2 & " " & Ending(eIndex)
              'do stuff
              If stopping Then
                 'save dIndex, iIndex, eIndex + 1
                 'may need to check if eIndex = eEnd and, if so
                 'save iIndex + 1 and 0 for eIndex
                 Exit Sub
              End If
          Next
          eStart = 0
      Next
      iStart = 0
  Next
End Sub

At the beginning call the sub with 0,0,0.  On restarting call it with the saved values.

Roger
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21786646
>Sancler
your code has the flaw that when you start with 0,5,0, it will skip the values for the second loop from 0..4 for ALL the value of the first loop... which is no good :)
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 34

Expert Comment

by:Sancler
ID: 21786825
angelIII

What about the third line from the end

     iStart = 0

Before dIndex goes from 0 to 1, iStart is reset to 0.

I must admit I haven't tested it, though.  Do you still reckon there's a flaw?

Roger
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21787557
oooops, I overlooked that! I stand corrected!
0
 
LVL 19

Author Closing Comment

by:Stephen Manderson
ID: 31467182
Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem with Filesystemobject or .NET version 3 34
How to use odbc in vb to connect to ms sql 14 34
Javascript and Jquery not firing 9 39
VS.net 2010 11 22
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now