none
VB.net Process Handling

    Question

  • I have a function say 

    Function abc()

    .........

    End Function 

    Can anyone help me to identify if the application crashes (manually or by itself ) on executing this function. ? 

    Thanks in advance 

    Hi all 

    I would like to re frame the question in order to give a better clarity .

    Here is my code 

    Dim filepath = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\abc.xlsx"

    Public Sub Read (filepath As String)

    'Here with the hep of OLEDB Connection we connect to excel file and open it.

    'Fetch all the records in the excel file and put it to the data set 

    'For Loop starts (for each record in the excel file execute the below process)

    'Read the values from the excel file and store in the required variables  Eg name,place,animal 

    //// rest of the code 

    mechanism(name,place,animal) 

    'Close For loop 

    'close the OLEDB Connection only after all the records are processed

    End Sub 

    Function mechanism(name As String, place As String, animal As String)

    /////rest of the code 

    Completed mechanism ()

    End Function

     Function Completed mechanism()

    'Insert the current record into another sheet of the same excel file. 

    End Function

    Now the issue is that if the execution stops in between due to system crash ( PC Shut down, Process killed through task manager ....etc ) there will not be any records in the second sheet of the excel file ,as I am closing the OLEDB connection only after all the records are processed. 

    Is there a way to insert the records to the excel sheet before the connection is closed Or can we recognize the system crash and close the connection. 

    I have tried using 

     Private Sub formclosing(ByVal sender As Object, ByVal e As EventArgs)
            Connection.Dispose()
            Connection.Close()
            Connection = Nothing
        End Sub

     But when the system crashes or when the user manually kills the process this function is not being triggered.                                                                                                                        Thanks in advance for the help.                                                                                               sauthee


    sauthee


    • Edited by Sauthee Friday, March 17, 2017 2:17 PM
    Wednesday, March 15, 2017 9:48 AM

All replies

  • Hello,

    Can't say without seeing the actual code.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, March 15, 2017 11:48 AM
    Moderator
  • Depends upon what you mean by "crash". You should add Try...Catch exception handling to handle any errors that occur in the Function. However, if the app simply terminates you may not know the cause unless Windows displays a message dialog box or creates an entry in the Windows Event Log when this occurs.

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Wednesday, March 15, 2017 12:10 PM
  • The code is impossible to use in VB for Net

    A function should have a return value 

    Fuction abc() as string

    for instance


    Success
    Cor

    Wednesday, March 15, 2017 12:32 PM
  • Hi Sauthee,

    Since Function method need to return a value to the calling code, Please refer to Function Statement to modify your code. You can also post a simple sample here, so that I can help you better.

    Best Regards,

    Cherry Bu


    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.

    Friday, March 17, 2017 8:42 AM
    Moderator
  • Don't use ODBC to communicate with Excel.  Use a direct automation API instead so that the code you execute causes immediate changes in the Excel client.  I assume you are relying on Excel's auto-recovery feature to restore the document after an unexpected system shutdown.  There's nothing you can do if the user kills your application via the task manager, or if the computer just shuts off due to power loss.

    You can use the Microsoft Office Interop API for Excel, or you can use ExcelDNA which can be installed via NuGet.  ExcelDNA is nice for targeting multiple versions of Excel or for creating custom functions in Excel, but its not quite as elegant to implement as the Office Interop Toolkit.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Saturday, March 18, 2017 12:34 AM
    Moderator
  • Have just reframed the question . Thanks

    sauthee

    Saturday, March 18, 2017 3:28 PM
  • Crash means system shut down or manually closing the application through task manager .

    sauthee

    Saturday, March 18, 2017 3:33 PM
  • Crash means system shut down or manually closing the application through task manager .

    sauthee

    If the application process is terminated from Task Manager the answer is no, you can't do anything to detect that. There are certain circumstances when the app can detect a shutdown, but not when Windows crashes or the process is abnormally terminated.

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Saturday, March 18, 2017 4:16 PM
  • You edited the message but it is still no VB code, 

    Are you using maybe QuickBasic?


    Success
    Cor

    Saturday, March 18, 2017 5:23 PM
  • Hi 

    No its VB.net itself .I have not posted the exact code its just the snippet. 


    sauthee

    Monday, March 20, 2017 2:39 AM
  • No its VB.net itself .I have not posted the exact code its just the snippet.

    This might be what you are referring to:
    How to: Write Log Messages

    The point at which the program was terminated would be indicated by the last entry in the file.   This would be determined by inspection, or if the log messages were appropriately structured, could be determined by analysis on next re-start of the application.

    Monday, March 20, 2017 4:00 AM