Results 1 to 4 of 4

Thread: [RESOLVED] Application process getting "suspended"?

  1. #1

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Resolved [RESOLVED] Application process getting "suspended"?

    I'm perplexed, in my development machine and my laptop the program runs fine, but when I deploy it to another computer (they are all windows 10 64bit) the program gets "suspended" when processing the excel file. The relevant code is included. I am using a background worker to display the progress. This works just fine in my development machine and in my laptop, but hangs on this specific machine where I am trying to test it. I am even trying to kill the process but somehow the excel process still is "attached" to my program. What could be happening? Could it be the background worker? Could I not be closing it properly? I am getting clueless.

    Code:
    using Excel = Microsoft.Office.Interop.Excel;
    
    namespace EnrollmentSystem
    {
        /// <summary>
        /// Interaction logic for GenerateForm2.xaml
        /// </summary>
        public partial class GenerateForm2 : Window
        {
    
            public GenerateForm2()
            {
                InitializeComponent();
    
                BackgroundWorker worker = new BackgroundWorker();
                worker.WorkerReportsProgress = true;
                worker.DoWork += worker_DoWork;
                worker.ProgressChanged += worker_ProgressChanged;
    
                worker.RunWorkerAsync();
    
            }
    
            void worker_DoWork(object sender, DoWorkEventArgs e)
            {
                Excel.Application excelApp = null;
                Excel.Workbooks workbooks = null;
                Excel.Workbook excelBook = null;
                Excel.Sheets sheets = null;
                Excel.Worksheet excelSheet = null;
    
                try
                {
                    excelApp = new Excel.Application();
                    //workbooks = excelApp.Workbooks;
    
                    workbooks = excelApp.Workbooks;
                    excelBook = workbooks.Open(@AppDomain.CurrentDomain.BaseDirectory + "Form2.xlsx");
    
                    sheets = excelBook.Worksheets;
                    excelSheet = (Excel.Worksheet)(sheets[1]);
    
    
                    List<UnifastForm2ViewModel> all = (from medicine in enrolmentManager.LoadForm2(PublicModule.CurrentSchoolYear)
                                                       select new UnifastForm2ViewModel(medicine)).ToList();
    
                    this.Dispatcher.Invoke((Action)(() =>
                    {//this refer to form in WPF application 
                        progressBar1.Maximum = all.Count;
                    }));
    
                    int lastRow = 2;
    
                    int a = 1;
    
                    foreach (var data in all)
                    {
                        (sender as BackgroundWorker).ReportProgress(a);
    
                        excelSheet.Cells[lastRow, 1].NumberFormat = "@";
                        excelSheet.Cells[lastRow, 1] = string.Format("{0:00000}", a);
    
                        excelSheet.Cells[lastRow, 2] = data.studentID;
                        excelSheet.Cells[lastRow, 3] = data.LRN;
                        excelSheet.Cells[lastRow, 4] = data.lastName;
                        excelSheet.Cells[lastRow, 5] = data.firstName;
                        excelSheet.Cells[lastRow, 6] = data.middleName;
                        excelSheet.Cells[lastRow, 7] = data.gender;
                        excelSheet.Cells[lastRow, 9] = data.bday;
                        excelSheet.Cells[lastRow, 9] = data.courseID;
                        excelSheet.Cells[lastRow, 10] = data.yearLevel;
                        excelSheet.Cells[lastRow, 11] = data.ZIP;
                        excelSheet.Cells[lastRow, 12] = data.Address;
                        excelSheet.Cells[lastRow, 13] = data.contactNo;
                        excelSheet.Cells[lastRow, 14] = data.units;
                        excelSheet.Cells[lastRow, 15] = data.amount;
                        excelSheet.Cells[lastRow, 18] = data.description;
                        lastRow += 1;
                        a += 1;
                    }
    
                    excelApp.DisplayAlerts = false;
                    excelBook.SaveAs(@AppDomain.CurrentDomain.BaseDirectory + "Form2Filled.xlsx", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing,
                        Type.Missing, Type.Missing);
    
                }
                finally
                {
                    //excelApp.Visible = true;       
    
                    //System.Runtime.InteropServices.Marshal.ReleaseComObject(excelSheet);
                    //System.Runtime.InteropServices.Marshal.ReleaseComObject(sheets);
    
                    //if (workbooks != null)
                    //{
                    //    excelBook.Close(0);
                    //    System.Runtime.InteropServices.Marshal.ReleaseComObject(excelBook);
                    //    System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);
                    //}
    
                    //excelApp.Quit();
                    //System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
    
                    int hWnd = excelApp.Hwnd;
    
                    NAR(excelSheet);
                    NAR(sheets);
                    excelBook.Close(0);
                    NAR(excelBook);
                    NAR(workbooks);
                    excelApp.Quit();
                    NAR(excelApp);
    
                    //excelSheet = null;
                    //sheets = null;
                    //excelBook = null;
                    //workbooks = null;
                    //excelApp = null;
    
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
    
                    PublicModule.CloseApplication(hWnd);
    
                    System.Diagnostics.Process.Start(@AppDomain.CurrentDomain.BaseDirectory + "Form2Filled.xlsx");
                    //MessageBox.Show("Done!", "Status", MessageBoxButton.OK, MessageBoxImage.Exclamation);
    
                    this.Dispatcher.Invoke((Action)(() =>
                    {
                        this.Close();
                    }));
                }
            }
    
            private void NAR(object o)
            {
                try
                {
                    while ((System.Runtime.InteropServices.Marshal.ReleaseComObject(o) > 0))
                    {
    
                    }
                }
                catch
                {
                }
                finally
                {
                    o = null;
                }
            }
    
    
            void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
                progressBar1.Value = e.ProgressPercentage;
            }
    
        }
    }
    You may notice that I am filling up data of an excel template and saving it and opening it again using Process.Start, in this regard, can I just make the Excel process visible and just let the user close it whenever he/she wants to? How can I clean up the references but let it stay open?

    Thank you so much!

    Name:  error.jpg
Views: 1854
Size:  7.9 KB
    Last edited by dee-u; Jan 21st, 2019 at 02:13 AM.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Application process getting "suspended"?

    Have you investigated suspended apps? I just did a search and found some sources that suggest that apps can become suspended due to specific circumstances that can be addressed.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Application process getting "suspended"?

    My google-fu is not working right now, care to share some links?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  4. #4

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Application process getting "suspended"?

    Reinstalling Microsoft Office fixed the problem.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width