|
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.
|
|
|
|
|
how to connected database using c# language with web application asp.net
|
|
|
|
|
|
My purpose in below program is getting 16 bytes of data from microcontroller and processing data for appropriate instructions. There are a lot of related questions and answers here but I couldnt find anything about in below issue. I can get 16 bytes from MCU. Values of bytes are correct and I can see them in dataGridView but the sequence of bytes is changing . For example at first MCUData[0] = 0x01 , MCUData[1] = 0xFE , MCUData[2] = 0xCC then it changes to MCUData[0] = 0xFE , MCUData[1] = 0xCC , MCUData[2] = 0x01. İt is like some problem shifting my datas in byte array. I am sure my MCU is sending data correctly because I checked in one of serial terminal program. My code is in below
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
namespace SerialCommunicationMCU
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
dataGridView1.Columns.Add("MCUData", "Byte Name");
dataGridView1.Columns.Add("MCUData", "Byte Value");
}
public System.IO.Ports.SerialPort SerialPc;
#region Variables
public string AvailablePort;
public string[] Ports = SerialPort.GetPortNames();
byte[] MCUData = new byte[16];
#endregion
private void Connect_Click(object sender, EventArgs e)
{
DataGreedByteNameShow();
SerialConnectandRead();
ConnectButton.Enabled = false;
DisconnectButton.Enabled = true;
}
private void Disconnect_Click(object sender, EventArgs e)
{
SerialPc.Close();
ConnectButton.Enabled = true;
DisconnectButton.Enabled = false;
}
public void SerialConnectandRead()
{
SerialPc = new SerialPort(AvailablePort, 115200, Parity.None, 8, StopBits.One);
try
{
SerialPc.Open();
SerialPc.DataReceived += new SerialDataReceivedEventHandler(SerialPc_DataReceived);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Serial Port Error");
}
}
private void SerialPc_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
SerialPc.DiscardNull = false;
SerialPc.Read(MCUData, 0, 16);
SerialPc.ReceivedBytesThreshold = 16;
DataGreedByteValueShow();
}
private void Form1_Load(object sender, EventArgs e)
{
foreach (string port in Ports)
{
comboBox1.Items.Add(port);
}
DisconnectButton.Enabled = false;
}
public void DataGreedByteNameShow()
{
dataGridView1.Rows.Add("MCUData[0]");
dataGridView1.Rows.Add("MCUData[1]");
dataGridView1.Rows.Add("MCUData[2]");
dataGridView1.Rows.Add("MCUData[3]");
dataGridView1.Rows.Add("MCUData[4]");
dataGridView1.Rows.Add("MCUData[5]");
dataGridView1.Rows.Add("MCUData[6]");
dataGridView1.Rows.Add("MCUData[7]");
dataGridView1.Rows.Add("MCUData[8]");
dataGridView1.Rows.Add("MCUData[9]");
dataGridView1.Rows.Add("MCUData[10]");
dataGridView1.Rows.Add("MCUData[11]");
dataGridView1.Rows.Add("MCUData[12]");
dataGridView1.Rows.Add("MCUData[13]");
dataGridView1.Rows.Add("MCUData[14]");
dataGridView1.Rows.Add("MCUData[15]");
}
private void DataGreedByteValueShow()
{
dataGridView1.Rows[0].Cells[1].Value = MCUData[0];
dataGridView1.Rows[1].Cells[1].Value = MCUData[1];
dataGridView1.Rows[2].Cells[1].Value = MCUData[2];
dataGridView1.Rows[3].Cells[1].Value = MCUData[3];
dataGridView1.Rows[4].Cells[1].Value = MCUData[4];
dataGridView1.Rows[5].Cells[1].Value = MCUData[5];
dataGridView1.Rows[6].Cells[1].Value = MCUData[6];
dataGridView1.Rows[7].Cells[1].Value = MCUData[7];
dataGridView1.Rows[8].Cells[1].Value = MCUData[8];
dataGridView1.Rows[9].Cells[1].Value = MCUData[9];
dataGridView1.Rows[10].Cells[1].Value = MCUData[10];
dataGridView1.Rows[11].Cells[1].Value = MCUData[11];
dataGridView1.Rows[12].Cells[1].Value = MCUData[12];
dataGridView1.Rows[13].Cells[1].Value = MCUData[13];
dataGridView1.Rows[14].Cells[1].Value = MCUData[14];
dataGridView1.Rows[15].Cells[1].Value = MCUData[15];
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
AvailablePort = comboBox1.SelectedItem.ToString();
}
}
}
modified 10-Jan-15 18:35pm.
|
|
|
|
|