-
Jan 20th, 2022, 08:10 PM
#1
Thread Starter
PowerPoster
[RESOLVED] Time Sync
Hi,
Is there a way to call (Process Start) the 'Sync Now' button under the settings: 'Date and Time' ?
I've been trying to understand how to set-up a scheduled task to sync my machine at a specific time every day without success.
Poppa
Along with the sunshine there has to be a little rain sometime.
-
Jan 20th, 2022, 08:32 PM
#2
Re: Time Sync
If you're open to non-VB suggestions, you should take a look at the usage of the "net time" command.
-
Jan 20th, 2022, 10:45 PM
#3
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 21st, 2022, 03:46 AM
#4
Re: Time Sync
Originally Posted by Poppa Mintin
Hi,
Is there a way to call (Process Start) the 'Sync Now' button under the settings: 'Date and Time' ?
I've been trying to understand how to set-up a scheduled task to sync my machine at a specific time every day without success.
Poppa
Out of interest is there a reason why just letting it automatically sync the time wouldn't work?
-
Jan 21st, 2022, 06:42 AM
#5
Thread Starter
PowerPoster
Re: Time Sync
Originally Posted by PlausiblyDamp
Out of interest is there a reason why just letting it automatically sync the time wouldn't work?
It's not a case of 'It didn't work'. (That would be my preferred solution).
I can't find how to use the scheduler
Poppa
Along with the sunshine there has to be a little rain sometime.
-
Jan 21st, 2022, 07:17 AM
#6
Thread Starter
PowerPoster
Re: Time Sync
Originally Posted by .paul.
Thanks .Paul,
I think you're pointing me to: -
'If you want to sync the clock it's better to just run the command
'net start w32time & w32tm /resync /force through CMD.
'You can Invoke it using Process.Start() with cmd as the executable
'and /C net start w32time & w32tm /resync /force as the args.
But I can't understand what that's telling me. (I hardly ever understand anything in StackOverflow)
I thought it meant: -
Code:
Process.Start(CMD / C net start w32time & w32tm /resync /force)
'or:-
Process.Start(CMD(/ C net start w32time & w32tm /resync /force))
'or:-
Process.Start(CMD(w32time & w32tm /resync /force))
'or:-
Process.Start(CMD, w32time & w32tm /resync /force)
Of course none of those were acceptable, 'CMD is not declared...' (Not just CMD, none of the arguments are declared). Except:-
Code:
Process.Start(CMD, / C Net start w32time & w32tm /resync /force)
In this case only the '/ C' were unacceptable. Removing either... back to square one.
The above are only examples of my attempts. I don't believe there would be a reasonable combination of syntax untried... Except any correct syntax, of course.
Using the suggested 'correction' usually made no difference and frequently made things worse.
Poppa
Along with the sunshine there has to be a little rain sometime.
-
Jan 21st, 2022, 07:37 AM
#7
Re: Time Sync
Originally Posted by Poppa Mintin
It's not a case of 'It didn't work'. (That would be my preferred solution).
I can't find how to use the scheduler
Poppa
I think his point was if you have "Set time automatically" turned on, then Windows will synchronize the time automatically itself.
I've never manually synchronized the time, but the dialog shows that the last time the clock was synchronized was earlier today, a little before 2am local time.
I assume it may synchronize once a day, or perhaps even on a reboot. PlausiblyDamp was just asking why having windows synchronizing itself isn't sufficient for you.
As for Process.Start, "/C" is a switch for cmd.exe. Why did you change it to "/ C" which is meaningless. "Switch C" exists, "Switch Space" does not. You have changed the single Switch "/C" into two arguments "/" and "C" which both have no meaning to cmd.exe.
Spaces are used to separate arguments on the command line.
"Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930
-
Jan 21st, 2022, 08:41 AM
#8
Thread Starter
PowerPoster
Re: Time Sync
Originally Posted by passel
Why did you change it to "/ C" which is meaningless.
Not me chief !
I typed in:- 'Process.Start(CMD/C net start w32time & w32tm /resync /force)', clicked elsewhere and the line changed to:-
'Process.Start(CMD / C net start w32time & w32tm /resync /force)'.
Likewise with 'Process.Start(CMD /C net start w32time & w32tm /resync /force)' and with 'Process.Start(CMD/ C net start w32time & w32tm /resync /force)'.
Poppa
Along with the sunshine there has to be a little rain sometime.
-
Jan 21st, 2022, 08:54 AM
#9
Re: Time Sync
You need to review how to use process.start.
https://docs.microsoft.com/en-us/dot...t?view=net-6.0
That being said, no VB code needs to even be involved in this in the end, though. A scheduled task can be created using the appropriate command directly. But hey, if you want to make it harder for yourself just because, then have at it.
-
Jan 21st, 2022, 09:06 AM
#10
Thread Starter
PowerPoster
Re: Time Sync
Originally Posted by passel
I assume it may synchronize once a day, or perhaps even on a reboot. PlausiblyDamp was just asking why having windows synchronizing itself isn't sufficient for you.
I do have 'Time set automatically' turned on.
I have an old 'Grandfather Clock', its time keeping is a little erratic, seemed at one time as though room temperature made the difference, Warm day... longer pendulum... slower timing. Cool day... shorter pendulum... faster timing. So I fabricated a new wooden arm for the pendulum to reduce the effect of temperature, only to discover that the original arm was already wood. I'd thought it was tin plate painted to look like wood.
I also have a radio controlled clock against which I have been comparing the mechanical clock. I'm not convinced that the radio clock can always 'see' the radio signal so I've made a little app. to 'click' when the clock strikes, it measures the difference between 'Now' and the click. The timings are still 'odd'. So I was hoping to just synchronise my laptop when I open my app. just to be sure it's the same each time.
You say "The dialog shows that the last time the clock was synchronized was earlier today, a little before 2am local time."
I can't find that dialogue. (Win.10)
Poppa
Along with the sunshine there has to be a little rain sometime.
-
Jan 21st, 2022, 09:31 AM
#11
Re: Time Sync
Originally Posted by Poppa Mintin
...You say "The dialog shows that the last time the clock was synchronized was earlier today, a little before 2am local time."
I can't find that dialogue. (Win.10)
Poppa
It is in the Date & time setting under 'Synchronize your clock',
The following works for me.
Code:
'you can remove & pause. With it you'll have to dismiss the command window
Dim proc As Process = Process.Start("CMD", "/C net start w32time & w32tm /resync /force & pause")
proc.WaitForExit()
-
Jan 21st, 2022, 09:53 AM
#12
Thread Starter
PowerPoster
Re: Time Sync
Thanks Dbasnett,
Looks as though my lappy last sync'd when it woke from its last sleep.
Poppa
Along with the sunshine there has to be a little rain sometime.
-
Jan 21st, 2022, 10:06 AM
#13
Re: Time Sync
Originally Posted by Poppa Mintin
Thanks Dbasnett,
Looks as though my lappy last sync'd when it woke from its last sleep.
Poppa
FWIW - you can check your PC time with this,
USNO Master Clock
-
Jan 21st, 2022, 10:52 AM
#14
Thread Starter
PowerPoster
Re: Time Sync
Thanks all,
My code looks as though it's working... But !
Code:
Private Sub Form1_Load() Handles MyBase.Load
Label1.Text = Now.ToString
Sync()
Label2.Text = Now.ToString
End Sub
Private Sub Sync()
Dim proc As Process = Process.Start("CMD", "/C net start w32time & w32tm /resync /force")
proc.WaitForExit()
End Sub
1. When I manually 'Sync Now' it visibly takes a second or two to do it, but mine doesn't.
2. The dialogue still shows the same time as previously.
3. If I reset the time manually, then use my app. the time doesn't correct.
Poppa
Along with the sunshine there has to be a little rain sometime.
-
Jan 21st, 2022, 11:01 AM
#15
Thread Starter
PowerPoster
Re: Time Sync
Originally Posted by dbasnett
My clock is about 3 sec's fast according to the US Naval Observatory Master Clock.
Pop.
Along with the sunshine there has to be a little rain sometime.
-
Jan 21st, 2022, 11:35 AM
#16
Re: Time Sync
Originally Posted by Poppa Mintin
Thanks all,
My code looks as though it's working... But !
Code:
Private Sub Form1_Load() Handles MyBase.Load
Label1.Text = Now.ToString
Sync()
Label2.Text = Now.ToString
End Sub
Private Sub Sync()
Dim proc As Process = Process.Start("CMD", "/C net start w32time & w32tm /resync /force")
proc.WaitForExit()
End Sub
1. When I manually 'Sync Now' it visibly takes a second or two to do it, but mine doesn't.
2. The dialogue still shows the same time as previously.
3. If I reset the time manually, then use my app. the time doesn't correct.
Poppa
Probably an issue where elevated permissions are needed for that command to work. Since that command is spawned from your app, try running your app with elevated permissions and see if that makes it work.
-
Jan 21st, 2022, 05:52 PM
#17
Thread Starter
PowerPoster
Re: Time Sync
Originally Posted by OptionBase1
Try running your app with elevated permissions and see if that makes it work.
How on earth do I do that ?
Have you tried to make sense of anything on the web associated with elevated permissions ?
Can you find anything remotely relevant to this project ?
Pop.
Along with the sunshine there has to be a little rain sometime.
-
Jan 21st, 2022, 05:56 PM
#18
Re: Time Sync
Right-click your exe file or your shortcut or whatever you use to run your app and choose "Run as administrator"
Good luck.
-
Jan 21st, 2022, 07:56 PM
#19
Thread Starter
PowerPoster
Re: Time Sync
Thanks OptionBase1,
That worked which shows that I do need to run as administrator, but I need to be able to do that in code so that the user can use the shortcut in the normal manner.
I'm struggling to do that. This code runs but doesn't actually sync my clock.
I'm pretty sure the problem is in lines 11 and 12, but I'm unable to figure out their correct syntax.
vb.net Code:
Private Sub Form1_Load() Handles MyBase.Load
Label1.Text = Now.ToString
Sync()
Label2.Text = Now.ToString
Label3.Text = Now.ToString
End Sub
Private Sub Sync()
Dim proc As New Process
proc.StartInfo.FileName = "CMD"
proc.StartInfo.Arguments = "/C net start w32time & w32tm /resync /force"
proc.StartInfo.UseShellExecute = False
proc.StartInfo.Verb = "runas"
proc.Start()
proc.WaitForExit()
End Sub
Poppa
Last edited by Poppa Mintin; Jan 21st, 2022 at 08:02 PM.
Reason: Typo
Along with the sunshine there has to be a little rain sometime.
-
Jan 21st, 2022, 08:13 PM
#20
Re: Time Sync
Project-->Properties-->Application-->View UAC Settings - app.manifest
Replace the requestedExecutionLevel node with requireAdministrator. Rebuild your Exe and your app will always run as administrator.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 22nd, 2022, 07:52 AM
#21
Thread Starter
PowerPoster
Re: Time Sync
Originally Posted by .paul.
Project-->Properties-->Application-->View UAC Settings - app.manifest
Replace the requestedExecutionLevel node with requireAdministrator. Rebuild your Exe and your app will always run as administrator.
Thanks .Paul,
Just for information, my version of VS (Visual Studio Community 2019 Version 16.11.8 .NET Framework Version 4.8.04084) doesn't have 'View UAC Settings' but, 'View Windows Settings'.
Everything else is as you indicate...
Made the change, using the supplied template.
Start the app. click the 'Restart' option that appears.
All now works correctly, thanks again.
Poppa
Last edited by Poppa Mintin; Jan 22nd, 2022 at 08:21 AM.
Reason: Minor addition.
Along with the sunshine there has to be a little rain sometime.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|