0

I have a class in a windows form app, that takes a file name that is used to call a web api returns some data and inserts it into a database. I call this in my main class after geting an array of file names. When I run the program like:

foreach(String M_file in files)
{
   FileTODataBase ftb = new FileToDataBase(M_file);
}

It works fine. If I move the FileToDataBase to method(object a) and call it in the loop like ThreadPool.QueueUserWorkItem(method, M_File);

foreach(String M_File in files){
    ThreadPool.QueueUserWorkItem(method, M_File);
}
private void method(object a){
   String M_Files = a as String;
   FileToDataBase ftb = new FileToDataBase(M_Files);
}

It only works for my test folder of 6 files. When running it on the full folder of 800+ items it does not appear to do anything. It does not freeze just does not appear to do any of the actual work of inserting or calling the api. I have read some other post about using thread pools and Task but have been unable to get any of the implementations to work.

kwongjose
  • 1
  • 2
  • 1
    Please show the full code so that we can test it. We need a [mcve]. – Enigmativity Sep 22 '16 at 03:13
  • What should I add? I can link the github but FileToDataBase is quite large to copy and the only thing happening before what I have to picking the folder and getting all the files. – kwongjose Sep 22 '16 at 03:17
  • Maybe a snippet of your code using the ThreadPool would be helpful. Are you catching exceptions in the method that is being invoked asynchronously? Do you have any sort of logging to verify whether or not the code is being executed? Have you tried placing a breakpoint and watching the control flow? – Jacob Sep 22 '16 at 03:19
  • I will add the full block. Bear with me I am getting used to the formatting. I have some debug statements in FileToDataBase. But honestly I am pretty new to threading and most of my coding has been in Java and eclipse rather than VS. – kwongjose Sep 22 '16 at 03:29
  • I would add a try-catch around the code in "method" and check for exceptions. Also, maybe print some output to the console before and after. Also, instead of using the ThreadPool you could use the task parallel library: public static void Main() { List files = new List(); Parallel.ForEach(files, M_File => { method(M_File); }); } private static void method(string M_Files) { FileToDataBase ftb = new FileToDataBase(M_Files); } – Jacob Sep 22 '16 at 03:41
  • Use Async -Await for IO bound councurrency – Mrinal Kamboj Sep 22 '16 at 03:49
  • 1
    @kwongjose - When you come here to ask a question you need to be prepared to provide code that demonstrates your problem where possible. You should do as much work as possible to make our job as easy as possible. Two bad things are not giving enough code and giving too much code. You need to give just enough so that we can copy, paste and run. Then we can help quickly and easily. – Enigmativity Sep 22 '16 at 03:51
  • Try consider using Parallel.For from .Net Task Parallel Library (TPL). It's much easier to implement and will address your problem. – Souvik Ghosh Sep 22 '16 at 03:59
  • @Jacob - Thank you! That seems to be working. I found how to limit the number of threads using ParalleOptions and everything seems to be running. Is there a place you can point to to to read about the syntax used with "=>"? I have not seen that before I started working with C#. – kwongjose Sep 22 '16 at 03:59
  • 1
    `Parallel.For`for IO bound operations, very bad idea – Mrinal Kamboj Sep 22 '16 at 04:25
  • => is a lamda expression: https://msdn.microsoft.com/en-us/library/bb397687.aspx – Jacob Sep 22 '16 at 07:34
  • I'm assuming Mrinal is hinting at wasting threadpool threads blocking for IO. If you're curious about improving your solution, here are a few articles to read: http://stackoverflow.com/questions/8505815/how-to-properly-parallelise-job-heavily-relying-on-i-o http://stackoverflow.com/questions/21270523/parallelizing-io-bound-network-foreach-loop http://stackoverflow.com/questions/35686341/parallel-execution-for-io-bound-operations – Jacob Sep 22 '16 at 15:29
  • @Jacob Thanks. I have only used lamda in course work previously but it looks like they can be quite useful . Thanks for the links. – kwongjose Sep 22 '16 at 18:33

0 Answers0