Results 1 to 26 of 26

Thread: Why does the exact method not work in another app. ?

  1. #1

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

    Why does the exact method not work in another app. ?

    Hi,
    This code works very well in at least two of my previous app's. However when I try it in a new app it doesn't seem to work, at least it doesn't update the 'Time and Date > Last successful sync' on the settings page (unlike the other two app's). None of these app's have any special imports associated with time. The subroutine was copied and pasted directly from the other two.
    Code:
        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.StartInfo.CreateNoWindow = True
            proc.Start()
            proc.WaitForExit()
        End Sub
    The other two app's were written in VS2019 in a Win.10 machine, this new app is in Win.11 but still VS2019.
    The new app runs the code but doesn't update the the info page.

    Poppa
    Along with the sunshine there has to be a little rain sometime.

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

    Re: Why does the exact method not work in another app. ?

    Maybe you aren't running the new app with admin rights. I don't think you can interact with services like that without admin rights.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,017

    Re: Why does the exact method not work in another app. ?

    Quote Originally Posted by OptionBase1 View Post
    Maybe you aren't running the new app with admin rights. I don't think you can interact with services like that without admin rights.
    Code:
    proc.StartInfo.Verb = "runas"
    That's Run As Administrator...

  4. #4
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,511

    Re: Why does the exact method not work in another app. ?

    Quote Originally Posted by .paul. View Post
    Code:
    proc.StartInfo.Verb = "runas"
    That's Run As Administrator...
    Thanks, I glossed over that line.

  5. #5
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,511

    Re: Why does the exact method not work in another app. ?

    What happens if you open a cmd window (with admin rights) and manually type in that command yourself?

  6. #6

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

    Re: Why does the exact method not work in another app. ?

    Quote Originally Posted by OptionBase1 View Post
    What happens if you open a cmd window (with admin rights) and manually type in that command yourself?
    I don't know how to do that in Win.11
    Neither do I know how to string the command together.




    Poppa
    Along with the sunshine there has to be a little rain sometime.

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

    Re: Why does the exact method not work in another app. ?

    On the start menu find command prompt, right click run as administrator.

    You would just type the "net start..." command. All your code is doing is running a command you could type at a command prompt, so just typing it yourself is a really easy way to test if it works.

  8. #8

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

    Re: Why does the exact method not work in another app. ?

    Quote Originally Posted by PlausiblyDamp View Post
    On the start menu find command prompt, right click run as administrator.

    You would just type the "net start..." command. All your code is doing is running a command you could type at a command prompt, so just typing it yourself is a really easy way to test if it works.
    This is Win.11. There is no command prompt in the start menu.
    To get to the command prompt I have to use Windows key + R, but then there's no administrator option.
    (Although I've also pinned it to the task bar for convenience, but there's still no administrator option.)
    Along with the sunshine there has to be a little rain sometime.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,017

    Re: Why does the exact method not work in another app. ?

    Use the search box in the taskbar to search for ‘cmd’. When it shows up in your start menu, right click and select run as administrator

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,017

    Re: Why does the exact method not work in another app. ?

    There’s plenty of advice on many topics to be found via Google

    Press Windows Key + R to open the Run dialog, then type cmd and press Ctrl + Shift + Enter to run it as administrator.

  11. #11

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

    Re: Why does the exact method not work in another app. ?

    Quote Originally Posted by .paul. View Post
    There’s plenty of advice on many topics to be found via Google
    Yes there are, thanks Paul, I finally got command prompt up and running as administrator, via advice on the web, stopped the time service and restarted it, used it to re-sync the time, all ok, re-sync time recorded in 'Time and Date > Last successful sync'. Tried my new app again, still no joy.

    The previous two app's do up-date the sync on this same laptop.
    Along with the sunshine there has to be a little rain sometime.

  12. #12
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,511

    Re: Why does the exact method not work in another app. ?

    If the desired process is to first stop the service and then start it, then that is what you should write code to do.

    Right now, all your code is doing is trying to start it. If the service is already running, trying to start it accomplishes nothing.

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

    Re: Why does the exact method not work in another app. ?

    You could try and get the error from the command prompt by Redirecting the standard error e.g.

    Code:
         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.StartInfo.CreateNoWindow = True
         proc.StartInfo.RedirectStandardError = True
         proc.Start()
         proc.WaitForExit()
    
         Dim errorMessage = proc.StandardError.ReadToEnd()
    That code would put any errors from trying to run the command into the errorMessage variable - that way you could log it, display it, whatever.

  14. #14
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,487

    Re: Why does the exact method not work in another app. ?

    How to add Command Prompt to the Windows 11 Start menu:

    https://www.youtube.com/watch?v=9-dBAt6IjJw

  15. #15

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

    Re: Why does the exact method not work in another app. ?

    Quote Originally Posted by PlausiblyDamp View Post
    You could try and get the error from the command prompt by Redirecting the standard error e.g.

    Code:
         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.StartInfo.CreateNoWindow = True
         proc.StartInfo.RedirectStandardError = True
         proc.Start()
         proc.WaitForExit()
    
         Dim errorMessage = proc.StandardError.ReadToEnd()
    That code would put any errors from trying to run the command into the errorMessage variable - that way you could log it, display it, whatever.
    Thanks PD,
    I copied that directly into my code ran it, but I think it threw an error...

    System.InvalidOperationException
    HResult=0x80131509
    Message=StandardError has not been redirected.
    Source=System
    StackTrace:
    at System.Diagnostics.Process.get_StandardError()
    at MultiClock.Form1.Sync() in C:\Visual Studio Projects\Projects\Projects\MultiClock\Form1.vb:line 344
    at MultiClock.Form1.Form1_Load() in C:\Visual Studio Projects\Projects\Projects\MultiClock\Form1.vb:line 39
    at MultiClock.Form1._Lambda$__R0-1(Object a0, EventArgs a1)
    at System.EventHandler.Invoke(Object sender, EventArgs e)
    at System.Windows.Forms.Form.OnLoad(EventArgs e)
    at System.Windows.Forms.Form.OnCreateControl()
    at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
    at System.Windows.Forms.Control.CreateControl()
    at System.Windows.Forms.Control.WmShowWindow(Message& m)
    at System.Windows.Forms.Control.WndProc(Message& m)
    at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
    at System.Windows.Forms.Form.WmShowWindow(Message& m)
    at System.Windows.Forms.Form.WndProc(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
    at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

    This exception was originally thrown at this call stack:
    [External Code]
    MultiClock.Form1.Sync() in Form1.vb
    MultiClock.Form1.Form1_Load() in Form1.vb
    [External Code]
    ...or maybe not ?
    Along with the sunshine there has to be a little rain sometime.

  16. #16

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

    Re: Why does the exact method not work in another app. ?

    Quote Originally Posted by OptionBase1 View Post
    If the desired process is to first stop the service and then start it, then that is what you should write code to do.

    Right now, all your code is doing is trying to start it. If the service is already running, trying to start it accomplishes nothing.
    You may've missed the fact that I couldn't find how to open the command prompt as administrator, having done that I was only testing that I had it right.
    Along with the sunshine there has to be a little rain sometime.

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

    Re: Why does the exact method not work in another app. ?

    Did you copy all of the code, including the bit about redirecting StandardError? Also, the error message itself tells you what the problem is.

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

    Re: Why does the exact method not work in another app. ?

    Quote Originally Posted by Poppa Mintin View Post
    You may've missed the fact that I couldn't find how to open the command prompt as administrator, having done that I was only testing that I had it right.
    And your manual command prompt test was invalid because you took additional steps that your posted code isn't doing. Your posted code is not stopping the service first, and you did that in your manual test. That was my point.

  19. #19

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

    Re: Why does the exact method not work in another app. ?

    Quote Originally Posted by OptionBase1 View Post
    And your manual command prompt test was invalid because you took additional steps that your posted code isn't doing. Your posted code is not stopping the service first, and you did that in your manual test. That was my point.
    Stopping and starting the service was only a test, it has nothing to do with my app.
    Along with the sunshine there has to be a little rain sometime.

  20. #20

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

    Re: Why does the exact method not work in another app. ?

    Quote Originally Posted by PlausiblyDamp View Post
    Did you copy all of the code, including the bit about redirecting Standard Error? Also, the error message itself tells you what the problem is.
    Oops! I missed that you'd added more than just the last command. (sorry)

    I've just copied your entire code and tried it again.
    No error this time but nothing else either, it ran normally but didn't record anything in the 'Time and Date > Last successful sync', so I'm still assuming that it didn't do it since it's still showing the time I sync'd this morning. Given that the new app is a clock it would be nice to sync it before it runs.
    Along with the sunshine there has to be a little rain sometime.

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

    Re: Why does the exact method not work in another app. ?

    Quote Originally Posted by Poppa Mintin View Post
    Oops! I missed that you'd added more than just the last command. (sorry)

    I've just copied your entire code and tried it again.
    No error this time but nothing else either, it ran normally but didn't record anything in the 'Time and Date > Last successful sync', so I'm still assuming that it didn't do it since it's still showing the time I sync'd this morning. Given that the new app is a clock it would be nice to sync it before it runs.
    So there wasn't any error message in the errorMessage variable?

    Out of interest, is the windows PC not automatically keeping the time in sync anyway? Not entirely sure what you mean when you say it is "still showing the time I sync'd this morning". Di you mean the time in your app isn't changing, or Windows' time isn't changing or something else entirely?

  22. #22
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,511

    Re: Why does the exact method not work in another app. ?

    Quote Originally Posted by Poppa Mintin View Post
    Stopping and starting the service was only a test, it has nothing to do with my app.
    If that is what you believe then you clearly have no idea what your code is actually doing. It seems like you're intentionally just arguing with zero interest in actually trying to figure out why this isn't working.

    Good luck.

  23. #23

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

    Re: Why does the exact method not work in another app. ?

    My new app is for a couple of quite old laptops, this one does auto sync when I open it for the first time each day.
    Oh! I guess I ought to see if it works on those two older machines. It still bugs me that it doesn't work on this machine though.
    I'll try to remember to try it on the others tomorrow. (My short term memory is shot! I had to give up the bridge club because I sometimes forgot the bid my partner had opened with! )
    Along with the sunshine there has to be a little rain sometime.

  24. #24

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

    Re: Why does the exact method not work in another app. ?

    Tried the new app in the other laptops but it didn't sync the time in either of those.
    Along with the sunshine there has to be a little rain sometime.

  25. #25
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,511

    Re: Why does the exact method not work in another app. ?

    I would guess that the other applications that you say this code works in has additional code in them that is first launching a command prompt and issuing a "net stop w32time" command to stop the Windows Time service. And after that is when the code that you posted above is being ran.

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

    Re: Why does the exact method not work in another app. ?

    Quote Originally Posted by Poppa Mintin View Post
    Tried the new app in the other laptops but it didn't sync the time in either of those.
    Do the other apps run as admin? Or if not do you get prompted to elevate when you run the above command?

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