user1853427
user1853427

Reputation: 3

Thread calling problems

I'm a bit of a newbie at this but I am trying to get the UI on a Reversi game to run on a different thread to the move selection part but I am having some trouble calling the thread on the button click

private void playerMoveOKButton_Click(object sender, EventArgs e)
{
       ReversiT.Invoke();
}

public void ReversiT() {...}

Upvotes: 0

Views: 150

Answers (5)

Daniel Kelley
Daniel Kelley

Reputation: 7747

I think you need to think about that you are actually trying to achieve here. Running code on a separate thread in a UI is a technique used to stop the UI from hanging. However, some tasks simply have to occur on the UI thread and so can't be run from another thread.

You need to break your logic out such that you can identify which parts need to run on the UI thread (anything that interacts with a control on your UI) and thus anything that can run on a separate thread.

You would end up with code like (as an example):

private void playerMoveOKButton_Click(object sender, EventArgs e)
{
    //thread is merely used as an example
    //you could also use a BackgroundWorker or a task
    var thread = new Thread(NonUiLogic);
    thread.Start();
}

private void NonUiLogic()
{
    ...
    //execute logic that doesn't touch UI
    ...

    BeginInvoke(ReversiT);
}

public void ReversiT() {...}

Once you have been through that exercise you may find that there is actually very little that can happen outside of the UI thread and so you really have nothing to gain from using threads.

Upvotes: 0

Bridge
Bridge

Reputation: 30711

I would create a BackgroundWorker to handle everything for me, setting it's DoWork event to call your move method (making sure that your move method doesn't touch the UI, or if it has to, invoking the controls on the UI thread).

I'd also set up a method to update the UI on the BackgroundWorker's RunWorkerCompleted event.

Now on your button click event above, call the BGW's RunWorkerAsync() method.

Upvotes: 0

Blachshma
Blachshma

Reputation: 17395

If you're trying to create a new thread, you can do something like this:

Thread thread = new Thread(ReversiT);
thread.Start();

Invoke is used for a different purpose though. It is used to run a method on a specific thread (for instance, if you run a piece of code on a separate thread but want to make UI changes, you will want to use Invoke to make those changes on the UI thread)

Upvotes: 1

Tilak
Tilak

Reputation: 30728

Use following

this.Invoke(ReversiT);

Upvotes: 0

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56727

You can not invoke a method like that. You can only invoke delegates. Also, calling Invoke doesn't spawn a new thread.

You can read this tutorial about delegates, and this one about threads. Also, your question leaves much space for discussion:

  1. What do you expect from using threads?
  2. Have you considered different options for doing background work?

etc.

Upvotes: 0

Related Questions