|
OG is correct in his assessment of what you are trying to do. Something he didn't mention, but which is highly pertinent, is that we don't write applications for others. We assist you if you have issues that you are trying to sort out.
If there were an article that already did what you wanted it to do, then you could use that code but, as there isn't, I would suggest you either abandoned your attempts to cheat, or went to a site where people will write code for you, for money.
|
|
|
|
|
Hello, this is my first post. I am stuck and could really use some help. This is a tough question to answer, and even tougher to ask, so I’m not getting my hopes up for an answer but I thought I would give it a try anyway.
I'm creating a simulation where objects move around in a 2 dimensional array. Each object in the array runs in its own thread. This means that each object moves and interacts with other objects in real time rather than sequentially.
For ease of explanation, we can consider the 2 dimensional array as a “field” and each object in it as a “sheep”. There is a possibility of a sheep breeding and adding another sheep to the field. This new sheep needs to be added to the field and start its own thread so it can move around. This is where I’m having problems.
I’m basically trying to start a new thread within another thread and it’s not working. The sheep will successfully breed and add the new sheep to the field but the new sheep fails to start moving around.
When the program is started, the field class populates itself with sheep and iterates through all of its sheep and starts a bunch of threads to move them around. Like I said, each sheep runs in its own thread.
To move, each sheep needs to know about the field it’s moving in and decide the next best position to move. To do this, each sheep has a “move” method that accepts the field as a parameter. It then examines the field and decides on an adjacent position to move and updates the field. So basically I pass the entire field to each sheep and it decides where to move itself and updates the field. The following two methods are part of the field class…
public void run()
{
foreach (IActor actor in actorsOnField)
{
Thread thread = new Thread(delegate() { move(actor); });
thread.Start();
}
}
public void move(IActor actor)
{
while (true)
{
int rand = RandomNumber.GetRandomNumber(1000, 2000);
Thread.Sleep(rand);
if (!isPaused)
{
lock (_grid)
{
actor.move(this);
}
}
}
}
When I click a button, the run method is executed and the sheep start moving around. The sheep move method looks like this…
public void move(Field field)
{
IActor[,] grid = field.Grid;
Location moveTo = MoveToRandomLocation(field);
if (moveTo != null)
{
grid[CurrentLocation.Y, CurrentLocation.X] = null;
CurrentLocation.X = moveTo.X;
CurrentLocation.Y = moveTo.Y;
grid[CurrentLocation.Y, CurrentLocation.X] = this;
}
reproduce(field);
}
The reproduce method looks like this…
public Herbivore reproduce(Field field)
{
int chanceToReproduce = 10;
List<IActor> neighbours = GetAdjacentActors(field);
chanceToReproduce = neighbours.Count * chanceToReproduce;
int rand = RandomNumber.GetRandomNumber(0, 100);
List<Location> adjacentLocations = this.GetAdjacentLocations(field);
List<Location> emptyLocations = new List<Location>();
foreach(Location loc in adjacentLocations)
{
if (field.Grid[loc.Y, loc.X] == null)
emptyLocations.Add(loc);
}
if (chanceToReproduce >= rand && emptyLocations.Count > 0)
{
Herbivore herb = new Herbivore(emptyLocations[0].X, emptyLocations[0].Y);
field.ActorsOnField.Add(herb);
field.Grid[emptyLocations[0].Y, emptyLocations[0].X] = herb;
return herb;
}
return null;
}
I have tried many things but I can’t get the new sheep to move around once they have been created. Does anyone have any ideas on how I can do this?
Thanks
|
|
|
|
|
I'd like to zero in on where you say you try to start a new thread and it's not working. What does not working mean?
Are you getting any error messages? Post the code where you create the new thread.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Thanks for the reply Richard!
I should have included that part in the code but I left it out because I couldn't get it working. I have tried a few things unsuccessfully and will list some of my failed attempts...
My first attempt made it so that if a "sheep" were to reproduce, the new animal would be put into the proper location in the 2D array (the grid I call it) from the animals "move" method. Every time a sheep executes its "move" method it will return the new animal if it successfully reproduced. I then take the new animal (if one was created) and attempt to start a new thread in the "move" method in the Field class like this...
public void move(IActor actor)
{
while (true)
{
int rand = RandomNumber.GetRandomNumber(1000, 2000);
Thread.Sleep(rand);
if (!isPaused)
{
lock (_grid)
{
IActor bingo = actor.move(this);
if(bingo != null)
{
Thread thread = new Thread(delegate() { move(bingo); });
thread.Start();
}
}
}
}
}
The "move" method for the "Herbivore" class looked like this...
public IActor move(Field field)
{
IActor[,] grid = field.Grid;
Location moveTo = MoveToRandomLocation(field);
if (moveTo != null)
{
grid[CurrentLocation.Y, CurrentLocation.X] = null;
CurrentLocation.X = moveTo.X;
CurrentLocation.Y = moveTo.Y;
grid[CurrentLocation.Y, CurrentLocation.X] = this;
}
IActor bingo = reproduce(field);
if(bingo != null)
{
return bingo;
}
return null
}
This did not produce any errors but the newly created animals just wouldn't move. I have tried to run over things with the debugger, and could see the new threads being hit, but it's very hard to tell whats going on when I'm hitting line breaks coming from like 30 + threads at the same time. It makes it almost impossible to debug the problem.
|
|
|
|
|
For multi-threading issues, its better to use trace messages vs. debugger breakpoints for the reason you just mentioned. Of course, your trace message should include the threadId so you can follow along. I.e. "[<threadId>] - <trace text>".
The grid is shared among threads, right? Seems to me like you aren't synchronizing across threads. You must for ALL shared resources (that are not static read-only).
|
|
|
|
|
Hi SledgeHammer, thanks for the advice. I will create a log file and see if I can't figure it out.
Yes the grid is being shared among all the threads. Each animal gets passed the grid (well actually the whole Field class) and makes its move by updating the grid. I use the "lock" keyword to prevent two or more threads from trying to update the grid at the same time. Everything works well aside from the reproducing of the animals. I can't get the newborns to start moving around. Can you please explain how else I can go about synchronizing things better?
Thanks.
|
|
|
|
|
It turns out this whole thing was just a bug in my code when placing new animal on the field. I have it working now. If an animal reproduces it passes the new object back to the field and starts a new thread successfully. The two Field methods now look like this...
public void run()
{
this.RestartThreads = false;
foreach (IActor actor in actorsOnField)
{
Thread t = new Thread(delegate() { move(actor); });
t.Start();
RunningThreads.Add(t);
}
}
public void move(IActor actor)
{
while (RestartThreads == false)
{
int rand = RandomNumber.GetRandomNumber(1000, 2000);
Thread.Sleep(rand);
if (!isPaused)
{
lock (_grid)
{
IActor player = actor.move(this);
if (player != null)
{
Thread t = new Thread(delegate() { move(player); });
t.Start();
}
}
}
}
}
Thanks.
|
|
|
|
|
Professional C# 4.0 and .NET 4
I have written a small program with c#,now want to study more about it.
|
|
|
|
|
And?
Read the book? Do the exercises?
Ask an actual question?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
read book then practice,3q
|
|
|
|
|
You're welcome, but use "proper" English please: "3q" isn't necessarily understood by all ages in all countries, where "Thank you" is.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
sorry,discoveried by you,my English is poor,I need study English more time later.
|
|
|
|
|
Your English is better than my Mandarin!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Hi, Its not about English. its about shortcut 3q. only after reading comment i understood meaning of 3q. by the way your English is better than mine.
|
|
|
|
|
|
|
Pro C# and the .NET framework is a good one, but Wrox and Adison Wesley both publish generally execlent books. Avoid anything with multiple exclamation marks, "in xxx days", or "for dummies" in the title.
If possible, get something in your native language (whatever that is) - it's a lot easier to understand new material when you don't have to try and translate new terms at the same time!
But whatever book you get, start at the beginning, and work through: do the exercises, even if they seem trivial - you learn best by doing, not reading the answers. Books tend to be organised so they build on previous material, and skipping a chapter or two can mean that you miss a lot of useful stuff!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Pick one for yourself: Useful Reference Books[^]
Skipper: We'll fix it.
Alex: Fix it? How you gonna fix this?
Skipper: Grit, spit and a whole lotta duct tape.
|
|
|
|
|
very good,i am reading,3q
|
|
|
|
|
And don't post the same thing twice in 3 minutes (or at all) - it'll just get deleted again...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
|
If you can understand the English, you could read the reviews of that book on Amazon: [^].
That book was published in 2010, and there are a lot of new developments in .NET and C# since then.
If you say more about the level of understanding and coding you are at now, perhaps you can get a recommendation for a more recent book.
cheers, Bill
«What we observe is not nature itself, but nature exposed to our method of questioning» Werner Heisenberg
|
|
|
|
|
oh~I have reply you 2 days ago,but because of net delay not display up,sorry.I read reviews about it on DangDang,very good.bye the way,I just write a small program which would auto check my bbs's register users,not very complex,so it is enough,I think,hehe.Thank you very much,anyway
|
|
|
|
|
when i add shockwave flash object from COM the visual studio was restarting
|
|
|
|
|
And how that related to C#?
Skipper: We'll fix it.
Alex: Fix it? How you gonna fix this?
Skipper: Grit, spit and a whole lotta duct tape.
|
|
|
|
|