Results 1 to 6 of 6

Thread: Rising events from class library called from module

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Posts
    6

    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:
    1. Module moMain
    2.  
    3.     Public my_Class As New ClassDll.clsMain
    4.  
    5.     Public Sub main()
    6.  
    7.         Application.Run(frmMain)
    8.         Debug.WriteLine("Form Loaded")
    9.  
    10.         my_Class.DoSomething()
    11.  
    12.     End Sub
    13.  
    14. End Module

    Class
    vb.net Code:
    1. Public Class clsMain
    2.  
    3.     Public Event EventRaise()
    4.  
    5.     Public Sub DoSomething()
    6.         RaiseEvent EventRaise()
    7.     End Sub
    8.  
    9. End Class

    Form
    vb.net Code:
    1. Public Class frmMain
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         AddHandler my_Class.EventRaise, AddressOf raised_from_class
    5.     End Sub
    6.  
    7.     Public Sub raised_from_class()
    8.         MsgBox("event raised")
    9.     End Sub
    10.  
    11. End Class

    The code is also attached without compiled files.
    Attached Files Attached Files

  2. #2
    Lively Member Blupig's Avatar
    Join Date
    Apr 2008
    Posts
    118

    Re: Rising events from class library called from module

    Code:
    frmMain.Show()
    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

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Posts
    6

    Re: Rising events from class library called from module

    That doesn't work. The form closes once the event is raised.

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

    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.
    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

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Posts
    6

    Re: Rising events from class library called from module

    Then how i should do that?

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

    Re: Rising events from class library called from module

    Quote Originally Posted by seinkraft View Post
    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.
    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

Tags for this Thread

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