ThreadPool don't work
-
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.
-
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
-
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?
Why don't you just call it without threadpool?
-
Why don't you just call it without threadpool?
-
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.
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:
-
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:
-
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
-
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.
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
-
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)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.
-
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.
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
-
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
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.
-
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.
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.
-
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.
-
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