time steps in sub and write value to console - vb.net

Some sub's in my VB.Net program take a lot of time, and I do not understand why. I'd like to start a counter at the beginning of the sub and write the Milliseconds passed for each row in the console.
I tried using a Timer and resetting it after each row but it was too threadintesive. Is there a better way, using the system date/time in order to get a quite precise reading of the time each step takes?
Thank you

Use the Stopwatch class. However doing so for each row seems a bit impossible... That means you would have to add for example Console.WriteLine() after every row.
Example usage:
Dim sw As New Stopwatch
sw.Start()
yourMethod()
sw.Stop()
Console.WriteLine(String.Format("{0} ms", sw.Elapsed.TotalMilliseconds))

Related

vba timer available in modules

Could anyone please tell me if and how it is possible to count time of running various modules in VBA excel?
I am only able to count the elapsed time of one single module. However, when it comes to more than 1 module it seems there is no info out there. Or I am blind.
With one module I pass Timer value to a variable set at the begining of code and then at the end. Then I deduct second variable from the first.
When I try to do it between modules initial variable value is reset to empty.
Many thanks for any pointers
Tommeck37
For a simple way to measure the time it takes to execute portions of your code, you can use the VBA timer() function. That function isn't perfect, but it's a good place to start.
Timer() returns the number of seconds that have elapsed since midnight, including fractions.
Just before you call a module, assign the result of timer() to a variable. Timer returns a Double, but you could store it in a variant, too. Then do another call to timer() just after the module. For example:
time1 = timer()
call Proc1()
time2 = timer()
call Proc2()
time3 = timer()
debug.print "Proc1 time: " & cStr(time2-time1)
debug.print "Proc2 time: " & cStr(time3-time2)
There are other timers that you can use in VBA, but they require using windows procedures, and are harder to setup and use. For gross timing, this works fine. If you want to time with millisecond accuracy (or better), then look into other ways.

How to slow down timer in VB.NET?

I am making a HTML tester. I added the following code to timer.
Dim sb As New System.Text.StringBuilder
sb.AppendLine(RichTextBox1.Text)
IO.File.WriteAllText("htmltester.html", sb.ToString())
WebBrowser1.Navigate("file:///" & IO.Path.GetFullPath(".\htmltester.html"))
But it reloads very fast and makes annoying sound. Is there any way to slow down the timer to execute this command after 5 seconds ?
Thanks !
You can refer the timer Class. Timer's tick is controlled by setting the interval property.so make its interval to 5000 if you want to repeated it in every 5 seconds. since the interval is measured in Milli seconds.
you can set the interval by using the following code,
Let's assume that the name of the timer is SlowTimer:
SlowTimer.Interval = 5000
Your code does not have any timing functions in it or any loops so it should just run once.
If this is just some hacky throw away code then try a Threading.Thread.Sleep(5000) before the navigate.

Microsecond thread sleep

I need to give a PWM output through a special output on my machine. I was just wondering how I could achieve something similar to thread.sleep but with the resolution of microseconds?
I am not sure why you want to rely on a so short period of time. You should bear in mind that the accuracy at these intervals is not too good and is even conditioned even by the CPU of the given machine. Thus, in any case you should thoroughly test the given approach under the given conditions to make sure that it delivers exactly what you want.
The smallest time value supported by .NET is a Tick (one hundred nanoseconds, as explained in this MSDN article) and thus you can build your own "sleep method" dealing with microseconds. Sample code:
Dim count As Integer = 0
Dim sw As Stopwatch = New Stopwatch
Dim microSeconds As Integer = 5
sw.Start()
Do While (sw.ElapsedTicks < 10 * microSeconds)
Loop
sw.Stop()

VB.net Hour(Now()) Not Working

I am trying to make a vb.net program which at a specific time that the user has chosen, the code will excecute. To do this, I need to check every minute to check every minute if the hour and minute the user has entered are matching to the current time (unless there is a better way to do this). I tried to use
Dim CurrentHour As Integer = Hour(Now())
But the program gives me an error message saying,
Expression is not an array or method, and cannot have an argument list
I am going to use a Do Loop to check, but of course to see if the two are matching, I need the current Hour and Minute
Your code is correct. What you need to watch our for is stuff like this:
Dim Now As Date
Dim CurrentHour = Hour(Now())
Which produces error BC30471: Expression is not an array or a method, and cannot have an argument list.
You see the problem by now perhaps, the Now variable hides the Now function. The compiler now gets confuzzled, it doesn't understand why the parentheses are present. And correctly complains that Now is not an array and not a method. It isn't, not anymore.
Other than renaming the variable, you can also solve it by giving a more complete name:
Dim CurrentHour = Hour(DateAndTime.Now())
Although that gets to be fairly obscure, using DateTime.Now instead is the .NET way instead of the Basic way.
You should use the native DateTime properties:
Dim CurrentHour As Integer = Now().Hour
If you want to use the Hour method, you may need to fully qualify it to be:
Microsoft.VisualBasic.Hour(Now())
because Hour is most likely a property or method elsewhere in your application.
Dim Inputtime As DateTime
if Inputtime = Date.Now.Hour Then
MsgBox("Success!")
End If
I wouldn't use a do loop as it will consume all of the memory for the program. I would go with a timer that ticks once every miunute. and have it fire this sub routine.
Task Scheduler is an option. I rather use Marshal.FinalReleaseComObject then the loop in the bottom of the code, and GC.Collect needs to be called again after GC.WaitForPendingFinalizers()

Loop to check time in VB.NET

So I'm kind of new to VB and am just playing around with a little project, I currently need a loop that is constantly checking the systems clock to see if it's equal to a certain time.
While Not myTime.Hour = 24
If TimeOfDay = newTime Then
nfi.ShowBalloonTip(15)
intRandNumb = RandomNumber(1, 15)
dblAddMinutes = intTime + intRandNumb
newTime = TimeOfDay.AddMinutes(dblAddMinutes)
End If
End While
I have this right now, but obviously it's grinding everything to a halt and using 50% of my cpu in the process, I just would like to know what I can substitute in or change to make this loop run better and perform how I need it to.
you can add
Threading.Thread.Sleep(0),
this will cause a context switch and greatly reduce the CPU usage
Also consider using a timer object to be called every 10 or 100 ms, this will also be better in usage then having a loop
You can use
Threading.Thread.Sleep(0)
This will cause the working thread to yield the rest of it's current timeslice which will reduce the cpu usage quite a bit. However you should consider whether you really nead busy waiting for the time or if you could get away with setting a timer to count down the difference between the current time and the expected time, e.g.:
var t = new System.Timers.Timer((DateTime.Now - DateTime.Now).TotalMilliseconds);
t.Elapsed = DoSomething;
t.Start();
checking the systems clock to see if it's equal to a certain time.
There are two "correct" ways to do this:
Build a normal app that doesn't care what time it is, and set it up in windows as a schedule task.
Check the time once and calculate how long until the desired time. Then set up a timer to wait for that exact duration.
Under no circumstance should you keep polling the system clock for something like this that will just run once.
As Joel pointed out, you should try using a timer instead. I'm not sure if your app is a form or console or other, so I'll try to be generic and use System.Timers.Timer.
The code here (interval is set at 10ms, change to a value of your need):
Private timer1 As System.Timers.Timer
Const interval As Integer = 10
Sub initTimer()
timer1 = New System.Timers.Timer(10)
AddHandler timer1.Elapsed, AddressOf Me.timer_Elapsed
timer1.Start()
End Sub
Sub timer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
'do your stuff here
'Console.WriteLine(e.SignalTime.ToString())
End Sub