-
Mar 19th, 2025, 12:04 PM
#1
Thread Starter
PowerPoster
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.
-
Mar 19th, 2025, 12:16 PM
#2
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.
-
Mar 19th, 2025, 12:24 PM
#3
Re: Why does the exact method not work in another app. ?
 Originally Posted by OptionBase1
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...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 19th, 2025, 12:48 PM
#4
Re: Why does the exact method not work in another app. ?
 Originally Posted by .paul.
Code:
proc.StartInfo.Verb = "runas"
That's Run As Administrator...
Thanks, I glossed over that line.
-
Mar 19th, 2025, 12:52 PM
#5
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?
-
Mar 19th, 2025, 06:48 PM
#6
Thread Starter
PowerPoster
Re: Why does the exact method not work in another app. ?
 Originally Posted by OptionBase1
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.
-
Mar 19th, 2025, 07:03 PM
#7
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.
-
Mar 20th, 2025, 04:10 AM
#8
Thread Starter
PowerPoster
Re: Why does the exact method not work in another app. ?
 Originally Posted by PlausiblyDamp
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.
-
Mar 20th, 2025, 04:33 AM
#9
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 20th, 2025, 05:11 AM
#10
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.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 20th, 2025, 07:56 AM
#11
Thread Starter
PowerPoster
Re: Why does the exact method not work in another app. ?
 Originally Posted by .paul.
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.
-
Mar 20th, 2025, 11:04 AM
#12
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.
-
Mar 20th, 2025, 11:48 AM
#13
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.
-
Mar 20th, 2025, 12:31 PM
#14
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
-
Mar 21st, 2025, 03:44 AM
#15
Thread Starter
PowerPoster
Re: Why does the exact method not work in another app. ?
 Originally Posted by PlausiblyDamp
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.
-
Mar 21st, 2025, 04:00 AM
#16
Thread Starter
PowerPoster
Re: Why does the exact method not work in another app. ?
 Originally Posted by OptionBase1
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.
-
Mar 21st, 2025, 07:22 AM
#17
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.
-
Mar 21st, 2025, 11:11 AM
#18
Re: Why does the exact method not work in another app. ?
 Originally Posted by Poppa Mintin
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.
-
Mar 21st, 2025, 11:56 AM
#19
Thread Starter
PowerPoster
Re: Why does the exact method not work in another app. ?
 Originally Posted by OptionBase1
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.
-
Mar 21st, 2025, 12:13 PM
#20
Thread Starter
PowerPoster
Re: Why does the exact method not work in another app. ?
 Originally Posted by PlausiblyDamp
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.
-
Mar 21st, 2025, 12:24 PM
#21
Re: Why does the exact method not work in another app. ?
 Originally Posted by Poppa Mintin
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?
-
Mar 21st, 2025, 12:43 PM
#22
Re: Why does the exact method not work in another app. ?
 Originally Posted by Poppa Mintin
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.
-
Mar 21st, 2025, 07:21 PM
#23
Thread Starter
PowerPoster
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.
-
Mar 22nd, 2025, 10:48 AM
#24
Thread Starter
PowerPoster
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.
-
Mar 22nd, 2025, 11:16 AM
#25
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.
-
Mar 22nd, 2025, 06:56 PM
#26
Re: Why does the exact method not work in another app. ?
 Originally Posted by Poppa Mintin
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|