0

I have a strange issue. I have following code in my function:

if (this.InvokeRequired)
{
    this.BeginInvoke((MethodInvoker)delegate
    {
        LoadGamesAndRefreshView();
    });

    return;
}

presenter.LoadGames();
presenter.ApplyFilter();

The presenter Method presenter.ApplyFilter() calls the View to update an UI Element.

The posted code (the function) is called over WCF, so from a different thread, so I have to invoke this. This works fine, but I have to call this function sometimes in a loop, so there are coming approx 15-20 calls of this function over WCF. When I do this, my client crashes because it was in wrong thread and cant change UI elements.

For each Function the server creates his own thread.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 2
    I think you need to use `presenter.InvokeRequired` rather than `this.InvokeRequired`. – Gabe Aug 06 '14 at 05:52
  • Is that code in the `LoadGamesAndRefreshView` method? – jmcilhinney Aug 06 '14 at 05:55
  • Yes this is the code in this method! The Method is on my View!! The presenter.ApplyFilter() calls a Method on my view, which has this invokerequired snippet too, and their it crashes!! – user2964559 Aug 06 '14 at 06:08

1 Answers1

0

This should solve your problem.
You have to check if the presenter needs an invoke.

    void LoadGamesAndRefreshView()
    {
        if (presenter.InvokeRequired)
        {
            this.BeginInvoke((MethodInvoker)Delegate
            {
                LoadGamesAndRefreshView();
            });
            return;
        }
        presenter.LoadGames();
        presenter.ApplyFilter();
    }
CoLiNaDE
  • 338
  • 2
  • 9