02-11-2022
12:22 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
02-11-2022
12:22 PM
This question looks nice, but is very hard to implement. Because in some cases is not possible to interrupt the execution. For example you can not interrupt execution inside transaction. More info.
You can implement the similar functionality using ProgressBar. See the following example
Sub Main
Dim oSketetch As Sketch = ThisDoc.Document.ActivatedObject
Dim linesCount = 1000
DrawLinesInSketch(oSketetch, linesCount)
End Sub
Private progressBar As progressBar
Private cancelRequested As Boolean
Sub DrawLinesInSketch(oSketetch As Sketch, linesCount As Integer)
'Initialze progress bar
progressBar = ThisApplication.CreateProgressBar(False, linesCount, "Drawing lines", True)
AddHandler progressBar.OnCancel, AddressOf ProgressBar_OnCancel
'Draw lines to sketch
For i As Integer = 1 To linesCount
'Check if cancel opration is requested
If cancelRequested Then
Exit For
End If
progressBar.UpdateProgress()
oSketetch.SketchLines.AddByTwoPoints(
ThisApplication.TransientGeometry.CreatePoint2d(0, i),
ThisApplication.TransientGeometry.CreatePoint2d(i, 0)
)
Next
'Cleanup progress bar
RemoveHandler progressBar.OnCancel, AddressOf ProgressBar_OnCancel
progressBar.Close()
'Display final messageBox
If cancelRequested Then
MsgBox("Canceled", MsgBoxStyle.Exclamation)
Else
MsgBox("Done", MsgBoxStyle.Information)
End If
End Sub
Private Sub ProgressBar_OnCancel()
cancelRequested = True
End Sub
For more complex task, which doesn't need Inventor for full time, you can use BackgroundWorker and use another thread for computing.