1

I have a list of thread I want to ensure the execution order between them this is the code

for (int k = 0; k < RadioList.Count; k++) { for (int i = 0; i < filePaths.Count(); i++) { Thread t = new Thread(delegate() { Thread_Encde_function(TempRadio.PublishPoint, filePaths[i], encodingtype); }); t.Start(); Thread.Sleep(1000); } } 

I want to know if thread.join() can do the job.

3
  • 1
    What do you mean by "ensure the execution order"? Can you explain more? Commented May 4, 2013 at 11:25
  • 1
    If you need to guarantee execution order, why not just execute the function synchronously? If you're worried about blocking the UI thread, you could execute them all on the same background thread. Commented May 4, 2013 at 11:25
  • 2
    No, Thread.Join() won't do the job. Take a look at MSDN: Thread Synchronization. Commented May 4, 2013 at 11:28

1 Answer 1

8

If your tasks can execute asynchronously (out of order), then threads are appropriate. However, if you have a number of tasks you want to execute in order (strictly one after the other), then why use threads? You should just execute them in your for loop, as they can't be parallelized. Using Thread.Join to wait for the thread right after you start it will do the trick, but that way you will have to wait until a task is finished before starting the next one, effectively executing them in order.

However, if you have some parts of the tasks that can be executed simultaneously, and other parts that have to be sequential, you can take a look at the C# Task Parallel Library, it makes doing things like that easy.

Sign up to request clarification or add additional context in comments.

2 Comments

ok as referred can I loop them in one thread then put a join to wait until it finish, because i will repeate this function many times as like as i put new values in the attribute filepaths , and have to wait until the first group of files to be encoded
@7addan Please read about the basic concept of asynchronous execution, threads, tasks, etc. There are plenty of resources online. If you have tasks that come from somewhere and you need something to process them as they arrive, please look up the producer-consumer pattern. In C#, it cean easily be implemented by using BlockingCollection and one separate thread.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.