Note that there are some explanatory texts on larger screens.

plurals
  1. PORun a task in a different threads in a console application
    primarykey
    data
    text
    <p>I have done quite a bit of research on this but I still can't seem to get it right. I have to generate a pdf file with 1000 pages (I use a library) and need to do this N times for different data. The data is independent of each other and I can totally generate this in parallel, which is what i'm trying to do. Ideally I would like to have this done in say 10 threads , each thread generating the pdf in memory and saving it at the end. Say it takes 15 minutes per pdf (with 1000 pages), if I do this sequentially, that would be 150 minutes for 10 pdf files vs say 30 minutes if I use 10 threads. I know people are not very fond of threading, but how can I speed it up otherwise?</p> <p>I was looking at <code>ThreadPool</code> but then I see this new <code>Task</code> in 4.0. I read that I can force each task to run in a separate thread if I use <code>TaskCreationOptions.LongRunning</code> but that does not seem to work for me. I tried to use <code>ThreadPool</code> too but since each PDF is generated from a url and for some reason, the <code>WebRequest.Create(url)</code> method seems to not execute when called from a threadpool? But I guess I would rather make the new Task library work.</p> <p>This is what I have now but it still seems to execute sequentially.</p> <pre><code>Task myTask= Task.Factory.StartNew(() =&gt; { //code for the task. //get html content //generate pdf file. } }, new CancellationToken(false), TaskCreationOptions.LongRunning, TaskScheduler.Default); myTask.Wait(); </code></pre> <p>What am I doing wrong here? if you have any suggestions, please let me know. I can't go above .net 4.0 at the moment.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload