I have the following code, that I call when a page is loaded:
public StartPage()
{
NavigationPage.SetHasNavigationBar(this, false);
InitializeComponent();
Task.Run(() =>
{
Scan_Function();
});
}
In Scan_Function()
, I have a stopwatch and a bunch of if-statements, that are supposed to make the text of bleText
label change dynamically:
public void Scan_Function()
{
Stopwatch sw = new Stopwatch();
sw.Start();
while (sw.Elapsed.TotalMilliseconds <= 10000)
{
if (sw.Elapsed.TotalMilliseconds >= 1000 && sw.Elapsed.TotalMilliseconds <= 1050)
{
bleText.Text = "Scanning.";
}
else if (sw.Elapsed.TotalMilliseconds >= 2000 && sw.Elapsed.TotalMilliseconds <= 2050)
{
bleText.Text = "Scanning..";
}
....... continues
}
sw.Stop();
bleText.Text = "Couldn't connect";
bleText.TextColor = Color.Red;
}
I'm calling my Scan_Function() from Task.Run()
, because I want the method to run asynchronously with everything else so that the user can still press buttons etc.
My problem is though, that nothing happens! I'm suspecting that something is wrong with bleText.Text
, cause my debugger reaches it and seems to get stuck without updating anything or jumping out of the loop.
If i comment my if-statements, it runs through the loop, but get stuck once it reaches bleText.Text
outside the loop.
What am I doing wrong here?