0

Basically, I cannot run a thread when this procedure is non-static. If it comes static, I cannot access the GUI's form.

How do I run a thread on a non-static procedure?

Thread check = new Thread(refreshInfo);

    private void refreshInfo()
    {
        if (Program.loggedIn == true)
        {
            btnLogIn.Visible = false;
            btnRegister.Visible = false;

            lblLoggedIn.Text = "Logged in";

            btnUserSettings.Enabled = true;
        }
        else
        {

        }
    }

Error

Error 12 A field initializer cannot reference the non-static field, method, or property 'BLHClient.BLHClient.refreshInfo()'

7
  • 1
    You need to assign the Thread in your constructor.
    – Rob
    Apr 7, 2016 at 11:36
  • 1
    Stupid mistake. I've fixed this before. Thanks Rob.
    – nerraD
    Apr 7, 2016 at 11:37
  • 2
    This code will crash. You can't modify a UI control from a background thread. Why use a raw thread anyway instead of using Tasks and async/await? You could perform whatever needed asynchronous processing and get back to the UI thread with a simple await RunMyAsyncMethod(); call Apr 7, 2016 at 11:40
  • How does await RunMyAsyncMethod work? Could you give me a tutorial post? @PanagiotisKanavos
    – nerraD
    Apr 7, 2016 at 11:42
  • Use MVVM with binding and INotifyPropertyChanged - this will be cleaner and quicker to implement.
    – weismat
    Apr 7, 2016 at 11:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.