I have a control that uploads 1..n files in chunks to a WCF service of mine. The chunking and reassembling is done via multiple calls to the WCF service which all works well for 1 file.
However when I have more than 1 file I am running into some issues. The outer loop which contains the data for each file continues on prior to the 1st file finishing uploading. In fact if it takes 10 calls to chunck a file to the server via WCF calls, processing returns after the 1st call to WCF because it is async with no blocking. This entire process is initiated by an Upload button click, so I assume this is being done on the UI thread. I have tried using an ‘AutoResetEvent’ but to no avail; it always freezes.
What I need is the outer loop to halt processing until the 1..n async calls to WCF within (via ProcessFile method) are completed and an entire single file has been uploaded. Code below:
'FileUploadData is an Observable collection of IO.FileInfo objects
For Each FileItem In FileUploadData
'Method internally calls WCF asynchronously and does so continually until file is completely uploaded.
ProcessFile(FileItem)
'NEED TO STOP HERE until file passed in above is complete.
'Currently processing continues on because 1st call to WCF is async and processing is returned to here.
Next
I am not necessarily looking for a syncronous solution here, but I need some method to allow these files to either be uploaded on their own thread or upload completely in the ‘ProcessFile’ method 1st (still calling WCF async) before continuing the outer loop to the next file displayed above.
Any ideas on how to solve this? Thanks!
Ok got this figured out almost as quick as posting this question (great when that happens). This article (http://www.informit.com/articles/article.aspx?p=1759240) helped me think of a slightly different way to appraoch this with a basic method I was already using.
I now use a LINQ query to only process 1 file at a time from the collection instead of a loop to continually process all of them. Once the file has completed loading I remove it from the collection, and I raise an Event I declared at the end of the ProcessFile method. The Event handler method is raised and selects the next file to process in the collection and repeats this until complete.
Works like a charm!