user1093111
user1093111

Reputation: 1111

Updating task data

I'm trying to update the status of task. I've created a dictionary of tasks with a GUID as the key, and set the value to a double. Then I calculate how many actions, I am going to do and store it as "total operations". The percentage of the job done would be finishedOperations/totalOperations. However, when I log my data, I always get "0, until the end when the job is finished (thus the task does equate to "1". What am I doing wrong. Do I need to store the value as a float not a double?

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

Upvotes: 0

Views: 31

Answers (1)

user8259632
user8259632

Reputation:

It's possible that both finishedOperations and totalOperations are being stored as Int32 and thus the division is Integer division.

Solution: You could cast finishedOperations to Double.

Upvotes: 1

Related Questions