1

I'm trying to print numbers with a period of 100ms. But I got all numbers after sleep 25*100ms. What I do wrong?

await Task.Run(delegate
{
    Application.Invoke(delegate
    {
        for (var i = 0; i < 25; i++)
        {
            Thread.Sleep(100);
            this.entry1.Text += i.ToString();
        }
    });
});

Edit:

This helped:

await Task.Delay(100);

But when I try do this:

public async void DoSomeWork()
{
    await Task.Run(delegate
    {
        Application.Invoke(async delegate
            {
                var errorTag = new TextTag("error");
                errorTag.Background = "#dc3122";
                errorTag.Foreground = "white";
                errorTag.Weight = Pango.Weight.Bold;
                textview1.Buffer.TagTable.Add(errorTag);

                Regex regex = new Regex(@"\w+");
                var matches = regex.Matches(this.text);
                foreach (Match match in matches)
                {
                    await Task.Delay(100);
                    var start = textview1.Buffer.GetIterAtOffset(match.Index);
                    var end = textview1.Buffer.GetIterAtOffset(match.Index + match.Length);
                    textview1.Buffer.ApplyTag(errorTag, start, end);
                }
            });
    });
}

and scroll textview's window I got error: http://pastebin.com/r94JQ1ZB

Edit 1:

Fix it. Rewrite like:

    public async Task DoMethod()
    {
        await Task.Run(async () => { 
            var errorTag = new TextTag("error");
            errorTag.Background = "#dc3122";
            errorTag.Foreground = "white";
            errorTag.Weight = Pango.Weight.Bold;
            textview1.Buffer.TagTable.Add(errorTag);

            Regex regex = new Regex(@"\w+");
            var matches = regex.Matches(this.text); //238618
            foreach (Match match in matches)
            {
                await Task.Delay(100);
                var start = textview1.Buffer.GetIterAtOffset(match.Index);
                var end = textview1.Buffer.GetIterAtOffset(match.Index + match.Length);
                Application.Invoke(delegate
                {
                    textview1.Buffer.ApplyTag(errorTag, start, end);
                });
            }
        });
    }
Gorodeckiy
  • 11
  • 3

0 Answers0