Visual Studio Macro - Logging Timestamps on Solution opens, closes, builds, etc - visual-studio

I've got a solution that takes a while to open. (5 minutes) Due to our processes and procedures I'm constantly jumping around between different branches of the same solution. I'm constantly closing and loading different variations of the same solution more than a few times a day. That's over 15 minutes - 30 minutes wasted just wating for my Visual Studio. I'm blaming the outdated computer hardware as the culprit. I want to capture metrics of how long solutions are taking to load as well as how long build times are taking. My goal is to present these metrics to management.
I'm using Visual Studio Macros to track this information.
As it stands right now I'm logging time stamps on these three events EnvDTE.SolutionEvents.BeforeClosing, EnvDTE.SolutionEvents.AfterClosing, EnvDTE.SolutionEvents.Opened.
The problem is, the SolutionEvents.Opened fires after the entire solution has been loaded. I need to generate a timestamp from when I start to load the solution and when it finishes.
How can I find out when Visual Studio starts loading the solution? So that I can compare it to when it finished loading.
Below is my code (My LogWithTimeStamp() function just writes the string to a file with the current timestamp as a prefix:
Private Sub SolutionEvents_BeforeClosing() Handles SolutionEvents.BeforeClosing
LogWithTimeStamp("SolutionEvents.BeforeClosing")
End Sub
Private Sub SolutionEvents_AfterClosing() Handles SolutionEvents.AfterClosing
LogWithTimeStamp("SolutionEvents.AfterClosing")
End Sub
Private Sub SolutionEvents_Opened() Handles SolutionEvents.Opened
LogWithTimeStamp("SolutionEvents.Opened")
End Sub

Related

Debugging Step Into, Over, Out within VB6 IDE closes the class window

I am experiencing very strange behavior within VB6 IDE whenever the break point hits(Step Into, Out, Over), the class is closed and makes it impossible to debug. Then within window-Cascade i can re-open the class but again when break point hits, the class is closed. Can anyone help please.
Step execution does sometimes behave that way. The reason is that VB is event driven and when an event occurs, then the code behind that event will run, and your code that you are stepping through might NOT be the code that gets run, so things change and code runs while your PAUSED code is still on hold.
When I encounter that I overcome it by using debug.print to send my monitored variables' current values to the OUTPUT window, or if you need more elaborate capability, write a sub that sends the data to a local text file and then invoke that sub as needed, passing into the variables ( and labels ) that you want displayed.
Once debug.print or a logging routine is in place then run the code WITHOUT pauses or breaks. The debugging output will tell you what is happening, in what order etc, so no need to stop the code or risk altering the order of execution.
Be sure to include lots of 'context' data such as : 'Entering SUB_XYZ, Param values are A, B, C... NOW at line 99 in SUB XYZ.... NOW in TRUE side of IF TEST # 1....
Include time stamps on all outputs.
Put your tracing logic only around the suspected problem area, expand from there only as needed.
It's a pain, but it works.
I finally resolved this issue and problem was within Display settings within windows 10. Basically if I apply vertical settings by placing both screen vertically 2nd on top of first then this issue happens,if i apply horizontal settings then this issue does not happen.
problematic settings with vb
settings that does solves debugging issue. VB is so weird and old cannot cope with display settings

Create a visual studio breakpoint that pauses for one second and then continues

The topic says it all. Sometimes i just want to take a quick look and then continue, especially when a code line get hit several times. Can i automate the press-manually-f5-to-continue-after-a-second?
Right click on a breakpoint an select "When Hit...":
You can run a macro like this:
Sub Sleep1s()
System.Threading.Thread.Sleep(1000)
End Sub
Or you can call 11 times {System.Threading.Thread.Sleep(90)} in the message. (Because VS debugger doesn't allow for expression to run for more than 100 ms).
But sleeping this way will block the main IDE UI thread. I don't know is it acceptable for you.

How to profile/debug VBA script that works fine on XLS files, but hangs on XLSX files?

I have a VB script that does row processing on a small Excel file (35 columns, 2000 rows, no hidden content). It takes about 3 seconds to process the file when applied to an .xls file. If I save the .xls as a .xlsx file, the same script takes about 30 to 60 seconds, completely freezing Excel several times in the process (although it finishes and the results are correct).
I suspect some loop parts of the code are responsible, but I need to find out which. I have found scripts like https://stackoverflow.com/a/3506143/5064264 which basically amount to timing all parts of the script by hand - similar to printf-debugging, which is not very elegant and also consumes much time for large scripts.
Is there a better alternative to semi-manual profiling, like performance analyzers in most IDEs, or exist any other approaches to this problem? I do not want to post the code as it is rather long and I also want to solve it for other scripts in the future.
yes - kind of. I use this to test how fast things are running and experiment with different approaches to get better timings. I stole this from somewhere here on SO.
sub thetimingstuff()
Dim StartTime As Double
Dim SecondsElapsed As Double
StartTime = Timer
'your code goes here
SecondsElapsed = Round(Timer - StartTime, 2)
MsgBox "This code ran successfully in " & SecondsElapsed & " seconds", vbInformation
end sub
Another EDIT
you could bury debug.print lines sporadically through your code to see how much each chunk takes to execute.
The error in my case was as Comintern's comment suggested - a method was copying whole columns into a dictionary to reorder them, instead of just the cells with values in them. To preserve this for future readers, I am copying his/her excellent comment here, thanks again!
An xls file has a maximum of 65,536 rows. An xlsx file has a maximum of 1,048,576 rows. I'd start by searching for .Rows. My guess is that you have some code iterates the entire worksheet instead of only rows with data in them.
As for generic profiling or how to get there, I used a large amount of breakpoints in the editor (set/remove with left-click in the line number column of the code editor):
I first set them every 20 lines apart in the main method, ran the program with the large dataset, then after each press of continue (F5) manually measured if it was slow or not.
Then I removed the breakpoints in the fast regions and added additional ones in the slow regions to further narrow it down into methods and then into lines inside the methods.
At the end, I could verify if by commenting out the responsible line of code and fix it.

VBA to close excel file when Windows7 computer is locked

I have an excel file on a shared drive used by 6/7 people, however only one person can edit at a time. The frustration comes when one person opens the file for editing then disappears for lunch for an hour, leaving the excel file open and un-editable for other users.
Is it possible for VBA to listen for when a station is locked, and activate a macro accordingly?
Sorry I am not posting any of my own attempts as I'm a bit of a fish out of water with this level of VBA.
Any points that may help get me started would be really useful.
You have a few options:
Kill the co-workers who do this
Have other users create a copy and save-as to then merge latter (quite hacky)
Or you try a timeout - so if the user selects nothing i 10 minutes the workbook closes. Selecting lock would be a issue with security I think and windows wouldnt let you have that kind of power.
So to timeout call a function every ten minutes to check if user has selected any other cells in the worbook.
If difference("n", lastaction , Now) > 10 Then
ThisWorkbook.Close SaveChanges:=True
End If
You can use NOW function in vba to find current date and time and the work out difference with when an action was made to find the value of 'lastaction'. To do this use:
Sub AnAction(ByVal Sh As Object, ByVal Target As Range)
lastaction = now
End Sub
Hopefully that answers your question.

Excel cell stopwatch program

I have very limited programming experience (i.e. mainly VB in Microsoft Excel and a bit of Visual Studio) so looking for help on how to create a program which is in effect a timer.
I have a boolean cell in Excel (0 or 1) which changes throughout the day. What I want to do is measure the time in a day for which this is 1. What is the easiest way to do this?
I tried doing it in Excel but came across several issues such as constant refreshing needed as it wouldn't pick up when the cell value changed. From years ago having used a bit of Visual Studio I thought this could be a good solution but struggling to understand how to get the cell value imported.
Any help would be greatly appreciated. Thanks.
Can't you simply handle the Worksheet_Change event. Something along the following lines:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngMyCell As Range
Set rngMyCell = Me.Range("A1") ' or whatever cell you're monitoring
If Not Intersect(Target, rngMyCell) Is Nothing Then
If rngMyCell.Value = 1 Then
' Start Stopwatch if it's not running
Else
' Pause Stopwatch if it's running
End If
End If
End Sub

Resources