更新任务数据

时间:2017-10-16 21:56:02

标签: c# asp.net

我正在尝试更新任务的状态。我已经创建了一个以GUID为键的任务字典,并将值设置为double。然后我计算了我要做多少动作并将其存储为“总操作”。完成工作的百分比将是finishedOperations / totalOperations。但是,当我记录我的数据时,我总是得到“0,直到作业完成时结束(因此任务确实等于”1)。我做错了什么。我是否需要将值存储为浮点数而不是一双?

    public ActionResult BuildPackets()
    {

        var taskId = Guid.NewGuid();
        tasks.Add(taskId, 0);

        Task.Factory.StartNew(() =>
        {
            baseDir = _env.WebRootPath + "\\iManageStorage\\";
            FolderHelper.clearFolder(baseDir);
            string WorksiteSetting = "!nrtdms:0:!session:NYCDMS:!database:NewYork:!";
            iManage worksite = new iManage(iManage.GetServerFromDocumentID(WorksiteSetting), iManage.GetDatabaseFromDocumentID(WorksiteSetting));

            IEnumerable<Packet> packetsToBuildTmp = _Repository.GetAllPackets();
            List<Packet> packetsToBuild = packetsToBuildTmp.ToList();
            var finishedOperations = 0;
            var totalOperations = packetsToBuild.Count + 1;
            foreach(Packet p in packetsToBuild)
            {
                totalOperations = totalOperations + p.Documents.Count;
            }

            foreach (Packet packet in packetsToBuild)
            {

                tasks[taskId] = finishedOperations / totalOperations;
                //tasks[taskId] = (i / packetsToBuild.Count); // update task progress
                PBPacket pbpacket = new PBPacket(packet, baseDir, worksite);

                foreach (Document document in packet.Documents)
                {
                    pbpacket.addID(report: document.Name, id: document.ImanageId, worksheet: document.Worksheet, printArea: document.Printarea, whiteout: document.Whiteout);
                    finishedOperations++;
                    tasks[taskId] = finishedOperations / totalOperations;
                }

                Console.WriteLine("BUILDING " + packet.Name + " ************");
                pbpacket.printFilesInPacket();
                pbpacket.convertToPDF();
                pbpacket.mergePacketFiles();

                finishedOperations++;
                tasks[taskId] = finishedOperations / totalOperations;
            }

            string filename = baseDir + "reports.zip";
            using (ZipFile zip = new ZipFile())
            {
                foreach (Packet packet in packetsToBuild)
                {
                    zip.AddFile(baseDir + packet.Name + ".pdf", "");
                }
                zip.Save(filename);
            }
            finishedOperations++;
            tasks[taskId] = finishedOperations / totalOperations;
        });

        return new OkObjectResult(taskId);
    }

    [HttpPost("api/build/progress/{id}")]
    public ActionResult Progress(Guid id)
    {
        baseDir = _env.WebRootPath + "\\iManageStorage\\";
        Boolean boo = tasks.Keys.Contains(id);

        if (boo)
        {
            return new OkObjectResult(tasks[id]);
        }
        return new OkObjectResult("Id " + id + "does not exist.");
    }

1 个答案:

答案 0 :(得分:1)

finishedOperationstotalOperations都可能存储为Int32,因此除法是整数除法。

解决方案:您可以将finishedOperations投射到Double