how to stop methods running at the same time in a console app?

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2009
Posts: 12
Reputation: tig2810 is an unknown quantity at this point 
Solved Threads: 0
tig2810 tig2810 is offline Offline
Newbie Poster

how to stop methods running at the same time in a console app?

 
0
  #1
Jul 13th, 2009
Hi

I have a problem with a console app. It will not run methods in sequence but rather runs them all at the same time? I have put some example code below. The problem is it will run the methods below at the same time, without waiting for the frs to finish? If I remove the foreach statement it does not help. I do not have this problem with a windows forms program, only console?

  1. Method("string1");
  2. Method("string2");
  3. Method("string3");
  4. Method("string4");

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. Transfer xfer = new Transfer();
  6. xfer.Run();
  7. }
  8. }
  9.  
  10. public class Transfer
  11. {
  12. public void Run()
  13. {
  14. List<string> myList= new List<string>();
  15. myList.Add("string1");
  16. myList.Add("string2");
  17. myList.Add("string3");
  18. myList.Add("string4");
  19.  
  20. Method(string i)
  21. {
  22. //code to do whatever
  23. }
  24. foreach(string i in myList)
  25. {
  26. Method(i);
  27. }
  28. }
  29. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,285
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 586
Sponsor
sknake's Avatar
sknake sknake is online now Online
.NET Enthusiast

Re: how to stop methods running at the same time in a console app?

 
0
  #2
Jul 13th, 2009
I can assure you they run sequentially and not at the same time. You also posted code that won't compile so I suspect your problem lies elsewhere. Please post your complete code so we can take a look at it.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 10
Reputation: GökhanBarış is an unknown quantity at this point 
Solved Threads: 2
GökhanBarış GökhanBarış is offline Offline
Newbie Poster

Re: how to stop methods running at the same time in a console app?

 
1
  #3
Jul 13th, 2009
i rearrange your code and runned. As far as i know its working sequentially well.

here is the code:

  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. Transfer xfer = new Transfer();
  13. xfer.Run();
  14. }
  15. }
  16.  
  17. public class Transfer
  18. {
  19. public void Run()
  20. {
  21. List<string> myList= new List<string>();
  22. myList.Add("string1");
  23. myList.Add("string2");
  24. myList.Add("string3");
  25. myList.Add("string4");
  26.  
  27. foreach(string i in myList)
  28. {
  29. Method(i);
  30. }
  31. }
  32.  
  33. public void Method(string i)
  34. {
  35. Console.WriteLine(i);
  36. }
  37. }
  38.  
  39. }
Last edited by GökhanBarış; Jul 13th, 2009 at 9:08 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 12
Reputation: tig2810 is an unknown quantity at this point 
Solved Threads: 0
tig2810 tig2810 is offline Offline
Newbie Poster

Re: how to stop methods running at the same time in a console app?

 
0
  #4
Jul 13th, 2009
Sorry, looking deeper into the code I found that an external process (System.Diagnostic.Process) was being called and was the cause of the problem. MY apologies but thanks for all that responded. The clarification that they would run is sequence forced me to look elsewhere so it helped me greatly.
thanks
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,285
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 586
Sponsor
sknake's Avatar
sknake sknake is online now Online
.NET Enthusiast

Re: how to stop methods running at the same time in a console app?

 
0
  #5
Jul 13th, 2009
I'm glad you a solution for your problem.

Please mark this thread as solved and good luck!
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC