Author Topic: make a program running immediatly at the first execution  (Read 6546 times)

0 Members and 1 Guest are viewing this topic.

samideqlqpart

  • Newt
  • Posts: 40
make a program running immediatly at the first execution
« on: August 02, 2017, 05:15:57 PM »
hello
my problem is
when i lunch my plug-in in autocad for the first time, it takes a long time to execute
but after and at every time it takes a very short moment
it seems that something is stoked in the RAM.i don't know wich element
my question : what can i do to make my program running immediatly at the first call
 
thank you

Atook

  • Swamp Rat
  • Posts: 1029
  • AKA Tim
Re: make a program running immediatly at the first execution
« Reply #1 on: August 02, 2017, 05:32:24 PM »
Sounds to me like you're loading a large array or list when loading the DLL? Maybe from a database/text file?

You could start using a timer to troubleshoot where the problem is.

Something like:
Code - C#: [Select]
  1. Stopwatch timer = Stopwatch.StartNew();
  2. // do stuff
  3. timer.Stop();
  4. Debug.Writeline($"Elapsed time: {timer.ElapsedMilliseconds} ms.");
  5.  

Once you've figured out where the problem is, you can optimize it, and decide if you want it called in the Inititalize() event of the IExtensionApplication, or when your code is actually called by the user.
« Last Edit: August 02, 2017, 05:38:10 PM by Atook »

samideqlqpart

  • Newt
  • Posts: 40
Re: make a program running immediatly at the first execution
« Reply #2 on: August 02, 2017, 05:51:38 PM »
i've translated it to vb.net

Dim timer As Stopwatch = Stopwatch.StartNew()
' do stuff
timer.[Stop]()
Debug.Writeline("Elapsed time: {timer.ElapsedMilliseconds} ms.")

1-where do i have to past this code?
2-what the code do?

Atook

  • Swamp Rat
  • Posts: 1029
  • AKA Tim
Re: make a program running immediatly at the first execution
« Reply #3 on: August 02, 2017, 07:20:33 PM »
That code wraps around various elements of your code that are initializing things. The Debug.Print statement will tell you how how long it took. You will see it in the output window of Visual studio when you debug your code.

Code - C#: [Select]
  1. Stopwatch dbTimer= Stopwatch.StartNew();
  2. // do load database
  3. dbTimer.Stop();
  4. Debug.Writeline($"Loading Database, elapsed time: {dbTimer.ElapsedMilliseconds} ms.");
  5. ...
  6. Stopwatch layerTimer= Stopwatch.StartNew();
  7. // do Layer Creation
  8. layerTimer.Stop();
  9. Debug.Writeline($"Creating Layers, elapsed time: {layerTimer.ElapsedMilliseconds} ms.");


When this code gets called you might see something like this in the output window of your IDE while debugging:
Code: [Select]
Loading Database, elapsed time: 5042 ms.
Creating Layers, elapsed time: 15 ms.

That would tell you that the delay is loading the database, and you can decide how to change it, or where to put it.

I'm not familiar with the VB.NET IExtensionApplication.Initialize event is, but that's where you put code that runs when your app is loaded. If you were calling the loaddatabase in that function you'd see a pause when your app was first loaded into memory. If you were calling loaddatabase in some other command you created, you'd see the pause then.

samideqlqpart

  • Newt
  • Posts: 40
Re: make a program running immediatly at the first execution
« Reply #4 on: August 03, 2017, 07:08:00 AM »
i see nothing :crazy2:
but i can't change anything cause my program is established
perhaps i reserved a large array dim... dim..... at the begin


but how to explain that in the second uses it's fast than the first run

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: make a program running immediatly at the first execution
« Reply #5 on: August 03, 2017, 08:12:41 AM »
Autocad doesn't load your DLL into memory until you execute something in it. So the first time you run your command it loads the DLL and executes the command. The second time and every time there after the DLL is already loaded and Autocad just executes your command.  That's the time difference in the first and second execution.
Revit 2019, AMEP 2019 64bit Win 10

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8702
  • AKA Daniel
Re: make a program running immediatly at the first execution
« Reply #6 on: August 03, 2017, 09:46:04 AM »
Not much you can do about time on the JITTER  :crazy2:
https://en.wikipedia.org/wiki/Just-in-time_compilation
see Startup delay and optimizations

samideqlqpart

  • Newt
  • Posts: 40
Re: make a program running immediatly at the first execution
« Reply #7 on: August 03, 2017, 04:56:28 PM »
ok
MexicanCustard you're in right!
i think that there is no solution.
is it possible to make a subroutine  between the moment of execution  and the tracing
to keep or to occupe the user like...
"wait for a moment!" or "the program compute!" or a mouse pointer in use or other

Jeff H

  • Needs a day job
  • Posts: 6150
Re: make a program running immediatly at the first execution
« Reply #8 on: August 03, 2017, 05:07:47 PM »
samideqlqpart,

There are so many different things it could be it is just wild guesses trying to figure out without having your add-in to test.
If you have no idea what order things are loaded and how when then get loaded then simplest thing would be commenting portion of code out until you find the bottleneck.

If you want to narrow it down for starters does your app use ExtensionApplication event?
Application Initialization

samideqlqpart

  • Newt
  • Posts: 40
Re: make a program running immediatly at the first execution
« Reply #9 on: August 03, 2017, 05:40:23 PM »
my app does'nt use ExtensionApplication event
i'm looking now at the link

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8702
  • AKA Daniel
Re: make a program running immediatly at the first execution
« Reply #10 on: August 03, 2017, 08:35:57 PM »
ExtensionApplication is a good place to move JIT time to, for example,
if your routine is using a database, you can run a query or two to get the database JITTED up,
or load your winforms (without calling show).. this will at least move the annoying wait to startup

samideqlqpart

  • Newt
  • Posts: 40
Re: make a program running immediatly at the first execution
« Reply #11 on: August 04, 2017, 07:39:28 AM »
hello
i've used Initialization of code and Optimizing the loading of my pluggin
i got the same result
i realise that i have to build something to keep the user's attention when  loading the database
from i get point insertion of my drawing until it's created like... "wait for a moment!" ....but how to do it.?.

samideqlqpart

  • Newt
  • Posts: 40
Re: make a program running immediatly at the first execution
« Reply #12 on: August 04, 2017, 09:20:42 AM »
ie
and make the message disapear when the drawing is done

n.yuan

  • Bull Frog
  • Posts: 348
Re: make a program running immediatly at the first execution
« Reply #13 on: August 04, 2017, 09:28:35 AM »

samideqlqpart

  • Newt
  • Posts: 40
Re: make a program running immediatly at the first execution
« Reply #14 on: August 04, 2017, 10:17:10 AM »
hello and thank you Mr Yuan
"This process may take a while to complete. It is a common practice to show a progress bar during this lengthy processing to let user know that AutoCAD is busy processing data."
Yeeeeaaaaaahhh for the link

samideqlqpart

  • Newt
  • Posts: 40
Re: make a program running immediatly at the first execution
« Reply #15 on: August 05, 2017, 08:11:10 AM »
hello
Mr Yuan have you the code in vb.net
cause my converter has problem with+=

public dlgProgress(
            ILongProcessingObject executingObj)
            : this()
        {
            _executingObject = executingObj;

            _executingObject.ProcessingStarted +=
                new LongProcessStarted(ExecutingObject_ProcessStarted);
            _executingObject.ProcessingProgressed +=
                new LongProcessingProgressed(ExecutingObject_Progressed);
            _executingObject.ProcessingEnded +=
                new EventHandler(ExecutingObject_ProcessEnded);
            _executingObject.CloseProgressUIRequested +=
               new EventHandler(ExecutingObject_CloseProgressUIRequested);
        }

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: make a program running immediatly at the first execution
« Reply #16 on: August 05, 2017, 08:20:24 AM »
Use AddHandler and AddressOf in VB.NET.  All the code is doing is subscribing to events.


So for the first event you would have


AddHandler _executingObject.ProcessingStarted, AddressOf ExecutingObject_ProcessStarted.



Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

samideqlqpart

  • Newt
  • Posts: 40
Re: make a program running immediatly at the first execution
« Reply #17 on: August 06, 2017, 07:11:07 PM »
i'm lost
what i have to do for displaying a progressbar  during this long time of waiting?
from the press the button of execution until the dispay of the drawing
to allow autocad load dll
ToolStripProgressBar...? doEvent....?
what are steps to do
thank you


MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: make a program running immediatly at the first execution
« Reply #18 on: August 08, 2017, 07:30:20 AM »
i'm lost
what i have to do for displaying a progressbar  during this long time of waiting?
from the press the button of execution until the dispay of the drawing
to allow autocad load dll
ToolStripProgressBar...? doEvent....?
what are steps to do
thank you

Progress Bars get complicated in AutoCAD since database manipulation has to happen on the UI thread. You can you use AutoCAD's built in ProgressMeter().  Not great but better than nothing. Check out Kenan's Blog.  Or Norman's  Blog.
Revit 2019, AMEP 2019 64bit Win 10

samideqlqpart

  • Newt
  • Posts: 40
Re: make a program running immediatly at the first execution
« Reply #19 on: August 09, 2017, 04:09:42 AM »
hi
yes i 've found it
and there is code in vb.net(with converter..)
Imports Autodesk.AutoCAD.Runtime
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Namespace ProgressMeterTest
    Public Class Cmds
        <CommandMethod("PB")> _
        Public Sub ProgressBarManaged()
            Dim pm As New ProgressMeter()
            pm.Start("Testing Progress Bar")
            pm.SetLimit(100)
            ' Now our lengthy operation
            For i As Integer = 0 To 100
                System.Threading.Thread.Sleep(5)
                ' Increment Progress Meter...
                pm.MeterProgress()
                ' This allows AutoCAD to repaint
                Application.DoEvents()
            Next
            pm.[Stop]()
        End Sub
    End Class
End Namespace


and an other most interresting code

Imports System
Imports System.Collections.Generic
Imports System.Windows.Forms
Imports System.ComponentModel



Class FibonacciNumber
    Inherits Form

   
    Private progressStatusStrip As StatusStrip
    Private toolStripProgressBar As ToolStripProgressBar
    Private requestedCountControl As NumericUpDown
    Private goButton As Button
    Private outputTextBox As TextBox
    Private backgroundWorker As BackgroundWorker
    Private toolStripStatusLabel As ToolStripStatusLabel
    Private requestedCount As Integer


    Public Sub New()
        [Text] = "Fibonacci"

        ' Prepare the StatusStrip.
        progressStatusStrip = New StatusStrip()
        toolStripProgressBar = New ToolStripProgressBar()
        toolStripProgressBar.Enabled = False
        toolStripStatusLabel = New ToolStripStatusLabel()
        progressStatusStrip.Items.Add(toolStripProgressBar)
        progressStatusStrip.Items.Add(toolStripStatusLabel)

        Dim flp As New FlowLayoutPanel()
        flp.Dock = DockStyle.Top

        Dim beforeLabel As New Label()
        beforeLabel.Text = "Calculate the first "
        beforeLabel.AutoSize = True
        flp.Controls.Add(beforeLabel)
        requestedCountControl = New NumericUpDown()
        requestedCountControl.Maximum = 1000
        requestedCountControl.Minimum = 1
        requestedCountControl.Value = 100
        flp.Controls.Add(requestedCountControl)
        Dim afterLabel As New Label()
        afterLabel.Text = "Numbers in the Fibonacci sequence."
        afterLabel.AutoSize = True
        flp.Controls.Add(afterLabel)

        goButton = New Button()
        goButton.Text = "&Go"
        AddHandler goButton.Click, AddressOf button1_Click
        flp.Controls.Add(goButton)

        outputTextBox = New TextBox()
        outputTextBox.Multiline = True
        outputTextBox.ReadOnly = True
        outputTextBox.ScrollBars = ScrollBars.Vertical
        outputTextBox.Dock = DockStyle.Fill

        Controls.Add(outputTextBox)
        Controls.Add(progressStatusStrip)
        Controls.Add(flp)

        backgroundWorker = New BackgroundWorker()
        backgroundWorker.WorkerReportsProgress = True
        AddHandler backgroundWorker.DoWork, AddressOf backgroundWorker1_DoWork
        AddHandler backgroundWorker.RunWorkerCompleted, AddressOf backgroundWorker1_RunWorkerCompleted
        AddHandler backgroundWorker.ProgressChanged, AddressOf backgroundWorker1_ProgressChanged
    End Sub


    Private Sub backgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs)
        ' This method will run on a thread other than the UI thread.
        ' Be sure not to manipulate any Windows Forms controls created
        ' on the UI thread from this method.
        backgroundWorker.ReportProgress(0, "Working...")
        Dim lastlast As [Decimal] = 0
        Dim last As [Decimal] = 1
        Dim current As [Decimal]
        If requestedCount >= 1 Then
            AppendNumber(0)
        End If
        If requestedCount >= 2 Then
            AppendNumber(1)
        End If
        Dim i As Integer

        While i < requestedCount
            ' Calculate the number.
            current = lastlast + last
            ' Introduce some delay to simulate a more complicated calculation.
            System.Threading.Thread.Sleep(100)
            AppendNumber(current)
            backgroundWorker.ReportProgress(100 * i / requestedCount, "Working...")
            ' Get ready for the next iteration.
            lastlast = last
            last = current
            i += 1
        End While


        backgroundWorker.ReportProgress(100, "Complete!")
    End Sub


    Delegate Sub AppendNumberDelegate(number As [Decimal])

    Private Sub AppendNumber(number As [Decimal])
        If outputTextBox.InvokeRequired Then
            outputTextBox.Invoke(New AppendNumberDelegate(AddressOf AppendNumber), number)
        Else
            outputTextBox.AppendText((number.ToString("N0") + Environment.NewLine))
        End If
    End Sub
    Private Sub backgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs)
        toolStripProgressBar.Value = e.ProgressPercentage
        toolStripStatusLabel.Text = e.UserState '
    End Sub


    Private Sub backgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs)
        If TypeOf e.Error Is OverflowException Then
            outputTextBox.AppendText((Environment.NewLine + "**OVERFLOW ERROR, number is too large to be represented by the decimal data type**"))
        End If
        toolStripProgressBar.Enabled = False
        requestedCountControl.Enabled = True
        goButton.Enabled = True
    End Sub


    Private Sub button1_Click(sender As Object, e As EventArgs)
        goButton.Enabled = False
        toolStripProgressBar.Enabled = True
        requestedCount = Fix(requestedCountControl.Value)
        requestedCountControl.Enabled = False
        outputTextBox.Clear()
        backgroundWorker.RunWorkerAsync()
    End Sub
End Class

now i'm searching how to launch the progress meter  at the same time as my application start and not after
or exactly how to fix the max of the progressmeter to the end of dll's load
thank you


samideqlqpart

  • Newt
  • Posts: 40
Re: make a program running immediatly at the first execution
« Reply #20 on: August 11, 2017, 10:22:14 AM »

my big problem is
HOW TO SET THE MAX OF PROGRESS BAR?
Who it's corresponding to the  apparition of my drawing
trannks

samideqlqpart

  • Newt
  • Posts: 40
Re: make a program running immediatly at the first execution
« Reply #21 on: August 12, 2017, 07:07:32 AM »
or how to estimate the remaining time and display it.
 

samideqlqpart

  • Newt
  • Posts: 40
Re: make a program running immediatly at the first execution
« Reply #22 on: October 05, 2017, 07:07:23 AM »
hi
How to fill or to keep users waiting?
from the moment the user specifies the insertion point until when the code finishes the drawing.
with message or progressbar..
as this moment how to set the "setlimit"
thanks

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8702
  • AKA Daniel
Re: make a program running immediatly at the first execution
« Reply #23 on: October 06, 2017, 04:49:55 AM »
How to fill or to keep users waiting?

Some nice elevator music?
<iframe width="854" height="480" src="https://www.youtube.com/embed/jj0ChLVTpaA" frameborder="0" allowfullscreen></iframe>

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2139
  • class keyThumper<T>:ILazy<T>
Re: make a program running immediatly at the first execution
« Reply #24 on: October 06, 2017, 05:13:56 AM »
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

samideqlqpart

  • Newt
  • Posts: 40
Re: make a program running immediatly at the first execution
« Reply #25 on: October 09, 2017, 04:59:51 AM »
yeah
not bad idea!!!!!!