需要进程锁代码说明

发布于 2024-08-06 23:10:14 字数 1356 浏览 1 评论 0原文

我最近在另一个线程中开始了这个问题(Reed Copsey 亲切地回答)但我觉得我没有很好地提出这个问题。

在我的问题的核心,我想举例说明如何获得 访问正在获取/设置的数据。

我有 Page.aspx.cs,并且在代码隐藏中,有一个循环:

           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");

“ServerVariable”类型获取并设置 ID、File 和 Completed。

现在,我需要向用户显示文件上传“进度”(实际上, 循环将 ServerVariable 项添加到 列出完成状态从 0 更改为 1 的时间。

现在,我有一个 Web 服务方法“GetStatus()”,我想要 用于将文件列表(上面创建的)作为 JSON 字符串返回(通过 jQuery)。完成状态为 0 的文件仍在进行中, 带有 1 的文件已完成。

我的问题是 - GetStatus() 内的代码是什么样的?如何 我是否在填充列表时查询列表 实时返回结果?我被告知我需要锁定 我查询时的工作过程(设置 ServerVariable 数据) GetStatus() 返回的值然后解锁同一进程?

如果我能很好地解释自己,我会很感激的代码说明 GetStatus() 中的逻辑。

感谢您的阅读。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灯角 2024-08-13 23:10:14

查看有关多线程锁的链接。

您需要在读取和写入时锁定该对象。

Have a look at this link about multi threading locks.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文