asp.netoopprocesslocking

Process Lock Code Illustration Needed


I recently started this question in another thread (to which Reed Copsey graciously responded) but I don't feel I framed the question well.

At the core of my question, I would like an illustration of how to gain access to data AS it is being get/set.

I have Page.aspx.cs and, in the codebehind, I have a loop:

           List<ServerVariable> files = new List<ServerVariable>();

            for (i = 0; i <= Request.Files.Count - 1; i++)
            {

                    m_objFile = Request.Files[i];
                    m_strFileName = m_objFile.FileName;
                    m_strFileName = Path.GetFileName(m_strFileName);

                files.Add(new ServerVariable(i.ToString(),
this.m_strFileName, "0"));

           }

           //CODE TO COPY A FILE FOR UPLOAD TO THE
           //WEB SERVER

            //WHEN THE UPLOAD IS DONE, SET THE ITEM TO
            //COMPLETED

            int index = files.FindIndex(p => p.Completed == "0");
            files[index] = new ServerVariable(i.ToString(),
this.m_strFileName, "1");

The "ServerVariable" type gets and sets ID, File, and Completed.

Now, I need to show the user the file upload "progress" (in effect, the time between when the loop adds the ServerVariable item to the list to when the Completed status changes from 0 to 1.

Now, I have a web service method "GetStatus()" that I would like to use to return the files list (created above) as a JSON string (via JQuery). Files with a completed status of 0 are still in progress, files with a 1 are done.

MY QUESTION IS - what does the code inside GetStatus() look like? How do I query List **as* it is being populated and return the results real-time? I have been advised that I need to lock the working process (setting the ServerVariable data) while I query the values returned in GetStatus() and then unlock that same process?

If I have explained myself well, I'd appreciate a code illustration of the logic in GetStatus().

Thanks for reading.


Solution

  • Have a look at this link about multi threading locks.

    You need to lock the object in both read and write.