Results 1 to 21 of 21

Thread: [RESOLVED] Time Sync

  1. #1

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Resolved [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.

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Time Sync

    If you're open to non-VB suggestions, you should take a look at the usage of the "net time" command.

  3. #3

  4. #4
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: Time Sync

    Quote Originally Posted by Poppa Mintin View Post
    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?

  5. #5

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Time Sync

    Quote Originally Posted by PlausiblyDamp View Post
    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.

  6. #6

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Time Sync

    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.

  7. #7
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Time Sync

    Quote Originally Posted by Poppa Mintin View Post
    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

  8. #8

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Time Sync

    Quote Originally Posted by passel View Post
    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.

  9. #9
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    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.

  10. #10

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Time Sync

    Quote Originally Posted by passel View Post
    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.

  11. #11
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Time Sync

    Quote Originally Posted by Poppa Mintin View Post
    ...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()
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  12. #12

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    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.

  13. #13
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Time Sync

    Quote Originally Posted by Poppa Mintin View Post
    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  14. #14

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    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.

  15. #15

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Time Sync

    Quote Originally Posted by dbasnett View Post
    FWIW - you can check your PC time with this,

    USNO Master Clock
    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.

  16. #16
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Time Sync

    Quote Originally Posted by Poppa Mintin View Post
    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.

  17. #17

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Time Sync

    Quote Originally Posted by OptionBase1 View Post
    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.

  18. #18
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    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.

  19. #19

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    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:
    1. Private Sub Form1_Load() Handles MyBase.Load
    2.         Label1.Text = Now.ToString
    3.         Sync()
    4.         Label2.Text = Now.ToString
    5.         Label3.Text = Now.ToString
    6.     End Sub
    7.  
    8.     Private Sub Sync()
    9.         Dim proc As New Process
    10.  
    11.         proc.StartInfo.FileName = "CMD"
    12.         proc.StartInfo.Arguments = "/C net start w32time & w32tm /resync /force"
    13.         proc.StartInfo.UseShellExecute = False
    14.         proc.StartInfo.Verb = "runas"
    15.         proc.Start()
    16.         proc.WaitForExit()
    17.     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.

  20. #20
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    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.

  21. #21

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Time Sync

    Quote Originally Posted by .paul. View Post
    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
  •  



Click Here to Expand Forum to Full Width