0

So, I am creating a C++ UWP application that when opened, displays current date and time. In addition, I need to also find a way so that I can display the day of the week as well. For the day of the week, I am using this code:

#include <ctime>
#include <time.h>
#include <chrono>
//under Mainpage::Mainpage()
time_t timeObj = time(nullptr);
tm aTime;
localtime_s(&aTime, &timeObj);
String ^ weekofdayout;
switch (aTime.tm_wday)
{
case 1:
    weekofdayout = "Monday";
    break;
case 2:
    weekofdayout = "Tuesday";
    break;
case 3:
    weekofdayout = "Wednesday";
    break;
case 4:
    weekofdayout = "Thursday";
    break;
case 5:
    weekofdayout = "Friday";
    break;
default:
    weekofdayout = "(NULL)";
    break;
}
timetext->Text += weekofdayout;

However, whenever I run it, the textbox returns: (NULL) the default case. I don't know whats causing it.

I have taken the system time before successfully in windows forms c++ with this code:

private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) {
DateTime datetime = DateTime::Now;
this->label3->Text = datetime.ToString();}

I was unable to port this over to UWP C++ because I think it uses the system namespace. Any ideas on how I would be able to recreate the same effect in c++?

For the stopwatch, I haven't found any real documentation online regarding UWP C++ stopwatch but I did find this: https://iot-developer.net/windows-iot/uwp-programming-in-c/timer-and-timing/stopwatch It isn't for C++ UWP but it is for C# UWP. I tried to make my own timer with a global function but it just freezes the program until the code is done running:

int seconds = 0;
for (int i = 0; ; i++)
    {
seconds++;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
if (stopwatch == false)
            {
        break;
            }
}
return seconds;

I'm pretty stumped, usually, I would find the answer online but due to the low documentation of UWP C++, I have no idea where to find the answer.

Richard Wei
  • 87
  • 2
  • 8
  • Perhaps you're running into an issue where it's on the wrong thread? Since you want to display the result to the UI, what about using `DispatcherTimer`? https://learn.microsoft.com/en-us/uwp/api/Windows.UI.Xaml.DispatcherTimer – Lee McPherson Mar 11 '19 at 20:34
  • Oh, and since you're comfortable with C++ and the std library, you should switch from C++/CX to C++/WinRT https://learn.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/index – Lee McPherson Mar 11 '19 at 20:35

0 Answers0