MultiThreading
-
hi all, i made this code to make multithreads for one method and every thread take one packet to move but it didnot work, Is there any error in this code
private void btn_simulate_Click(object sender, EventArgs e) { for (int i = 0; i < lineArray.LineList.Count; i++) { /// Assign a method to thread to execute packetsMovingThreadArray.Add(new Thread(threadProcedure)); dataPacketsArray.Add(new Packet()); } this.btn_simulate.Enabled = false; this.btn_stop.Enabled = true; movingPacketThreadFlag = true; for (int i = 0; i < lineArray.LineList.Count; i++) { ((Thread)packetsMovingThreadArray[i]).Start(); } } int simulationIndex = -1; private void threadProcedure() { simulationIndex++; Packet pack = ((Packet)dataPacketsArray[simulationIndex]); AdHocMobility.simulate(ref pack,((Line)lineArray.LineList[simulationIndex]), this); } /// /// Abort the Thread of painting /// private void btn_stop_Click(object sender, EventArgs e) { try { this.btn_stop.Enabled = false; this.btn_simulate.Enabled = true; packetsMovingThreadArray.Clear(); foreach (Thread Th in packetsMovingThreadArray ) { Th.Abort(); } simulationIndex = -1; movingPacketThreadFlag = false; Invalidate(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
thanks Generator -
hi all, i made this code to make multithreads for one method and every thread take one packet to move but it didnot work, Is there any error in this code
private void btn_simulate_Click(object sender, EventArgs e) { for (int i = 0; i < lineArray.LineList.Count; i++) { /// Assign a method to thread to execute packetsMovingThreadArray.Add(new Thread(threadProcedure)); dataPacketsArray.Add(new Packet()); } this.btn_simulate.Enabled = false; this.btn_stop.Enabled = true; movingPacketThreadFlag = true; for (int i = 0; i < lineArray.LineList.Count; i++) { ((Thread)packetsMovingThreadArray[i]).Start(); } } int simulationIndex = -1; private void threadProcedure() { simulationIndex++; Packet pack = ((Packet)dataPacketsArray[simulationIndex]); AdHocMobility.simulate(ref pack,((Line)lineArray.LineList[simulationIndex]), this); } /// /// Abort the Thread of painting /// private void btn_stop_Click(object sender, EventArgs e) { try { this.btn_stop.Enabled = false; this.btn_simulate.Enabled = true; packetsMovingThreadArray.Clear(); foreach (Thread Th in packetsMovingThreadArray ) { Th.Abort(); } simulationIndex = -1; movingPacketThreadFlag = false; Invalidate(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
thanks GeneratorHexaDeveloper wrote:
Is there any error in this code
If "simulate()" is painting you can't do that from a worker thread and the way you are using
simulationIndex
does not look safe in short you need to do some more reading on multi-thread concepts like synchronization. I recommend books by Jeffery Richter on that subject. -
HexaDeveloper wrote:
Is there any error in this code
If "simulate()" is painting you can't do that from a worker thread and the way you are using
simulationIndex
does not look safe in short you need to do some more reading on multi-thread concepts like synchronization. I recommend books by Jeffery Richter on that subject.led mike wrote:
If "simulate()" is painting you can't do that from a worker thread
hi can u explain this statement , in simulate i called movePacket() and this method returns new points for the packet and i draw it in onPaint() and actually i want to ask what is the meaning of not safe to use simulationIndex thanx Generator
-
led mike wrote:
If "simulate()" is painting you can't do that from a worker thread
hi can u explain this statement , in simulate i called movePacket() and this method returns new points for the packet and i draw it in onPaint() and actually i want to ask what is the meaning of not safe to use simulationIndex thanx Generator
-
hi all, i made this code to make multithreads for one method and every thread take one packet to move but it didnot work, Is there any error in this code
private void btn_simulate_Click(object sender, EventArgs e) { for (int i = 0; i < lineArray.LineList.Count; i++) { /// Assign a method to thread to execute packetsMovingThreadArray.Add(new Thread(threadProcedure)); dataPacketsArray.Add(new Packet()); } this.btn_simulate.Enabled = false; this.btn_stop.Enabled = true; movingPacketThreadFlag = true; for (int i = 0; i < lineArray.LineList.Count; i++) { ((Thread)packetsMovingThreadArray[i]).Start(); } } int simulationIndex = -1; private void threadProcedure() { simulationIndex++; Packet pack = ((Packet)dataPacketsArray[simulationIndex]); AdHocMobility.simulate(ref pack,((Line)lineArray.LineList[simulationIndex]), this); } /// /// Abort the Thread of painting /// private void btn_stop_Click(object sender, EventArgs e) { try { this.btn_stop.Enabled = false; this.btn_simulate.Enabled = true; packetsMovingThreadArray.Clear(); foreach (Thread Th in packetsMovingThreadArray ) { Th.Abort(); } simulationIndex = -1; movingPacketThreadFlag = false; Invalidate(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
thanks Generatoruse System.Threading.ThreadStart delegate instead of Thread