简体   繁体   中英

Xamarin iOS Sleep UI thread not working

I have a Thread/sleep problem with my Xamarin iOS application. I want to create a simple game, kind of memory game. When pressing the start button there will be 4 buttons which will change it fonts. Start with 1 then 2 and so on. The problem is that I want to have a pause in the method which shows the button, because the user need to see the changed color for a few seconds. I think the multithreading catch up directly the next task so the sleep is gone :( How to fix this? Thanks!

Code:

    partial void btnStart (NSObject sender)
    {
        teller++;

        InvokeOnMainThread( ()=> { for (int i = 0; i < teller; i++) {
                String randomGetal = randomizer();
                Lijst.Add(new Opslag() {nummerGetal = teller, getal = randomGetal});
                ThreadPool.QueueUserWorkItem(o => InvokeOnMainThread(() => {ButtonColor(randomGetal);}));
        };
        });
        lblRandom1.Text = Lijst.Count.ToString();
    }

    public void ClearButton()
    {
        btnA.SetTitleColor (UIColor.Green, UIControlState.Normal);
        btnB.SetTitleColor (UIColor.Green, UIControlState.Normal);
        btnC.SetTitleColor (UIColor.Green, UIControlState.Normal);
        btnD.SetTitleColor (UIColor.Green, UIControlState.Normal);
    }

    public void ButtonColor(string a)
    {

        if (a == "A") {
            btnA.SetTitleColor (UIColor.Magenta, UIControlState.Normal);
            Thread.Sleep(2000);
            ClearButton ();
        }

        if (a == "B") {
            btnB.SetTitleColor (UIColor.Magenta, UIControlState.Normal);
            Thread.Sleep(2000);
            ClearButton ();
        }

        if (a == "C") {
            btnC.SetTitleColor (UIColor.Magenta, UIControlState.Normal);
            Thread.Sleep(2000);
            ClearButton ();
        }

        if (a == "D") {
            btnD.SetTitleColor (UIColor.Magenta, UIControlState.Normal);
            Thread.Sleep(2000);
            ClearButton ();
        }
    }

    public string randomizer()
    {
        int randomNummer = random.Next (0, 4);
        if (randomNummer == 0)
            return "A";
        if (randomNummer == 1)
            return "B";
        if (randomNummer == 2)
            return "C";
        if (randomNummer == 3)
            return "D";
        else
            return "Empty";
    }

}

public class Opslag
{
    public int nummerGetal {get;set;        
    }

    public string getal {
        get;
        set;
    }

}

}

I had similar problem with Xamarin Android. Thread.Sleep() didn't work:
- neither from System.Threading.Thread.Sleep()
- nor from Java.Lang.Thread.Sleep()

Spent 2h trying to work out solution.
Found above answer , but as is a little complex, decided to distill core problem.

Solution

Replace 'thread based' sleep, like here:

DoSth() {
  Thread newThread = new Thread(new ThreadStart(DoWork));
  newThread.Start();
}

void DoWork() {
  Thread.Sleep(5000);
  // do sth after 5 secs
}

with 'async based' method:

DoSth() {
  DoWorkAsync();
}

async void DoWorkAsync() {
  await Task.Delay(5000);
  // do sth after 5 secs
}

BTW. Problem occurs only on the very beggining after app start.

I know you got it to work but here is a quick suggestion to simplify your code:

    private UIButton[] buttons;
    private Random random;

    public override void ViewDidLoad()
    {
        base.ViewDidLoad ();

        this.buttons = new [] { btnA, btnB, btnC, btnD };

        this.random = new Random ();

        // put in an event handler to fire up ButtonColor
    }

    private async void ButtonColor()
    {
        var r = random.Next (0, this.buttons.Length);
        if (r < this.buttons.Length)
        {
            var button = this.buttons [r];
            var curColor = button.TitleColor(UIControlState.Normal);
            button.SetTitleColor(UIColor.Magenta, UIControlState.Normal);
            await Task.Delay (2000);
            button.SetTitleColor(curColor, UIControlState.Normal);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM