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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HTA - Outputing text to a span area *during* script execution 1

Status
Not open for further replies.

BenChristian

IS-IT--Management
Jan 11, 2004
193
AU
I'm putting togehter an HTA and have come across an issue. The script won't update the actual HTA (a span area) until it has finished.

I've listed a simple example below. I would like to display the text in the span area before the script finishes, but as you will see if you run the HTA, the span area doesn't actually update until the sciprt finishes. If I place a msgbox function between updating the span area and starting the Do.. Loop then the span area is updated immediately (which seems to be because the script has been interrupted).

Code:
<html>
<head>
<Title>Test HTA Application</Title>
<HTA:APPLICATION
   ID = "HTAapp"
   APPLICATIONNAME = "Test HTA Application"
   BORDER = "thick"
   CAPTION = "yes"
   ICON = "app.ico"
   SHOWINTASKBAR = "yes"
   SINGLEINSTANCE = "yes"
   SYSMENU = "yes"
   WINDOWSTATE = "normal"
   SCROLL = "no"
   SCROLLFLAT = "yes"
   VERSION = "1.0"
   INNERBORDER = "yes"
   SELECTION = "no"
   MAXIMIZEBUTTON = "yes"
   MINIMIZEBUTTON = "yes"
   NAVIGABLE = "yes"
   CONTEXTMENU = "yes"
   BORDERSTYLE = "normal"
   >
</head>

<script language="vbscript">

Sub btnStart_onClick

	'Output to the HTA by updating the "testpan" SPAN area
	testspan.innerhtml = "Test output before Do.. Loop"
	
	'Create a delay by using a loop.  There's no sleep command with vbscript inside an HTA
	strResume = Second(now()) + strDelay
	
	Do until Second(Now()) > strResume
	Loop		

	msgbox "End of Do.. Loop routine"

End Sub

</script>

<body>
<INPUT TYPE="Button" NAME="btnStart" VALUE="Start"><br>
<span id = "testspan"></span>
</body>
<html>

The HTA that I am writing uses a For Next loop, and for each iteration of the loop I want to update the span area with something lile "processing line 1 of 989". I have the same issue if I try and upate a text area.

I'd really appreciate any input or suggestions, I've been stuck on this issue for a few days now!

Many thanks.

 
[tt]
sub dothing (n)
if second(now())>cint(n) then
msgbox "End of Do.. Loop routine"
else
setTimeout "dothing('" & n & "')",100
end if
end sub

Sub btnStart_onClick

'Output to the HTA by updating the "testpan" SPAN area
testspan.innerhtml = "Test output before Do.. Loop"

'Create a delay by using a loop. There's no sleep command with vbscript inside an HTA
strDelay=3 'op script has no strDelay assigned
strResume = Second(now()) + strDelay
setTimeout "dothing('" & strResume & "')",100
[red]'[/red]Do until Second(Now()) > strResume
[red]'[/red]Loop
[red]'[/red]msgbox "End of Do.. Loop routine"
End Sub
[/tt]
 
