Exemple #1
0
 private void addFiles(string[] fList)
 {
     foreach (var item in fList)
     {
         string    name = item.Substring(item.LastIndexOf("\\") + 1);
         itemClass obj  = new itemClass(item, name);
         files.Add(obj);
     }
     previewButton.Enabled  = false;
     renameBtn.Enabled      = false;
     totprogressBar.Maximum = files.Count;
     countLabel.Text        = $"{fCount}/{files.Count}";
     for (int i = listView1.Items.Count; i < files.Count; i++)
     {
         ListViewItem item = new ListViewItem(new string[] { $"00{(i + 1)}", "Queued", files[i].currentName, "", "" });
         listView1.Items.Add(item);
     }
     fRun = true;
 }
Exemple #2
0
        private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            hashing = true;
            for (int i = fCount; i < files.Count; i++)
            {
                if (state == "paused")
                {
                    break;
                }
                if (worker.CancellationPending == true || cancel == true)
                {
                    e.Cancel = true;
                    break;
                }
                else if (files[i].skip == true)
                {
                    fCount += 1;
                    worker.ReportProgress(-1);
                    continue;
                }

                FileStream stream = File.OpenRead(files[i].currentFilePath);

                byte[] hash       = null;
                string cHex       = "";
                bool   flag       = true;
                int    bufferSize = 4096;

                byte[] readAheadBuffer, buffer;
                int    readAheadBytesRead, bytesRead;
                long   cnt = 0;

                size = stream.Length;
                // find number of times loop runs till end of stream
                cnt = size / Convert.ToInt64(bufferSize);
                // find interval in which to update progressbar
                if (cnt <= 100 && cnt > 1)
                {
                    // example -> cnt = 49, after below calc cnt = 2, so update progress bar by 2 every time loop runs
                    cnt = (100 / (cnt + 1));
                    // to show cnt < 100 or not
                    flag = false;
                }
                else if (cnt > 100)
                {
                    // example -> cnt = 200, after below calc cnt = 3, update progress bar by 1 every time loop runs cnt times (3 in this case)
                    cnt = (cnt / 100) + 1;
                }
                else if (cnt <= 1)
                {
                    // example -> cnt = 0 (size < bufferSize), cnt = 100 cause loop only runs once in this case
                    // example -> cnt = 1 (size = bufferSize = 4096), cnt = 100 cause loop only runs once in this case
                    cnt  = 100;
                    flag = false;
                }

                // set stream position to totalBytesRead which holds paused position or default 0 (start of stream)
                stream.Position = totalBytesRead;
                readAheadBuffer = new byte[bufferSize];
                // read next 4096 bytes (returns number of bytes read)
                readAheadBytesRead = stream.Read(readAheadBuffer, 0, readAheadBuffer.Length);
                // update totalBytesRead counter by number of bytes read
                totalBytesRead += readAheadBytesRead;
                // used for speed calc
                tmp1 = tmp2 = totalBytesRead;

                // if starting for the first time (not resuming)
                if (state == "unknown")
                {
                    crc32 = new Crc32Algorithm();
                    worker.ReportProgress(0, size);
                }

                do
                {
                    if (skip || remove || cancel || state == "paused")
                    {
                        break;
                    }
                    // if there are any pending operations
                    if (pending)
                    {
                        pending = false;
                        if (operation == "skip")
                        {
                            for (int j = 0; j < selIndex.Length; j++)
                            {
                                if (selIndex[j] < fCount)
                                {
                                    // already hashed file
                                    continue;
                                }
                                else if (fCount == selIndex[j])
                                {
                                    // if currently hashing file is to be skipped
                                    skip = true;
                                }
                                files[selIndex[j]].skip = true;
                            }
                            worker.ReportProgress(-2);
                            if (skip)
                            {
                                break;
                            }
                        }
                        else if (operation == "remove")
                        {
                            int tmp = fCount;
                            for (int j = 0; j < selIndex.Length; j++)
                            {
                                if (selIndex[j] < tmp)
                                {
                                    // removing already hashed file
                                    fCount -= 1;
                                    i      -= 1;
                                }
                                else if (tmp == selIndex[j])
                                {
                                    // if currently hashing file is to be removed
                                    i     -= 1;
                                    remove = true;
                                }
                                files.RemoveAt(selIndex[j]);
                            }
                            worker.ReportProgress(-2);
                            if (remove)
                            {
                                break;
                            }
                        }
                        else if (operation == "addfiles")
                        {
                            foreach (var item in fileHolder)
                            {
                                string    name = item.Substring(item.LastIndexOf("\\") + 1);
                                itemClass obj  = new itemClass(item, name);
                                files.Add(obj);
                            }
                            worker.ReportProgress(-2);
                        }
                    }
                    bytesRead = readAheadBytesRead;
                    buffer    = readAheadBuffer;

                    // read next 4096 bytes while current 4096 bytes are being processed
                    readAheadBuffer    = new byte[bufferSize];
                    readAheadBytesRead = stream.Read(readAheadBuffer, 0, readAheadBuffer.Length);

                    totalBytesRead += readAheadBytesRead;
                    tmp1            = totalBytesRead;

                    // if end of stream reached
                    if (readAheadBytesRead == 0)
                    {
                        crc32.TransformFinalBlock(buffer, 0, bytesRead);
                    }
                    else
                    {
                        crc32.TransformBlock(buffer, 0, bytesRead, buffer, 0);
                    }
                    // if cnt > 100
                    if (flag)
                    {
                        p += 1;
                        if (p == cnt)
                        {
                            p = 0;
                            worker.ReportProgress(1, totalBytesRead);
                        }
                    }
                    else
                    {
                        worker.ReportProgress(Convert.ToInt32(cnt), totalBytesRead);
                    }
                } while (readAheadBytesRead != 0);

                if (cancel == false && state != "paused" && skip == false && remove == false)
                {
                    // convert hash to readable form and store it
                    hash = crc32.Hash;
                    foreach (byte b in hash)
                    {
                        cHex += b.ToString("x2");
                    }
                    files[fCount].hash = cHex.ToUpper();
                    fCount            += 1;
                    // check if size > 4096 to prevent reporting 100% progess twice in cases of size being < 4096
                    if (size > 4096)
                    {
                        worker.ReportProgress(100, size);
                    }
                    totalBytesRead = 0;
                    p = 0;
                    // so that it creates new crc32
                    state = "unknown";
                }

                if (remove)
                {
                    remove         = false;
                    totalBytesRead = 0;
                    p     = 0;
                    state = "unknown";
                }

                if (skip)
                {
                    skip = false;
                    files[fCount].skip = true;
                    fCount            += 1;
                    totalBytesRead     = 0;
                    p     = 0;
                    state = "unknown";
                    worker.ReportProgress(-1);
                }

                if (state != "paused")
                {
                    crc32.Dispose();
                }
                stream.Dispose();
            }
            hashing = false;
        }