1 Attachment(s)
Rising events from class library called from module
Hi, I've the next problem.
I've an app that starts to work from a module wich should load a form an do other things with a class library who raise an event an the form shoud cath it.
The problem is that applicarion.run(frmMain) displays the form in the same thread the the code stops work while the form is open. And I need that the app works in this way because the user need to send files to the app using args and this avoid that the app init another instance of it (in other code) and update the form with the info when the event is raised.
Please help :(
Module
vb.net Code:
Module moMain
Public my_Class As New ClassDll.clsMain
Public Sub main()
Application.Run(frmMain)
Debug.WriteLine("Form Loaded")
my_Class.DoSomething()
End Sub
End Module
Class
vb.net Code:
Public Class clsMain
Public Event EventRaise()
Public Sub DoSomething()
RaiseEvent EventRaise()
End Sub
End Class
Form
vb.net Code:
Public Class frmMain
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddHandler my_Class.EventRaise, AddressOf raised_from_class
End Sub
Public Sub raised_from_class()
MsgBox("event raised")
End Sub
End Class
The code is also attached without compiled files.
Re: Rising events from class library called from module
Like that instead of
Code:
Application.Run(frmMain)
Additionally if you want to have more than 1 copy of the same form, you would have to recreate instances of your form, such as:
Code:
Public formMain as New frmMain
Re: Rising events from class library called from module
That doesn't work. The form closes once the event is raised.
Re: Rising events from class library called from module
You shouldn't be using the Main method for that, plain and simple. The Main method exists for three reasons:
1. Setup the app before displaying the main form.
2. Display the main form.
3. Cleanup after closing the main form.
Any code that isn't involved in any of those three activities doesn't belong in the Main method.
Re: Rising events from class library called from module
Then how i should do that?
Re: Rising events from class library called from module
Quote:
Originally Posted by
seinkraft
Then how i should do that?
Wherever is appropriate for what you're trying to do. This is obviously just a test so where you put the code is pretty much irrelevant, as long as it's somewhere that the code can be executed while the main form is open. To that end, maybe putting it in the main form would be a good idea. In a real app you wouldn't have to ask where to put the code because the method you're calling would do something useful and you'd already know where and when you wanted that useful task performed.