0

I want to implement multithreading in my wpf application.I have a list of tables and I need generate data files for each table concurrently.

Suppose I have a function called GenerateFiles,

 public void GenerateFiles() { //creating scripts } 

and I have

 foreach(var table in tables) { GenerateFiles(); } 

How can genrate the files using GenerateFiles() concurrently using threads? Is it correct ?

while(tables.count) { Thread th = new Thread(); oThread.Start(new ThreadStart(GenerateFiles)); } 

How can I implement this using Multithreading ?

1
  • You may wan to rethink this. Parallelizing I/O is not always a good idea. On a SSD it might be a little faster, on a HDD it will be (a lot) slower. Commented Nov 21, 2014 at 12:13

2 Answers 2

2

With your code you move the generation of the Files to a background thread. If you want to create them parallel you can use:

Parallel.ForEach(tables, table => { GenerateFiles(); } 
Sign up to request clarification or add additional context in comments.

Comments

1

If you want the files to be generated in the background and not affect the UI, you may want to wrap this inside a Task.

 Task.Run(()=> { Parallel.ForEach(tables, table => { GenerateFiles(); } }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.