I've some function in my command which calculating lots of data on dwg file.
I would like to show to user a WinForm with progress value. From the command I'm incrementing value of ProgressBar.
The problem is that my ProgressBar is incrementing to the 100% after done all the job - suddendly.
I think I can use some threading or backgroundWorker but it's hard for me and I didn't find any examples.
Can anyone help me?
It simple and straightforwaord approch of showing progress bar on a form is to move the long processing logic into Form's code behind. However, this reduces code maintenanbility because it mixes the UI and data process code. This post might be useful to you:
AutoCAD has a built-in progress bar feature, have you looked at that?
if you don't need the UI to be responsive during the process (you don't have a Stop button, for example) then the easiest approach is moving the calculation to the UI thread
If you need the UI to be responsive, then look at this class http://msdn.microsoft.com/en-us/library/system.com
I have just done what you want and it works!
First you have to init the min and max value of your ProgressBar, for instance iMin=0 and iMax=object.count
Then in your loop just add a line which increment the i value of the ProgressBar.
And the most essential is to upgrade your active form with the "Refresh" method.
'ProgressBar init' Dim iMin As Long = 0 Dim iMax As Long 'Object Count' Using acTrans = acDocs.MdiActiveDocument.Database.TransactionManag
er.StartTransaction() acBlkTbl = acTrans.GetObject(acDocs.MdiActiveDocument.Databas e.BlockTableId, OpenMode.ForRead) acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelS pace), OpenMode.ForRead) For Each acObjId As ObjectId In acBlkTblRec imax=iMax+1 Next acObjId End Using ProgressBar1.Minimum= iMin ProgressBar1.Maximum= iMax 'Iterate Through objects' Dim i as Long Using acTrans = acDocs.MdiActiveDocument.Database.TransactionManag er.StartTransaction() acBlkTbl = acTrans.GetObject(acDocs.MdiActiveDocument.Databas e.BlockTableId, OpenMode.ForRead) acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelS pace), OpenMode.ForRead) For Each acObjId As ObjectId In acBlkTblRec 'Your code' i=i+1 ProgressBar1.Value = i Me.Refresh 'Me=ActiveForm' Next acObjId End Using
Hope this may help you.
See you soon.
I suggest refreshing just the ProgressBar
Instead of Me.Refresh use ProgressBar1.Refresh
I had a lengthy operation and after a lot of optimalisation of the calculations, I found out, that half of the duration took the UI refreshing.
Access a broad range of knowledge to help get the most out of your products and services.
Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.
Upgrading to a 2015 product? Make sure to check these out 1st!