none
Class sub not getting called in Async Program RRS feed

  • Question

  • main form:

                Task<Tuple<int, string, string, bool, bool>> Results;
                Results = FileToProcess.ReadWriteSSheetAsync(dlgSelFile, strFileName, strSaveAsFile, oWBTo, oXLTo, oSheetTo,
                                                             blnFirstTime, blnNewDate, blnFileAlreadyProcessed, pbInvoices, lblInitProgress,
                                                             backgroundWorker1);

    (Results gets filled just fine)

    FileToProcess class:  

                       public static async Task<Tuple<int, string, string, bool, bool>> ReadWriteSSheetAsync(OpenFileDialog ofd,

                                                           string strFileName, string strSaveAsFile,
                                                           _Workbook oWBTo, Microsoft.Office.Interop.Excel.Application oXLTo,
                                                           _Worksheet oSheetTo, bool blnFirstTime, bool blnNewDate,

                                                           bool blnFileAlreadyProcessed,
                                                           ProgressBar pbInvoices, System.Windows.Forms.Label lblInitProgress,

                                                           BackgroundWorker backgroundWorker1)

                            await Task.Run(() => ReadWriteAutolivHSTTax.ReadWriteSSheet(oWBTo, oSheetFrom, oSheetTo,                                                                                                     intOrigBillToRow, oXLTo, pbInvoices, backgroundWorker1, 
                                                                                     lblInitProgress));

    (above does not get called.  Program just stops)

    ReadWriteAutolivHSTTax class:

            public static void ReadWriteSSheet(_Workbook oWBTo, _Worksheet oSheetFrom, _Worksheet oSheetTo,

                                                               int intOrigBillToRow,
                                                               Microsoft.Office.Interop.Excel.Application oXLTo, ProgressBar pbInvoices,
                                                              BackgroundWorker backgroundWorker1, System.Windows.Forms.Label lblInitProgress)

    (ABOVE FUNCTION DOES NOT GET CALLED)

     
    Friday, January 18, 2019 9:10 PM

All replies

  • Hi OldeEnglishD,

    Since the code is not complete, we could not reproduce the issue on my side, could you please share a simple complete sample that could reproduce the issue via OneDrive.


    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 21, 2019 8:54 AM
    Moderator
  • You want me to submit my whole program?
    Tuesday, January 22, 2019 2:38 PM
  • I see some problems with this code. The biggest issue appears to be a threading issue. In your async method you're passing OpenFileDialog and what appears to be an Excel COM object in addition to ProgressBar. None of these objects can be called outside the UI thread. For the COM object it needs to be called on the thread that created it (in most cases, haven't verified with Excel). Attempting to do otherwise will cause the function to fail. In your posted code where you call the task I see you are capturing the task into results but until you wait on it or try to get the result you won't see any exceptions from the function call. Are you doing that?

    The ReadWriteSSheet call is ultimately where I suspect things are failing. To test this theory out remove the Task.Run around the method call. After it returns await on Task.Delay(1) so the async method is happy. If that call is successful then it is because you're referencing those types mentioned earlier in a different thread and they are crashing.

    In most cases you should consider reducing the # of parameters you're passing to this function. It makes it a lot harder to read. Consider either using Action to allow running extra code or use a simple struct/class to wrap up the "options". The BackgroundWorker, in particular, concerns me because it appears you're trying to mix async and BWC and that is probably not going to work out well. If you're in Winforms then you can use BWC without the need for tasks. Especially consider using BWC if you need cancellation and progress support.


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, January 22, 2019 3:11 PM
    Moderator
  • All I really care about, for the time being, is to be able to move/resize the window as well as observe the progressbar while the data is processing.  DoEvents works fine for this program but I am trying to make logical changes for better future compatability.  You were saying that, since I am using Winforms, I should use the BackgroundWorker instead of the async-await combination?  As of now, the BackgroundWorker is doing absolutely nothing.

    The program I use for production works just fine even if it is Synchronous with DoEvents.

    Just trying to rewrite this for Async without DoEvents.

    Tuesday, January 22, 2019 4:12 PM
  • If you want to keep the UI responsive then use BWC. This is especially true if you need to show a progress indicator that you need to update within the code. You can do it via async/await but it isn't as clean as BWC, in this scenario.

    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, January 22, 2019 4:34 PM
    Moderator
  • Thanks Michael!  Will try that route.
    Tuesday, January 22, 2019 4:48 PM
  • Hi OldeEnglishD,

    If you have any updated, please feel free let us know.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 23, 2019 2:40 AM
    Moderator