none
Don't catch exceptions from a form opened in diferent Thread (VB.Net 2015) RRS feed

  • Question

  • Hi, thanks for your attention.
    I have a form (fmPrincipal) where i show the main menu of my system. In there i have a button to open the list of logged users of my system (fmUsers). I have a thread to show the form of users, the code to open this this:

    Sub OpenWindows ()
                Try
       Dim th As System.Threading.Thread = New        Threading.Thread(AddressOf AbrirFormNuevo)
                    th.SetApartmentState(ApartmentState.STA)
                    th.Start()
                Catch ex As Exception
                    MsgBox(ex)
                End Try    
    End Sub
    
    
     Public Sub AbrirFormNuevo()
            Try
                Application.Run(fmUsers)
            Catch ex As Exception
                RegistrarLogEventos("Metodo:AbrurFormNuevo[menu principal]:" & ex.Message)
            End Try
    End Sub
    
    
    The problem is that i can't catch the exceptions of the fmUsers. I would gratefully so much if someone can helpme.
    Regards.


    Monday, August 26, 2019 12:19 PM

Answers

  • If you convert the code to VB, they you don't need a try/catch  anywhere in any code  and let the centralized exception handler catch the exception and you can log the exception too.

    https://www.codeproject.com/Articles/43182/Centralised-Exception-Handling-in-C-Windows-Applic

    • Marked as answer by CM16 Wednesday, August 28, 2019 12:16 PM
    Tuesday, August 27, 2019 9:48 AM

All replies

  • Hi

    Why are you using Threading with the opening of a Form? Why not create a New instance of the desired Form and just .Show that?


    Regards Les, Livingston, Scotland

    Monday, August 26, 2019 12:48 PM
  • Because on this form i user a recurcivelly search of users using tcp protocol.
    Monday, August 26, 2019 12:52 PM
  • Try a little bit to understand windows. 

    Windows is created when everything was running in the foreground. They created as well services and because they had the same design, services where also runs as internal windows. There where 2 concepts, the old one based on trying to spend as less memory as possible, the original one and NT. 

    However, presentation has always been in the foreground. You never noticed it probably, you can actually only run for one user in the foreground. Therefore running a second presentation in a separate thread (the background) is the same as running it in a closed box.

    What you try to do is starting two executables in one whatever it can be named and then use the second executable as a thread. 

    It is the same as using a Rolls Roys engine on a scooter. You may think you can connect it. But use it is not possible. 

       

     


    Success
    Cor

    Monday, August 26, 2019 1:19 PM
  • Did you try to replace ‘Application.Run(fmUsers)’ with ‘Dim f = New fmUsers : f.ShowDialog’?

    Monday, August 26, 2019 1:42 PM
  • Hi. Thank for your response. I need to use serparatedly the main menu of my application and the UserForm, because the UserForm works like LAN Chat Application. I think not to serve me Showdialog. 
    Monday, August 26, 2019 2:00 PM
  • Hi,

    Similarly, I recommend that you use Form. ShowDialog.

    Best Regards,

    Julie


    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.

    Tuesday, August 27, 2019 9:34 AM
    Moderator
  • If you convert the code to VB, they you don't need a try/catch  anywhere in any code  and let the centralized exception handler catch the exception and you can log the exception too.

    https://www.codeproject.com/Articles/43182/Centralised-Exception-Handling-in-C-Windows-Applic

    • Marked as answer by CM16 Wednesday, August 28, 2019 12:16 PM
    Tuesday, August 27, 2019 9:48 AM
  • Hi,

    Similarly, I recommend that you use Form. ShowDialog.

    Best Regards,

    Julie


    ulie

    I see now twice an advice to use showdialog (a modal form which blocks every interaction), why?

    If it is a simple second form to show a message then a simple extra form can be used in shown form, that is opened in its own window separately from the main thread. (Likewise a Splash screen).

    But you cannot use it like an interactive chat form, therefore the reply given by Leshay fits the most (but not easy to implement). 

    But of course, I can be wrong and can the showdialog be used to create an interactive chat which at the same time has a kind of core queue which can communicate with an other window. 

    Therefore please explain?


    Success
    Cor





    • Edited by Cor Ligthert Wednesday, August 28, 2019 12:17 PM
    Wednesday, August 28, 2019 12:06 PM
  • This is perfect. Thanks DA924x.

    Best Regards.

    Wednesday, August 28, 2019 12:16 PM