Thanks tsuji for your reply. I had accidently left out the strDelay variable (I was testing something and didn't set it back properly).

I tested your example and it works (thanks) but I'd appreciate some further explaination. Could you please explain how you are acheiving this using the setTimeout function?

It seems that any type of loop causes the delayed output (do.. loop, for.. next, while.. wend) which is why your example works?

I need to read through an array and update the span area for each line that I process. Reading through the array uses a For.. Next statement and I don't think I can avoid that.

I appreciate your help!

Thanks.
Ben.

 
[1] Without setting strDelay, you have the fortunate result at least it does not hang the application. A loop burning time can never be scripted that way. It takes up all the cpu cycle.

[2] Time in the browser is some delicate thing to accomodate if timing is the essence. Sometimes, you bave to branch out calculation intensive with a time delay in order to flush out the writing to other control. Another calculation intensive for illustration.
[tt]
Sub btnStart_onClick
testspan.innerhtml = "Test output before Do.. Loop " & now()
setTimeout "dothing2",10
End Sub
sub dothing2
dim i,n
for i=1 to 500000
n=sqr(i) mod 13
next
msgbox "End of Do.. Loop routine"
end sub
[/tt]
Small time delay to branch out to dothing2 is critical.
 
Thanks. I only inserted the delay to simulate the script doing something. I have posted the actual script that I am working on below. I would like to update the span area each time it reads a line from the dictionary array so that the user can monitor the progress. I will be adding more code to actually manipulate the file that it's working with but for now I'm just trying to get this part working.

You can use any large text file to read through (around 10,000 lines is what I 'll be working with). The script will append the file name to add .bak to the end.

Code:
<html>
<head>
<Title>Remove Break Bulk</Title>
<HTA:APPLICATION
   ID = "oApp"
   APPLICATIONNAME = "Simple About Box 2001"
   BORDER = "thick"
   CAPTION = "yes"
   ICON = "app.ico"
   SHOWINTASKBAR = "yes"
   SINGLEINSTANCE = "yes"
   SYSMENU = "yes"
   WINDOWSTATE = "normal"
   SCROLL = "yes"
   SCROLLFLAT = "yes"
   VERSION = "1.0"
   INNERBORDER = "yes"
   SELECTION = "no"
   MAXIMIZEBUTTON = "yes"
   MINIMIZEBUTTON = "yes"
   NAVIGABLE = "yes"
   CONTEXTMENU = "yes"
   BORDERSTYLE = "normal"
   >
</head>
<body bgcolor="#DFDFDF">
 


  <p><font face="Tahoma" size="2">File:</font></font>
	</b>
	<INPUT TYPE="Text Box" length="20" NAME="txtFile" size="50"> <INPUT TYPE="Button" NAME="btnBrowse" VALUE="...">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <INPUT TYPE="Button" NAME="btnStart" VALUE="Start"><p>
	<br>
	<font face="Tahoma" size="2">Process Log:</font><p>
	<span id="LineProgress"></span><br>
	<textarea name="txtLog" rows="6" cols="53" wrap="physical"></textarea></font><p>
	<font face="Tahoma" size="2">Breakbulk:</font>
	<br>
	<textarea name="BBList" rows="22" cols="53" wrap="physical"></textarea>
 

</body>

<script language=vbscript>
Dim FileName
Dim iTimer
Const ForWriting = 2
Const ForReading = 1

Sub btnBrowse_OnClick()
	on Error Resume Next
	Set objDialog = CreateObject("UserAccounts.CommonDialog")

	objDialog.Filter = "All Files|*.*"
	objDialog.FilterIndex = 1
	objDialog.InitialDir = "C:\Scripts\breakbulk"
	intResult = objDialog.ShowOpen
 
	If intResult <> 0 Then
		FileName = objDialog.FileName
	    txtFile.value = FileName
	End If

End Sub

Sub btnStart_OnClick()

	'On Error Resume Next
	
	'Rename file to .bak
	SourceFileName = FileName & ".bak"
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	objFSO.MoveFile FileName , SourceFileName
	If err = 0 then
		txtLog.value = "Renamed original file to " & SourceFileName
	Else
		txtLog.value = "Error accessing or renaming the source file"
		Exit Sub
	End If
	err.Clear
	
	Set objSourceFile = objFSO.OpenTextFile(SourceFileName, ForReading, True)
	Set objDestFile = objFSO.OpenTextFile(FileName, ForWriting, True)

	Set objDictSourceFile = CreateObject("Scripting.Dictionary")
	
	LineNumber = 0
	Do Until objSourceFile.AtEndOfStream
		CurrentLine = objSourceFile.Readline
		LineNumber = LineNumber + 1
    	objDictSourceFile.Add LineNumber, CurrentLine
    Loop

	LineCount = objDictSourceFile.Count	
	
	For each line in objDictSourceFile
	
		LineProgress.innerHTML = "Processing Line " & Line & " of " & LineCount
		'The script will be doing more within this For Next loop, for now I'm just working
		'on getting the progress output working.
	Next	

msgbox "Done"

End Sub

</script>
</html>

 
you can use a "cumulative message" that is built one line at a time but displays previous lines, making it seem that it's doing one line at a time.

msg = msg & "processing line 1 of 955"
display message
msg = msg & "processing line 2 of 955"
display msg
the result will look line this:

processing line 1 of 955
processing line 2 of 955
 
it's not a command. i just put it there as pseudo code.
the actual command is:
DataArea.InnerHTML = msg
 
Hi Barny,

I think you've misunderstood my issue. The HTA isn't updating the span area until the code finishes.

Code:
    For each line in objDictSourceFile
    
        LineProgress.innerHTML = "Processing Line " & Line & " of " & LineCount

    Next    

msgbox "Done"

The span area updates at the same time as the msgbox is produced. I want the span area to update with each iteration of the For.. Next loop.

 
This is a very simple little HTA, but it does what you're wanting it to do. The 10 in the set Interval is a delay in miliseconds. Basically, this works by throwing a delay between each run of Sub Looping. Once i is larger than the array, it stop the Sub Looping.
Code:
<HEAD>
<TITLE>Loading REQ77</TITLE>
<HTA:APPLICATION
  WindowsState = "normal"
/>
<script type="text/vbscript">

Dim i, iTimerID
Dim YourArray(50)
Sub window_OnLOad
  i = 0
  iTimerID = window.setInterval("Looping",10)
End Sub

Sub Looping
  If i <= UBound(YourArray) Then
    Junk.InnerText = "Proccessing " & Cstr(i+1)
    'Code for whatever you're doing with the array here
    i = i + 1
  Else
    window.clearInterval(iTimerID)
  End If
End Sub
</script></HEAD>


<body>
<span id="Junk"></span>
</body>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top