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. Multi-threaded Task stops after 150 tasks

Multi-threaded Task stops after 150 tasks

Scheduled Pinned Locked Moved C#
help
3 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.
  • M Offline
    M Offline
    MAW30
    wrote on last edited by
    #1

    I used the following code for multi-threading, The code works fine but much slower if I add the tasks[nData].wait() command see below "*****" but if I remove the tasks[nData].wait() command then the code generates all the task but only gathers information for approximately 150 task then stops. Can anyone tell me why this is happening and what I can do to correct this problem. Any help will be greatly appreciated. Michael

    public Boolean LoadData()
    {
    Boolean bComplete = false;
    Int32 nTotalData = 10000;
    this.tasks = new Task[nTotalData];
    DateTime dtStartTime = DateTime.Now;

            ProcessingBarsCS.StartTime();
            var parent = Task.Factory.StartNew(() =>
            {
                dtStartTime = DateTime.Now;
    
                for (Int32 nData = 0; nData < nTotalSymbols; nData++)
                {
                    bComplete = GetData\_Task(nData, dtStartTime, nTotalData);
                }
            });
            parent.Wait();
            ProcessingBarsCS.StopTime();
    
            return bComplete;
        }
    
        Boolean GetData\_Task(Int32 nData, DateTime  dtStartTime, Int32 nTotalData)
        {
            DataCS dataCS = new DataCS();
            tasks\[nData\] = Task.Factory.StartNew(() =>
            {
                nData = dataCS.Data();
            })
                .ContinueWith(antecendent => ProcessingData(nData));
    
            // \*\*\*\*\*
            tasks\[nData\].Wait();
    
            // Wait for all task to finish calculations
            try
            {
                Task.WaitAll(tasks);
                SetData\_ElapsedTime(dtStartTime, nTotalData);
            }
            catch { }
    
            return true;
        }
    
    W Richard DeemingR 2 Replies Last reply
    0
    • M MAW30

      I used the following code for multi-threading, The code works fine but much slower if I add the tasks[nData].wait() command see below "*****" but if I remove the tasks[nData].wait() command then the code generates all the task but only gathers information for approximately 150 task then stops. Can anyone tell me why this is happening and what I can do to correct this problem. Any help will be greatly appreciated. Michael

      public Boolean LoadData()
      {
      Boolean bComplete = false;
      Int32 nTotalData = 10000;
      this.tasks = new Task[nTotalData];
      DateTime dtStartTime = DateTime.Now;

              ProcessingBarsCS.StartTime();
              var parent = Task.Factory.StartNew(() =>
              {
                  dtStartTime = DateTime.Now;
      
                  for (Int32 nData = 0; nData < nTotalSymbols; nData++)
                  {
                      bComplete = GetData\_Task(nData, dtStartTime, nTotalData);
                  }
              });
              parent.Wait();
              ProcessingBarsCS.StopTime();
      
              return bComplete;
          }
      
          Boolean GetData\_Task(Int32 nData, DateTime  dtStartTime, Int32 nTotalData)
          {
              DataCS dataCS = new DataCS();
              tasks\[nData\] = Task.Factory.StartNew(() =>
              {
                  nData = dataCS.Data();
              })
                  .ContinueWith(antecendent => ProcessingData(nData));
      
              // \*\*\*\*\*
              tasks\[nData\].Wait();
      
              // Wait for all task to finish calculations
              try
              {
                  Task.WaitAll(tasks);
                  SetData\_ElapsedTime(dtStartTime, nTotalData);
              }
              catch { }
      
              return true;
          }
      
      W Offline
      W Offline
      walterhevedeich
      wrote on last edited by
      #2

      My guess is that you had a SystemOutOfMemoryException. Try to check the Event Viewer for possible error messages that the application has thrown. Also it would be wise to set a limit for the number of tasks that will be created so as not to overload your machine.

      Signature construction in progress. Sorry for the inconvenience.

      1 Reply Last reply
      0
      • M MAW30

        I used the following code for multi-threading, The code works fine but much slower if I add the tasks[nData].wait() command see below "*****" but if I remove the tasks[nData].wait() command then the code generates all the task but only gathers information for approximately 150 task then stops. Can anyone tell me why this is happening and what I can do to correct this problem. Any help will be greatly appreciated. Michael

        public Boolean LoadData()
        {
        Boolean bComplete = false;
        Int32 nTotalData = 10000;
        this.tasks = new Task[nTotalData];
        DateTime dtStartTime = DateTime.Now;

                ProcessingBarsCS.StartTime();
                var parent = Task.Factory.StartNew(() =>
                {
                    dtStartTime = DateTime.Now;
        
                    for (Int32 nData = 0; nData < nTotalSymbols; nData++)
                    {
                        bComplete = GetData\_Task(nData, dtStartTime, nTotalData);
                    }
                });
                parent.Wait();
                ProcessingBarsCS.StopTime();
        
                return bComplete;
            }
        
            Boolean GetData\_Task(Int32 nData, DateTime  dtStartTime, Int32 nTotalData)
            {
                DataCS dataCS = new DataCS();
                tasks\[nData\] = Task.Factory.StartNew(() =>
                {
                    nData = dataCS.Data();
                })
                    .ContinueWith(antecendent => ProcessingData(nData));
        
                // \*\*\*\*\*
                tasks\[nData\].Wait();
        
                // Wait for all task to finish calculations
                try
                {
                    Task.WaitAll(tasks);
                    SetData\_ElapsedTime(dtStartTime, nTotalData);
                }
                catch { }
        
                return true;
            }
        
        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        Walking this code through:

        • You start a new Task;
        • You call GetData_Task 10000 times in a loop;
        • GetData_Task creates a new Task and immediately waits for it to complete;
        • GetData_Task then waits for all 10000 tasks - most of which haven't been created yet - to complete, catching and ignoring the ArgumentException thrown when the WaitAll method encounters the first null element in the array;
        • The LoadData method then waits for the parent Task to complete;

        No wonder your code is slow! This code looks like a suitable candidate for the Parallel class[^]:

        public bool LoadData()
        {
        const int nTotalData = 10000;

        DateTime dtStartTime = DateTime.Now;
        ProcessingBarsCS.StartTime();
        
        Parallel.For(0, nTotalData, nData =>
        {
            var dataCS = new DataCS();
            var result = dataCS.Data();
            ProcessingData(result);
        });
        
        SetData\_ElapsedTime(dtStartTime, nTotalData);
        ProcessingBarsCS.StopTime();
        
        return true;
        

        }


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        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