none
Looking for C#: Simple BackgroundWorker example RRS feed

  • Question

  • I have Win forms Application

    Which is  Preforming some excel automation.

    From mainform  

    Calling  some other Class which will be produce excel file( within this same class open save dialog ask for file path for export)  .

    Problem is while Excel is generating i am not able to handle any GUI operations complete Gui will be frezzeed

    I am new to backGround class Multi threading please give some sample code which help in understanding 

    1) backGround  worker class

    2) save dialog if Not Cancelled by user!!!!!

    protectedvoid myWorker_DoWork(object sender, DoWorkEventArgs e)        {            BackgroundWorker sendingWorker = (BackgroundWorker)sender;//Capture the BackgroundWorker that fired the event//  StringBuilder sb = new StringBuilder();//Declare a new string builder to store the result.int i = 0;                if (!sendingWorker.CancellationPending)//At each iteration of the loop, check if there is a cancellation request pending                 {                //sb.Append(string.Format("Counting number: {0}{1}", PerformHeavyOperation(i), Environment.NewLine));//Append the result to the string builder                btnCancel.Enabled = true;                ICECAExcelReportEEPLUS E = newICECAExcelReportEEPLUS(this);                    E.SaveBook();                    i = 100;                sendingWorker.ReportProgress(i);//Report our progress to the main thread            }                else                {                    e.Cancel = true;//If a cancellation request is pending,assgine this flag a value of true// If a cancellation request is pending, break to exit the loop                }                     // e.Result = sb.ToString();// Send our result to the main thread!        }

    public void SaveBook()
           {
               string ResultPath=null;
               MainFormInstance.Invoke((MethodInvoker)delegate //changed this.Invoke to MainFormInstance.Invoke
               {
                  
                   
                   SaveFileDialog saveFileDialog = new SaveFileDialog();
                   saveFileDialog.Filter = "Execl files (*.xlsx)|*.xlsx";
                   saveFileDialog.FilterIndex = 0;
                   saveFileDialog.RestoreDirectory = true;
                   saveFileDialog.CreatePrompt = true;
                   saveFileDialog.FileName = Filename;
                   saveFileDialog.Title = "Save path of the file to be exported";
                 //  saveFileDialog.DefaultExt = ".xls";
                   saveFileDialog.OverwritePrompt = true;
                   saveFileDialog.ValidateNames = true;
     
                   if (saveFileDialog.ShowDialog() == DialogResult.OK)
                   {
                       ResultPath = saveFileDialog.FileName;
                       if (File.Exists(ResultPath)) File.Delete(ResultPath);
                   }
               });
     
               SaveExcelWorkBook(ResultPath);
              // return ResultPath;
           }

    Thanks

    Ranjith

                                                                                                           


    Ranjith@Learner @Microsoft products

    Wednesday, August 24, 2016 12:32 PM

Answers

  • Hi Ranjith Murthy ,

    Thank you for posting here.

    >> I am new to backGround class Multi threading please give some sample code which help in understanding.

    When you use multiple thread, you should use lock to protect public resource. If no, your program will cause deadlock. This phenomenon is like GUI operator complete will be freeze.

    In addition, For avoid deadlock, we also can use Event and mutex or interlocked class, for more information, please see it. https://msdn.microsoft.com/en-us/library/mt679037.aspx

    About how to create new thread, please refer to below link

    http://www.tutorialspoint.com/csharp/csharp_multithreading.htm

    Best Regards,

    Hart


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    • Proposed as answer by Hart Wang Friday, September 2, 2016 7:46 AM
    • Marked as answer by DotNet WangModerator Wednesday, September 7, 2016 3:01 AM
    Thursday, August 25, 2016 6:04 AM