.NET

Reply
Valued Contributor
GrzesiekGP
Posts: 67
Registered: ‎02-03-2012
Message 1 of 7 (856 Views)

Show WinForm with ProgressBar while doing job in command.

856 Views, 6 Replies
08-27-2012 01:03 AM

Hello!

 

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?

Thanks.

*Expert Elite*
norman.yuan
Posts: 1,016
Registered: ‎04-27-2009
Message 2 of 7 (835 Views)

Re: Show WinForm with ProgressBar while doing job in command.

08-27-2012 06:44 AM in reply to: GrzesiekGP

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:

 

http://drive-cad-with-code.blogspot.ca/2010/12/showing-progress-window-when-running.html

 

 

ADN Support Specialist
augusto.goncalves
Posts: 367
Registered: ‎04-30-2009
Message 3 of 7 (793 Views)

Re: Show WinForm with ProgressBar while doing job in command.

08-30-2012 07:44 AM in reply to: GrzesiekGP

Hi,


AutoCAD has a built-in progress bar feature, have you looked at that?

 

http://through-the-interface.typepad.com/through_the_interface/2007/05/displaying_a_pr.html

 

Regards,

 

Regards,



Augusto Goncalves
Autodesk Developer Network
Mentor
matus.brlit
Posts: 246
Registered: ‎03-11-2008
Message 4 of 7 (781 Views)

Re: Show WinForm with ProgressBar while doing job in command.

08-30-2012 12:03 PM in reply to: GrzesiekGP

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.componentmodel.backgroundworker.aspx but don't forget to create thread-safe method for incrementing the progress bar.

Active Contributor
MGOMKD
Posts: 33
Registered: ‎06-23-2011
Message 5 of 7 (749 Views)

Re: Show WinForm with ProgressBar while doing job in command.

09-03-2012 07:09 AM in reply to: matus.brlit

Hi,

 

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.

 

For instance:

 

'ProgressBar init'
Dim iMin As Long = 0
Dim iMax As Long

'Object Count'
Using acTrans = acDocs.MdiActiveDocument.Database.TransactionManager.StartTransaction()
      acBlkTbl = acTrans.GetObject(acDocs.MdiActiveDocument.Database.BlockTableId, OpenMode.ForRead)
      acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), 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.TransactionManager.StartTransaction()
      acBlkTbl = acTrans.GetObject(acDocs.MdiActiveDocument.Database.BlockTableId, OpenMode.ForRead)
      acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), 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.

Mentor
matus.brlit
Posts: 246
Registered: ‎03-11-2008
Message 6 of 7 (730 Views)

Re: Show WinForm with ProgressBar while doing job in command.

09-03-2012 11:40 PM in reply to: MGOMKD

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.

Active Contributor
MGOMKD
Posts: 33
Registered: ‎06-23-2011
Message 7 of 7 (638 Views)

Re: Show WinForm with ProgressBar while doing job in command.

10-04-2012 05:46 AM in reply to: matus.brlit

Yeah! refreshing progressing bar instead of the form, is better!

 

See you.  

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.