Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loops in program causing extream CPU usage.

Status
Not open for further replies.

Sorwen

Technical User
Nov 30, 2002
1,641
US
Maybe it is just me and I'm using too many loops/nested loops, but some of my programs use too much of the CPU. Is there a better way to wait for something to happen than a loop?

For example I've got several programs that work with a telnet display (To be more specific TN5250 through a RUMBA plug-in). Each "screen" has an ID and I have to wait for the next ID so I loop (Do Until) until the screen sends back the ID I'm waiting for. Sometimes I might have an area that has another ID I have to watch for so I might have a second loop inside waiting for it, but the most is those two loops and only occasionally. When it hits any of the loops it will cause the program to use as much of the CPU that is available until the condition is met. The rest of the time it is only moderate usage. It can sometimes take several minutes before it reaches the next screen.

Any suggestions? I haven't given it much thought, but should I think about trying to figure a way to maybe use a timer instead or something with threading I haven't figure out? What is a better way to handle this?

-I hate Microsoft!
-Forever and always forward.
 
Events...Sorry, I did forget to mention some things so that I don't see how I could event it. There is a lot more to the actual code, to much to post it, but let me make this example.


Code:
'DataProcess is at collection of objects that contain Name, Address, and PhoneNumber for example
Private Sub ProcessThis(ByVal DataProcess as Collection)
   Dim CurrentData as Object

   For Each CurrentData in DataProcess
      SendToScreen(CurrentData.Name)
      SendToScreen(CurrentData.Address)
      SendToScreen(CurrentData.PhoneNumber)
      SendEnterToScreen()
      WaitForScreen(1985686)
   Next

End Sub

Private Sub WaitForScreen(ByVal ScreenID as Long)
   Do Until GetCurrentScreenID() = ScreenID
      Application.DoEvents
   Loop
End Sub
That is sort of how it goes. The any further processing has to stop until the remote computer if finished with what it does to the data sent to it. That is why I have to use the DoEvents rather than thread it. It then needs to pickup with the next set of data in DataProcess.

Sorry, I didn't say that at first. I don't see a way I could event it, but I'm also really shaky on custom events so...

-I hate Microsoft!
-Forever and always forward.
 
A timer out of the threading namespace and an event that gets raised when the scren finishes; Very simple.

Where do you check to see there is something in the other screen?

Christiaan Baes
Belgium

"My old site" - Me
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top