0

This code works when not put in a thread, why does the label not change when it is put into a thread? Doing a Thread.Join also works. But that just slows down the UI..


public void Filechecker()
    {
        new Thread(() =>
        {
            if (status == 1)
        {
            for (int i = 0; i < 100; i++)
            {
                if (status == 0)
                {
                    Labelstatus.Text = "Files okay";
                    Labelstatus.ForeColor = Color.Green;
                    break;
                }
                Thread.Sleep(100);
                if (i == 99)
                {
                    Labelstatus.Text = "Warning no activity 1";
                    Labelstatus.ForeColor = Color.Red;
                }
            }
        }
        }).Start();
    }
  • 1
    Asp.net does it's work on a main request thread. You shouldn't spin up threads in asp.net, and for more reasons than you can't update your controls. Either use async/await where you think you want a thread or just do it all synchronously and let the runtime do what it was highly tuned to do. – Crowcoder Mar 21 '19 at 12:21
  • Have a code example? – Jake Mogensen Mar 21 '19 at 12:22
  • Of what? Don't do that stuff in a new thread. Just do it in a regular method. – Crowcoder Mar 21 '19 at 12:24
  • Please clarify your question, you added tag `asp.net` but it seems you are talking about win forms. – bot_insane Mar 21 '19 at 12:25
  • this says textbox..but the same concept works. https://stackoverflow.com/questions/1136399/how-to-update-textbox-on-gui-from-another-thread – 2174714 Mar 21 '19 at 12:25
  • It says labelstatus does not contain a definition for BeginInvoke? – Jake Mogensen Mar 21 '19 at 12:37
  • @JakeMogensen If you are using asp.net as you say you are then you can ignore the advice that tells you to do things that are only Winforms. – Crowcoder Mar 21 '19 at 12:42
  • I don't understand what you mean with " Just do it in a regular method", i need a thread to prevent UI slowdown. @Crowcoder – Jake Mogensen Mar 21 '19 at 12:45
  • You are not speeding anything up by using a thread, the request cannot return until the work is done anyway. What I mean is *just don't use a thread*. You don't need it and it is not gaining you anything, and it is actually detrimental to the runtime. – Crowcoder Mar 21 '19 at 12:47
  • Yes i am, the rest of my code works this way, i just want to change the label text in a thread why is that so hard? – Jake Mogensen Mar 21 '19 at 12:49
  • "why is that so hard?" this is why: https://stackoverflow.com/questions/661561/how-do-i-update-the-gui-from-another-thread – Jeremy Mar 21 '19 at 12:53
  • Still completely lost, im using asp.net and all those examples are for windows form. – Jake Mogensen Mar 21 '19 at 12:59
  • While the API is different, the concept applies regarding accessing the UI from another thread. – Crowcoder Mar 21 '19 at 13:05
  • Fixed it with: public static string result; – Jake Mogensen Mar 21 '19 at 13:13
  • 1
    is that threadsafe? what if multiple threads try to update result at the same time? what if you are reading result, and another thread is going to update result? – 2174714 Mar 21 '19 at 13:21

0 Answers0