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. how to use thread in for loop in c#

how to use thread in for loop in c#

Scheduled Pinned Locked Moved C#
csharpperformancetutorialquestion
26 Posts 7 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.
  • S superselector

    Hi i did not understand do you want the code to be changed to for (int index = startIP; index <= EndIP; index++) { ipVal = startSubnet + "." + index.ToString(); Thread wmithread = new Thread(() => PerformWMIOperation(ipVal.ToString().Trim(), txtUserName.Text.ToString().Trim(), txtPassword.Text.ToString(), txtDomain.Text.ToString(), dtAssetValues, chkImpersonate.Checked, recordnumber)); wmithread.Start(); wmithread.Join(); }

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #21

    That wouldn't help. The problem is this:

    superselector wrote:

    wmithread.Start(); wmithread.Join();

    That's like calling that ping operation without a thread, except this way resources are wasted as well. Or to put it differently, it's like hiring a team to play a game of monopoly, in order to do it faster. That doesn't work. If everyone takes turns anyway, you might as well do it by yourself. You need an array of threads. Fill the array, start them all, then, and only then, join them all. Besides, what's with the WMI stuff? Do you need something that the Ping class[^] can't do?

    S 1 Reply Last reply
    0
    • L Lost User

      That wouldn't help. The problem is this:

      superselector wrote:

      wmithread.Start(); wmithread.Join();

      That's like calling that ping operation without a thread, except this way resources are wasted as well. Or to put it differently, it's like hiring a team to play a game of monopoly, in order to do it faster. That doesn't work. If everyone takes turns anyway, you might as well do it by yourself. You need an array of threads. Fill the array, start them all, then, and only then, join them all. Besides, what's with the WMI stuff? Do you need something that the Ping class[^] can't do?

      S Offline
      S Offline
      superselector
      wrote on last edited by
      #22

      can you please give a sample code to implement this

      L 1 Reply Last reply
      0
      • S superselector

        can you please give a sample code to implement this

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #23

        Ok.. it's not that hard though.

        Thread[] threads = new Thread[something];
        for (int i = 0; i < threads.Length; i++)
        {
        threads[i] = new Thread(something);
        threads[i].Start();
        }
        for (int i = 0; i < threads.Length; i++)
        threads[i].Join();

        S 2 Replies Last reply
        0
        • L Lost User

          Ok.. it's not that hard though.

          Thread[] threads = new Thread[something];
          for (int i = 0; i < threads.Length; i++)
          {
          threads[i] = new Thread(something);
          threads[i].Start();
          }
          for (int i = 0; i < threads.Length; i++)
          threads[i].Join();

          S Offline
          S Offline
          superselector
          wrote on last edited by
          #24

          hi with this approach , I am missing data .. means i am not getting output for all the IPs

          1 Reply Last reply
          0
          • L Lost User

            Ok.. it's not that hard though.

            Thread[] threads = new Thread[something];
            for (int i = 0; i < threads.Length; i++)
            {
            threads[i] = new Thread(something);
            threads[i].Start();
            }
            for (int i = 0; i < threads.Length; i++)
            threads[i].Join();

            S Offline
            S Offline
            superselector
            wrote on last edited by
            #25

            The ip address is getting overlapped. Means two rows are having same ip address.

            L 1 Reply Last reply
            0
            • S superselector

              The ip address is getting overlapped. Means two rows are having same ip address.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #26

              Did you forget to use synchronization when saving the result?

              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