interrupting calculations

  • Thread starter Thread starter andreas
  • Start date Start date
A

andreas

When I do a long calculation is there a possibility to interrupt this
calculation properly?

For i as integer = 1 to 100000000
do something
next

and retrieve the i at the moment of the interrupting

Thanks for any response
 
Do you mean this:

dim iAfterLoop as integer

for i as integer =1 to 10000000

if i = something then
iAfterloop = i
end for
end if
next
 
Hello Andreas,

Ah, I misunderstood as well. If yer looking for how to interrupt at form
close, perhaps for the purpose of continuing the long op at the next run..
it's pretty simple.

Im the Form_QueryClose event set a varaible.. like bFormClosing to True..
Then in the loop ask the variable if the form is closing.. and if it is,
exit the op. (Exit For).

-Boo
 
Note that this aproach will only succeed if the loop is started with a
asynchronous delegate

Otherwise the form will simply freeze untill the loop has completed and
pressing the close button will result in a " This program is not responding
message "

But for the rest you are right you can then set a boolean flag to jump out
of the loop and let the function or a property setter return the value to
the initial caller

regards

Michel Posseth [MCP]
 
Example

Public Class Form1
Private Mvar_Cancell As Boolean
Friend Property Cancell() As Boolean
Get
Return Mvar_Cancell
End Get
Set(ByVal value As Boolean)
Mvar_Cancell = value
End Set
End Property
Delegate Sub AsynchMethodInvoker()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim AsyncProcess As New AsynchMethodInvoker(AddressOf Me.DoSomething)
AsyncProcess.BeginInvoke(Nothing, Nothing)
End Sub
Private i As Integer
Private Sub DoSomething()
For i = 1 To 100000000
'do your stuff here
If Me.Cancell Then
Exit For
End If
Next
End Sub
Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
Me.Cancell = True
MsgBox("The value of i =" & i.ToString)
End Sub
End Class


regards

Michel Posseth [MCP]






Michel Posseth said:
Note that this aproach will only succeed if the loop is started with a
asynchronous delegate

Otherwise the form will simply freeze untill the loop has completed and
pressing the close button will result in a " This program is not responding
message "

But for the rest you are right you can then set a boolean flag to jump out
of the loop and let the function or a property setter return the value to
the initial caller

regards

Michel Posseth [MCP]






GhostInAK said:
Hello Andreas,

Ah, I misunderstood as well. If yer looking for how to interrupt at form
close, perhaps for the purpose of continuing the long op at the next run..
it's pretty simple.

Im the Form_QueryClose event set a varaible.. like bFormClosing to True..
Then in the loop ask the variable if the form is closing.. and if it is,
exit the op. (Exit For).

-Boo
 
Hello Michel Posseth [MCP],

The loop could alternatively massage the message pump to keep a responsive
UI.

-Boo

Note that this aproach will only succeed if the loop is started with a
asynchronous delegate

Otherwise the form will simply freeze untill the loop has completed
and pressing the close button will result in a " This program is not
responding message "

But for the rest you are right you can then set a boolean flag to jump
out of the loop and let the function or a property setter return the
value to the initial caller

regards

Michel Posseth [MCP]

Hello Andreas,

Ah, I misunderstood as well. If yer looking for how to interrupt at
form close, perhaps for the purpose of continuing the long op at the
next run.. it's pretty simple.

Im the Form_QueryClose event set a varaible.. like bFormClosing to
True.. Then in the loop ask the variable if the form is closing.. and
if it is, exit the op. (Exit For).

-Boo
 
The loop could alternatively massage the message pump to keep a responsive
UI.

and so wasting proces cycles ?

However you are right it could work ,,, personally i would prefer ofcourse
my example code wich has the highest performance and is most flexible

regards

Michel Posseth [MCP]


GhostInAK said:
Hello Michel Posseth [MCP],

The loop could alternatively massage the message pump to keep a responsive
UI.

-Boo

Note that this aproach will only succeed if the loop is started with a
asynchronous delegate

Otherwise the form will simply freeze untill the loop has completed
and pressing the close button will result in a " This program is not
responding message "

But for the rest you are right you can then set a boolean flag to jump
out of the loop and let the function or a property setter return the
value to the initial caller

regards

Michel Posseth [MCP]

Hello Andreas,

Ah, I misunderstood as well. If yer looking for how to interrupt at
form close, perhaps for the purpose of continuing the long op at the
next run.. it's pretty simple.

Im the Form_QueryClose event set a varaible.. like bFormClosing to
True.. Then in the loop ask the variable if the form is closing.. and
if it is, exit the op. (Exit For).

-Boo

No, Ahmed I am thinking at the event of closing my form.

Do you mean this:

dim iAfterLoop as integer

for i as integer =1 to 10000000

if i = something then
iAfterloop = i
end for
end if
next
Ahmed
andreas wrote:
When I do a long calculation is there a possibility to interrupt
this calculation properly?

For i as integer = 1 to 100000000
do something
next
and retrieve the i at the moment of the interrupting
Thanks for any response
 
Back
Top