0

Could anyone here check my code.

Methods still not yet finish on processing things

but Backgroundworker completed the process already.

        bg.DoWork += Bg_DoWork;

        bg.RunWorkerCompleted += (object ss, RunWorkerCompletedEventArgs ee) =>
        {
            _sysLog("Successfully uploaded.", Color.Green);              
        };

        bg.RunWorkerAsync();

Background Worker do the Work.

    private void Bg_DoWork(object sender, DoWorkEventArgs e)
    {           
        var files = Folder.GetFileToPublicFolder(Folder.srcFolder);

        foreach (var chunk in files.Split(10))
        {
            foreach (var item in chunk)
            {
                File.Move(Path.Combine(Folder.srcFolder, item.Name), Path.Combine(Folder.tmpFolder, item.Name));
            }

            ProcessParallelThread(e);
        }

    }

Parallel Thread will process all the files in the public folder.

    private void ProcessParallelThread(DoWorkEventArgs e)
    {
        object LockObject = new object();

        var Files = Folder.GetFileToPublicFolder(Folder.tmpFolder);

        Parallel.ForEach(Files, new ParallelOptions { MaxDegreeOfParallelism = 10 }, xFile =>
        {
            lock (LockObject)
            {
                bg.ReportProgress(0, Thread.CurrentThread.ManagedThreadId + " :(" + xFile + ") : Started ");
            }
            try
            {
                UploadFile(xFile);

                Thread.Sleep(1000);

                lock (LockObject)
                {
                    bg.ReportProgress(0, Thread.CurrentThread.ManagedThreadId + " :(" + xFile + ") : Ended =>>");
                }
            }
            catch (Exception)
            {
                bg.ReportProgress(0, Thread.CurrentThread.ManagedThreadId + " :(" + xFile + ") : Error X___X");

            }
        });
    }

this uploadfile will be processed based of how many thread and needs to be done.

    private void UploadFile(FileInfo file)
    {
        var slicePDF = new PDFSplitter();

        var pdf = slicePDF.SplitPdf(file);

        var poExist = new POProcess();

        if (poExist.checkPO(pdf.pono).Result)
        {
            bg.ReportProgress(0, Thread.CurrentThread.ManagedThreadId + " :(" + file.Name + ") : File Exist X___X");

            File.Delete(Folder.tmpFolder + "\\" + file.Name);

        }
        else
        {
            var processPDF = new FTPProcess();

            var link = processPDF.uploadPDF(Folder.tmpFolder + "\\" + file.Name, pdf.sid).Result;

            if (link == "Error")
            {
                bg.ReportProgress(0, Thread.CurrentThread.ManagedThreadId + " :(" + file.Name + ") : Link Error X___X");

                MoveToErrFolder(file);
            }
            else
            {
                var po = new PO();

                po.sid = pdf.sid;
                po.pono = pdf.pono;
                po.region = pdf.region;
                po.location = pdf.location;
                po.division = pdf.division;
                po.link = link;
                po.filestatus = "Available";
                po.released = DateTime.Now;
                po.expiration = DateTime.Now.AddDays(7);
                po.isExpired = "no";

                if (poExist.addPO(po).Result)
                {
                    bg.ReportProgress(0, Thread.CurrentThread.ManagedThreadId + " :(" + file.Name + ") : Success");

                    Folder.CreateBackFolder(Folder.bckFolderPath);

                    File.Move(Folder.tmpFolder + "\\" + file.Name, Folder.bckFolderPath + "\\" + file.Name);

                }
                else
                {
                    bg.ReportProgress(0, Thread.CurrentThread.ManagedThreadId + " :(" + file.Name + ") : Error in Database X___X");

                    MoveToErrFolder(file);

                }                    
            }
        }
    }

thank you in advance!

Mafii
  • 7,227
  • 1
  • 35
  • 55
Ryan Ayala
  • 77
  • 1
  • 8

2 Answers2

0

The "work" of the background worker end when all code of "DoWork" method is executed.

In that code in this code is executed another thread that will executed asyncronous respect "do work" method.

So you can:

1) Remove internal thread, background work is already executed in another thread respect the first one.

2) Convert internal thread in task and await it.

0

I think that method ProcessParallelThread will not end till all threads started in Parallel.ForEach will finish, so that is no problem

...but maybe some of your others methods like MoveToErrFolder etc. are asynchronous, if so, than that is problem.

also I don't see your BackgroundWorker initialization part, but does it have WorkerReportsProgress set to true? - required for your ReportProgress calls

makro88
  • 102
  • 8
  • yea it does. some of the method like `poExist.checkPO(pdf.pono).Result` and `processPDF.uploadPDF(Folder.tmpFolder + "\\" + file.Name, pdf.sid).Result` are all async. – Ryan Ayala Mar 17 '16 at 13:25
  • and what if `poExist.checkPO(pdf.pono)` or`processPDF.uploadPDF(Folder.tmpFolder + "\\" + file.Name, pdf.sid)` will fail? They could be also Cancelled. You should take care of it also. If any exception would happen, RunWorkerCompleted will still be fired. – makro88 Mar 17 '16 at 13:58
  • yea, that is my current problem if how could i catch that whenever the 2 processes fails like if `checkpo` encountered connection problem and `uploadpdf` failed to connect on FTP. – Ryan Ayala Mar 17 '16 at 19:54
  • Well, if u are using Tasks - be familiar with using await / async stuff. Mixing it with BackgroundWorker ... i don't think it's a good idea - hard to find out what is going on, especially while your are using it in parallel. Maybe it's time for a change of approach - check out this [solution](http://stackoverflow.com/a/11565317/5969068) – makro88 Mar 18 '16 at 07:52