Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. MultiThreading

MultiThreading

Scheduled Pinned Locked Moved C#
help
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    HexaDeveloper
    wrote on last edited by
    #1

    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

    L H 2 Replies Last reply
    0
    • H HexaDeveloper

      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

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      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.

      H 1 Reply Last reply
      0
      • L led mike

        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.

        H Offline
        H Offline
        HexaDeveloper
        wrote on last edited by
        #3

        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

        L 1 Reply Last reply
        0
        • H HexaDeveloper

          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

          L Offline
          L Offline
          led mike
          wrote on last edited by
          #4

          Multi-Threaded application almost always need one or more forms of "Syncrhonization[^]" It's not something that can be explained in a few sentences in a forum message. Look at the link and the author [^]I suggested.

          1 Reply Last reply
          0
          • H HexaDeveloper

            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

            H Offline
            H Offline
            Hesham Yassin
            wrote on last edited by
            #5

            use System.Threading.ThreadStart delegate instead of Thread

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups