Jump to content
  • Advertisement
Sign in to follow this  
homer_3

[.net] Help creating threads

This topic is 3404 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok I don't see what I'm missing. I've been looking and some tutorials and can't seem to create a thread. I have this class for scrolling the map.
class MapScroller
    {
        const int X_INC = 20;
        const int Y_INC = 20;
        int dir = 1;

        public void scrollH(Sprite map, Player player, int len)
        {
            if (len < 0)
                dir = -1;
            else
                dir = 1;
            len = Math.Abs(len);

            for (int i = 0; i < len; i+=X_INC)
            {
                map.position.X += X_INC * dir;
                Thread.Sleep(TimeSpan.FromMilliseconds(500));
            }
        }

        public void scrollV(Sprite map, Player player, int len)
        {
            if (len < 0)
                dir = -1;
            else
                dir = 1;
            len = Math.Abs(len);

            for (int i = 0; i < len; i += Y_INC)
            {
                map.position.Y += Y_INC * dir;
                Thread.Sleep(TimeSpan.FromMilliseconds(500));
            }
        }
    }

Then I try to create a thread from it with
 MapScroller mapScroller = new MapScroller();
 ThreadStart tt = new ThreadStart(mapScroller.scrollH(curMap, player, 800));
 Thread   mapScrollerThread = new Thread(tt);

But mapScroller.scrollH(curMap, player, 800) has an error that says "method name expected". What am I missing?

Share this post


Link to post
Share on other sites
Advertisement
mapScroller.scrollH(curMap, player, 800) calls the function right then and there. What you want is a ThreadStart delegate. One way to do that would be:

class ScrollH {
Sprite map;
Player player;
int len;
MapScroller scroller;

ScrollH(Sprite map, Player player, int len, MapScroller scroller) {
this.map = map;
this.player = player;
this.len = len;
this.scroller = scroller;
}

void Execute() {
scroller.scrollH(map, player, len);
}
}

ThreadStart tt = new ThreadStart(new ScrollH(curMap, player, 800, mapScroller).Execute);

Share this post


Link to post
Share on other sites
SiCrane is right, the constructor of Thread requires a ParameterizedThreadStart or ThreadStart delegate (.NET equivalent of function pointers), you may want to read into them: http://en.wikipedia.org/wiki/Delegate_(.NET)

You can do funny stuff with inline delegates:

Thread thread = new Thread(new ThreadStart(delegate {
// do code here
}));
thread.Start();

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!