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. ThreadPool don't work

ThreadPool don't work

Scheduled Pinned Locked Moved C#
com
20 Posts 8 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.
  • A abbd

    Thank you verry mutch for your answer, the TEST method isn't executed, I do not get the document word. I want to spend a block of code to run heavy, however the code does not return anything, thank you for your feedback on my big problem.

    P Offline
    P Offline
    Pete OHanlon
    wrote on last edited by
    #6

    As I said, when you run something in a background process, you need to wait for it to complete. A simplistic mechanism here would be to use a Monitor to wait for the document method to complete - although in this case, I have to question why it's a background task at all. Architecturally, you'd be better off having the task run on the main thread and then exit.

    Forgive your enemies - it messes with their heads

    My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

    A 1 Reply Last reply
    0
    • S SledgeHammer01

      Its not going to execute and its not expected to. As Pete indicated, your application exits BEFORE the Test method has a chance to run. You need to wait on your main thread for the worker threads to finish.

      A Offline
      A Offline
      abbd
      wrote on last edited by
      #7

      How i can wait the end of execution ?

      1 Reply Last reply
      0
      • P Pete OHanlon

        As I said, when you run something in a background process, you need to wait for it to complete. A simplistic mechanism here would be to use a Monitor to wait for the document method to complete - although in this case, I have to question why it's a background task at all. Architecturally, you'd be better off having the task run on the main thread and then exit.

        Forgive your enemies - it messes with their heads

        My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

        A Offline
        A Offline
        abbd
        wrote on last edited by
        #8

        exactly, whene i put Thread.Sleep(10000); the TEST method was excuted, so i need to wait the time necessary for executin my method, it's possible?

        D 1 Reply Last reply
        0
        • A abbd

          exactly, whene i put Thread.Sleep(10000); the TEST method was excuted, so i need to wait the time necessary for executin my method, it's possible?

          D Offline
          D Offline
          Daniel Scott
          wrote on last edited by
          #9

          Why don't you just call it without threadpool?

          A 1 Reply Last reply
          0
          • D Daniel Scott

            Why don't you just call it without threadpool?

            A Offline
            A Offline
            abbd
            wrote on last edited by
            #10

            i would excute the TEST method 5X in the same time, how in can do this?

            P 1 Reply Last reply
            0
            • A abbd

              Hello, I would use a ThreadPool like this :

              public void TEST()
              {
              Object oMissing = System.Reflection.Missing.Value;
              Object oTrue = true;
              Object oFalse = false;
              Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
              Microsoft.Office.Interop.Word.Document oWordDoc = new Microsoft.Office.Interop.Word.Document();
              oWord.Visible = true;
              oWord.Visible = false;
              Object oTemplatePath = @".\1.doc";
              oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);

                      foreach (Microsoft.Office.Interop.Word.Range range in oWordDoc.Words)
                      {
                          if (range.Text.Trim().Contains("P"))
                          {
                              int pos = range.Text.Trim().IndexOf("P");
                              string str = range.Text.Trim().Replace("P", "");
                              range.Text = str;
                          }
              
                      }
                      Object oSaveAsFile = "./2.doc";
                      oWordDoc.SaveAs(ref oSaveAsFile, ref oMissing, ref oMissing, ref oMissing,
                      ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                      ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                      ref oMissing, ref oMissing);
              
                      oWordDoc.Close(ref oFalse, ref  oMissing, ref oMissing);
                      oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
                  }
              
                  static void Main(string\[\] args)
                  {
                     
              
                          Program test = new Program();
                          ThreadPool.QueueUserWorkItem(o => test.TEST());
                  }
              

              Unfortunatly it don't work. Thank you verry mutch.

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #11

              Looks like you could really use some study on threading since you seem to not understand what the others are telling you. There's lots of books on the subject, but for free there's always the documentation at MSDN: Managed Threading[^]

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              A 1 Reply Last reply
              0
              • M Mark Salsbery

                Looks like you could really use some study on threading since you seem to not understand what the others are telling you. There's lots of books on the subject, but for free there's always the documentation at MSDN: Managed Threading[^]

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                A Offline
                A Offline
                abbd
                wrote on last edited by
                #12

                i would usoont the threading pool because there ara many advantages, like the memory using, i dont have visual studio 2010, i work with 2008, thank you for answhering me.

                P L 2 Replies Last reply
                0
                • A abbd

                  i would excute the TEST method 5X in the same time, how in can do this?

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #13

                  If you're executing the same method 5 times together, you're probably going to get a conflict when you try to save the document.

                  Forgive your enemies - it messes with their heads

                  My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                  1 Reply Last reply
                  0
                  • A abbd

                    i would usoont the threading pool because there ara many advantages, like the memory using, i dont have visual studio 2010, i work with 2008, thank you for answhering me.

                    P Offline
                    P Offline
                    Pete OHanlon
                    wrote on last edited by
                    #14

                    abbd wrote:

                    i would usoont the threading pool because there ara many advantages, like the memory using

                    Eh? What memory advantages?

                    abbd wrote:

                    i dont want using thread

                    Ultimately, if you are using the ThreadPool, you are using threading. It's just that the gory details are hidden from you - as is the fine grained control you get if you manage the threading yourself. Seriously, read up on threading. It's a tricky concept to master, and you are just storing up trouble for yourself if you attempt to use it without a detailed understanding of it.

                    Forgive your enemies - it messes with their heads

                    My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                    1 Reply Last reply
                    0
                    • I Ian Shlasko

                      Teacher: Ok, class... For this quiz, you're going to go through this document and cross out all of the P's... Ready... set... GO! Time's up! Pencils down... Class: But we haven't started yet... Teacher: Pass your tests forward now!

                      Proud to have finally moved to the A-Ark. Which one are you in?
                      Author of the Guardians Saga (Sci-Fi/Fantasy novels)

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #15

                      Ian Shlasko wrote:

                      encils down...

                      FTFY. :)

                      Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                      Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                      1 Reply Last reply
                      0
                      • A abbd

                        i would usoont the threading pool because there ara many advantages, like the memory using, i dont have visual studio 2010, i work with 2008, thank you for answhering me.

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #16

                        you don't need .NET 4.0 to get good multi-threading, you can do it easily since 2.0, provided you understand how it works. Hit the books and study. Now stop asking to be spoon fed. Warning: I'm not sure the Office PIA behaves well in multi-threading of any kind, I'm not saying it won't, I'm saying I have some doubts. :|

                        Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                        Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                        modified on Monday, May 16, 2011 6:25 PM

                        A 1 Reply Last reply
                        0
                        • L Luc Pattyn

                          you don't need .NET 4.0 to get good multi-threading, you can do it easily since 2.0, provided you understand how it works. Hit the books and study. Now stop asking to be spoon fed. Warning: I'm not sure the Office PIA behaves well in multi-threading of any kind, I'm not saying it won't, I'm saying I have some doubts. :|

                          Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                          Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                          modified on Monday, May 16, 2011 6:25 PM

                          A Offline
                          A Offline
                          abbd
                          wrote on last edited by
                          #17

                          Hello, I try for :

                          Object oMissing = System.Reflection.Missing.Value;
                          Object oTrue = true;
                          Object oFalse = false;
                          Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
                          Microsoft.Office.Interop.Word.Document oWordDoc = new Microsoft.Office.Interop.Word.Document();
                          oWord.Visible = true;
                          oWord.Visible = false;
                          Object oTemplatePath = @".\1.doc";
                          oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);

                                  foreach (Microsoft.Office.Interop.Word.Range range in oWordDoc.Words)
                                  {
                                      if (range.Text.Trim().Contains("P"))
                                      {
                                          int pos = range.Text.Trim().IndexOf("P");
                                          string str = range.Text.Trim().Replace("P", "");
                                          range.Text = str;
                                      }
                          
                                  }
                                  Directory.CreateDirectory(@"F:\\PS-IN\\"+threadContext);
                                  Object oSaveAsFile = @"F:\\PS-IN\\" + threadContext+"\\\\" + threadContext + ".doc";
                                  oWordDoc.SaveAs(ref oSaveAsFile, ref oMissing, ref oMissing, ref oMissing,
                                  ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                  ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                  ref oMissing, ref oMissing);
                                  Console.WriteLine(threadContext+" "+DateTime.Now.ToShortTimeString());
                                  oWordDoc.Close(ref oFalse, ref  oMissing, ref oMissing);
                                  oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
                              }
                          
                              static void Main(string\[\] args)
                              {
                          
                                  const int FibonacciCalculations = 50;
                          
                                  // One event is used for each Fibonacci object.
                                  ManualResetEvent\[\] doneEvents = new ManualResetEvent\[FibonacciCalculations\];
                                  Program\[\] fibArray = new Program\[FibonacciCalculations\];
                                  Random r = new Random();
                          
                                  // Configure and start threads using ThreadPool.
                                  Console.WriteLine("launching {0} tasks...", FibonacciCalculations);
                                  for (int i = 0; i < FibonacciCalculations; i++)
                                  {
                                      doneEvents\[i\] = new ManualResetEvent(false);
                                      Program f = new Program();
                                      fibArray\[i\] = f;
                                      ThreadPool.QueueUserWorkItem(f.TEST,i);
                                  }
                          
                                  // Wait for all threads in pool to calculate.
                          
                          B 1 Reply Last reply
                          0
                          • A abbd

                            Hello, I try for :

                            Object oMissing = System.Reflection.Missing.Value;
                            Object oTrue = true;
                            Object oFalse = false;
                            Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
                            Microsoft.Office.Interop.Word.Document oWordDoc = new Microsoft.Office.Interop.Word.Document();
                            oWord.Visible = true;
                            oWord.Visible = false;
                            Object oTemplatePath = @".\1.doc";
                            oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);

                                    foreach (Microsoft.Office.Interop.Word.Range range in oWordDoc.Words)
                                    {
                                        if (range.Text.Trim().Contains("P"))
                                        {
                                            int pos = range.Text.Trim().IndexOf("P");
                                            string str = range.Text.Trim().Replace("P", "");
                                            range.Text = str;
                                        }
                            
                                    }
                                    Directory.CreateDirectory(@"F:\\PS-IN\\"+threadContext);
                                    Object oSaveAsFile = @"F:\\PS-IN\\" + threadContext+"\\\\" + threadContext + ".doc";
                                    oWordDoc.SaveAs(ref oSaveAsFile, ref oMissing, ref oMissing, ref oMissing,
                                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                    ref oMissing, ref oMissing);
                                    Console.WriteLine(threadContext+" "+DateTime.Now.ToShortTimeString());
                                    oWordDoc.Close(ref oFalse, ref  oMissing, ref oMissing);
                                    oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
                                }
                            
                                static void Main(string\[\] args)
                                {
                            
                                    const int FibonacciCalculations = 50;
                            
                                    // One event is used for each Fibonacci object.
                                    ManualResetEvent\[\] doneEvents = new ManualResetEvent\[FibonacciCalculations\];
                                    Program\[\] fibArray = new Program\[FibonacciCalculations\];
                                    Random r = new Random();
                            
                                    // Configure and start threads using ThreadPool.
                                    Console.WriteLine("launching {0} tasks...", FibonacciCalculations);
                                    for (int i = 0; i < FibonacciCalculations; i++)
                                    {
                                        doneEvents\[i\] = new ManualResetEvent(false);
                                        Program f = new Program();
                                        fibArray\[i\] = f;
                                        ThreadPool.QueueUserWorkItem(f.TEST,i);
                                    }
                            
                                    // Wait for all threads in pool to calculate.
                            
                            B Offline
                            B Offline
                            BobJanova
                            wrote on last edited by
                            #18

                            Do you ever set doneEvents members to true? If not, how can you expect the wait to ever succeed? Please think about what you're doing before posting a code dump here. If you don't understand how threading and wait handles work, please read up on them before trying to use them.

                            A 1 Reply Last reply
                            0
                            • B BobJanova

                              Do you ever set doneEvents members to true? If not, how can you expect the wait to ever succeed? Please think about what you're doing before posting a code dump here. If you don't understand how threading and wait handles work, please read up on them before trying to use them.

                              A Offline
                              A Offline
                              abbd
                              wrote on last edited by
                              #19

                              i find the solution, we must add doneEvents[int.Parse(threadContext.ToString())].Set(); after the and of generation.

                              modified on Tuesday, May 17, 2011 5:05 AM

                              A 1 Reply Last reply
                              0
                              • A abbd

                                i find the solution, we must add doneEvents[int.Parse(threadContext.ToString())].Set(); after the and of generation.

                                modified on Tuesday, May 17, 2011 5:05 AM

                                A Offline
                                A Offline
                                abbd
                                wrote on last edited by
                                #20

                                I need to pass parameters (like arraylist,...) to the TEST method, how i can do this ? I don't understand whay the test methode must have juste the object parameters.

                                modified on Tuesday, May 17, 2011 5:45 AM

                                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