0

I'm trying to make it so that my form refreshes when I click a button. However I keep getting an error

'Cross-thread operation not valid: Control 'Form1' accessed from a thread other than the thread it was created on.'

 private void button1_Click(object sender, EventArgs e)
    {
        worker.DoWork += formReload;
        worker.RunWorkerAsync();
    }

    static BackgroundWorker worker = new BackgroundWorker();

    private void formReload(object sender, DoWorkEventArgs ev)
    {
        this.Refresh();
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {

    }

I've tried to research it, and I get that I have to use the Invoke method, however I don't understand where to put it, and why to use it? Any help would be much appreciated!

Thanks, Jarrod

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Jarrod
  • 13
  • 5
  • post some code of DoWork, you have to put it into that method. – Tobia Zambon Jul 09 '13 at 13:25
  • 3
    possible duplicate of [Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on](http://stackoverflow.com/questions/142003/cross-thread-operation-not-valid-control-accessed-from-a-thread-other-than-the) – Tim Jul 09 '13 at 13:27
  • move Refresh from `bg_DoWork` to `bg_runworkercompleted` – Bolu Jul 09 '13 at 13:27
  • You could have Googled the error message first... – Thorsten Dittmar Jul 09 '13 at 13:28
  • Hi, thanks for the fast reply. I have nothing in DoWork, so I posted the this.Refresh(); in there, however now there's no error, but it doesn't work either. What I have in my code is a variable, and in the form_load there's a switch case, so if the variable = 1, then do the code that's in case 1. – Jarrod Jul 09 '13 at 13:30
  • I did try to google the error, hence why I know about the invoke method, however I didn't understand it, nor where to put it. – Jarrod Jul 09 '13 at 13:31

1 Answers1

4

Actually your code does nothing, the DoWork is unnecessary. You can rewrite your code as:

private void button1_Click(object sender, EventArgs e)
{
    worker.RunWorkerAsync();
}

static BackgroundWorker worker = new BackgroundWorker();

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    if (this.InvokeRequired)
        this.Invoke(new Action(()=>Refresh()));
}

Assuming that you subscribed the DoWork method in the contructor using

worker.DoWork += backgroundWorker1_DoWork;

Take note that a Refresh doesn't change anything. What do you have to refresh?

Tobia Zambon
  • 7,479
  • 3
  • 37
  • 69
  • Hi, thanks! The error doesn't appear anymore, but the form doesn't seem to be reloading – Jarrod Jul 09 '13 at 13:35
  • I've edited the reply, what do you have to reload? Where is the code to reload the form? – Tobia Zambon Jul 09 '13 at 13:40
  • What I thought it'd do was refresh everything (even in form_load). Because I have a int called refresh, so everytime the form is refreshed is adds one. Then my idea was to run a switch which gets the refresh int and draw different things in each case? If that makes sense – Jarrod Jul 09 '13 at 13:43
  • the Form_Load is called only once (when the form is actually loaded the first time). If you want to call it you don't have to use the `Refresh` but call it explicitly: use `this.Invoke(new Action(()=>form_Load(null,null)));` – Tobia Zambon Jul 09 '13 at 13:46
  • Thankyou very much :) Sorry for wasting your time – Jarrod Jul 09 '13 at 13:49
  • No Problem, I see you are new here: if the answer fits your need please mark it as accepted (put the green tick under the answer's score), thank you – Tobia Zambon Jul 09 '13 at 13:52