0

I faced a very weird situation. I'm working on an C# WPF application with MVVM pattern. The app has a page from where user can change system time. And there is a NEXT button, which is binded to an AsyncCommand.

The problem occurs when I press the NEXT button. The system time is updated (both date & time values), but right after that, the NEXT button freezes. It is visible, but the command has stopped working. However, I have another CANCEL button which is still available and works perfectly fine.

Could be something similar to this question: SystemClock freezes the system, but I couldn't find a potential cause or solution.

I tried to set system time in 2 ways: one using the SetSystemTime() API and the other one using a Powershell cmdlet. For both cases it ended up with that freezed NEXT button.

For any other cmdlets, the application works fine.

Does anybody faced the same situation ?

[UPDATE with code]:

    public void SetSystemDatetime(string selectedDate, string selectedTime)
    {
        var process = StartPowershellProcess($"Set-Date -Date '{selectedDate} {selectedTime}'");
        process.WaitForExit();
    }

    private static Process StartPowershellProcess(string arguments)
    {
        var startInfo = new ProcessStartInfo("powershell.exe")
        {
            UseShellExecute = false,
            WindowStyle = ProcessWindowStyle.Hidden,
            CreateNoWindow = true,
            Verb = "runas",
            Arguments = arguments,
            RedirectStandardOutput = true,
            RedirectStandardError = true
        };

        var process = Process.Start(startInfo);

        return process;
    }

Actual cmdlet: Set-Date -Date 'MM-dd-yyyy HH:mm'

  • Please show the code. – adv12 Dec 15 '21 at 17:05
  • Are you saying that your code stops at the call to `WaitForExit`? Or where does your app freeze? – mm8 Dec 15 '21 at 17:18
  • On my system, `powershell Set-Date -Date '12-15-2021 11:18'` generates an error: Set-Date : A positional parameter cannot be found that accepts argument '11:18' – adv12 Dec 15 '21 at 17:20
  • But that in itself shouldn't cause a hang. – adv12 Dec 15 '21 at 17:21
  • I am wondering how that code is called, maybe it was called in a non-async way, that would cause the GUI thread to hang until the process ended – Cleptus Dec 15 '21 at 17:24
  • The `-Date` parameter expects a **DateTime** object, not a **string** (because it depends on your system's locale whether or not this string can be parsed into a DateTime object or not) – Theo Dec 15 '21 at 19:41
  • @mm8 - The app itself is not freezing (rest of the buttons and UI elements can be used). It's just the button from where I trigger the command. That button does no longer send commands to the view model. Even if it is visible. A stranger behaviour is that setting time ahead don't cause this issue. Could be a coincidence or not. Don't know yet. I was just curios if anybody used that cmdlet before without any problems. – Cristian Stirbe Dec 15 '21 at 21:32
  • I tried to move this method of setting system time to a background worker or to a separate thread. But with the same result. And I can wait hours and that button still remains blocked. It's so akward. – Cristian Stirbe Dec 15 '21 at 21:34
  • If I call other cmdlet, I don't face this situation anymore – Cristian Stirbe Dec 15 '21 at 21:37
  • I don't know but Set-Date requiers administrator privilege. Even with the `Verb = "runas"` your script may wait for an elevation screen that may block the button? – Hazrelle Dec 15 '21 at 21:48
  • After days of investigation, the root cause of my problem seemed to be caused by the style of the button: Style="{StaticResource TertiaryButton}" Once I've removed this style, everything went fine. – Cristian Stirbe Dec 28 '21 at 14:44

0 Answers0