Results 1 to 8 of 8

Thread: How to load different form on different thread.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2012
    Posts
    106

    Resolved How to load different form on different thread.

    Hello,
    I have a project in which i am using multiple forms. I want to load multithreaded forms on a button click event.
    After some googling I found following code:

    Form1:
    Code:
    Imports System.Threading
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            OpenFormEvent(sender, e)
        End Sub
    
        Private Sub OpenFormEvent(ByVal sender As Object, ByVal e As EventArgs)
            If Me.InvokeRequired Then
                Dim args As Object() = {sender, e}
                Me.BeginInvoke(New EventHandler(AddressOf OpenFormEvent), args)
            Else
                Dim SecondForm As New Form2()
                SecondForm.Show()
            End If
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Threading.Thread.CurrentThread.Suspend()
        End Sub
    End Class
    Form2 is opening on pressing Button1. But on pressing Form1 Button2. Form2 is also getting suspend. I dont want Form2 to get suspend on suspending current thread of Form1.
    How can prevent form2 from getting it suspend?

    Sorry for my bad english.

    Thanks,
    Regards
    Last edited by green.pitch; Oct 24th, 2013 at 02:37 AM. Reason: Marked as solved

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Feb 2012
    Posts
    106

    Re: How to load different form on different thread.

    Sorry, After doing few work, it comes true.. Now Form2 is not hanging on suspending current thread of Form1.

    Code:
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim aThread As New Threading.Thread(AddressOf bSub)
            aThread.IsBackground = True
            aThread.Start()
        End Sub
    
        Private Sub bSub(ByVal vMsg As String)
            Dim Nfrm As Form2
            Nfrm = New Form2
            Nfrm.msg = vMsg
            Application.Run(Nfrm)
        End Sub
    If anyone have any other best solution, please share..

    Thanks
    Regards,
    Last edited by green.pitch; Oct 24th, 2013 at 02:41 AM.

  3. #3
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: How to load different form on different thread.

    What on earth are you doing. You cant create a secondary thread to open a form :/

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: How to load different form on different thread.

    Quote Originally Posted by green.pitch View Post
    Sorry, After doing few work, it comes true.. Now Form2 is not hanging on suspending current thread of Form1.

    Code:
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim aThread As New Threading.Thread(AddressOf bSub)
            aThread.IsBackground = True
            aThread.Start()
        End Sub
    
        Private Sub bSub(ByVal vMsg As String)
            Dim Nfrm As Form2
            Nfrm = New Form2
            Nfrm.msg = vMsg
            Application.Run(Nfrm)
        End Sub
    If anyone have any other best solution, please share..

    Thanks
    Regards,
    I got mixed feelings about this. Application.Run creates a message loop which UI elements like Forms and controls need to work correctly but you already got a perfectly fine message loop on your main thread. I don't know weather to praise you or kick you I don't have enough information about why you want this to determine weather you should be doing it at all. At worst, its just overkill.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2012
    Posts
    106

    Re: How to load different form on different thread.

    Quote Originally Posted by ident View Post
    What on earth are you doing. You cant create a secondary thread to open a form :/
    But indeed this code is working fine with me.. Would you like to suggest how to load another form which will not be hanged on suspending the current thread of the main form..?

    Quote Originally Posted by Niya View Post
    I got mixed feelings about this. Application.Run creates a message loop which UI elements like Forms and controls need to work correctly but you already got a perfectly fine message loop on your main thread. I don't know weather to praise you or kick you I don't have enough information about why you want this to determine weather you should be doing it at all. At worst, its just overkill.
    I've not yet decided what will I do with that code, but its sure it will surely be useful to me for my further projects. I am new in vb.net. Please correct me.

    Thanks

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

    Re: How to load different form on different thread.

    Quote Originally Posted by green.pitch View Post
    But indeed this code is working fine with me.. Would you like to suggest how to load another form which will not be hanged on suspending the current thread of the main form..?
    Just like any other object, you can create a form on any thread you like. The need to do so is VERY, VERY rare though. One example is the splash screen functionality built into VB.NET. The spalsh screen is created on a secondary thread so that the startup form can continue to load on the UI thread.

    Why on earth would you be suspending the main thread in the first place though? This sounds like a solution to a problem that you've created yourself by doing something else wrong.
    Quote Originally Posted by green.pitch View Post
    I've not yet decided what will I do with that code, but its sure it will surely be useful to me for my further projects. I am new in vb.net. Please correct me.
    I guess you can do what you want but I very much doubt that this will be useful IF you do other things the right way. I've been programming in VB.NET for 10 years and never had need for such a thing beyond the splash screen functionality I mentioned earlier. You've created a solution to a problem that doesn't exist.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Feb 2012
    Posts
    106

    Re: How to load different form on different thread.

    Quote Originally Posted by jmcilhinney View Post
    Why on earth would you be suspending the main thread in the first place though? This sounds like a solution to a problem that you've created yourself by doing something else wrong.I guess you can do what you want but I very much doubt that this will be useful IF you do other things the right way. I've been programming in VB.NET for 10 years and never had need for such a thing beyond the splash screen functionality I mentioned earlier. You've created a solution to a problem that doesn't exist.
    Thanks ! You are right it's on the programmers hand how he want to use the codes. I wanted to develop few desktop widgets like CPU/RAM monitor, HDD monitor.. and so on like that.. For that i need to create many forms and don't want them to get hang coz of another form.. that's the main reason i asked such question.

    That's the sample code i posted to clear my doubt in which i am suspending current thread to let you understand what exactly i am asking..

    Thanks everyone
    Regards,

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: How to load different form on different thread.

    Quote Originally Posted by green.pitch View Post
    Thanks ! You are right it's on the programmers hand how he want to use the codes. I wanted to develop few desktop widgets like CPU/RAM monitor, HDD monitor.. and so on like that.. For that i need to create many forms and don't want them to get hang coz of another form.. that's the main reason i asked such question.
    Forms don't hang because of other forms. There are lots of applications that have many forms open at the same time, all created on the same main thread and all functioning as you'd expect. A form will only freeze if you're doing long-running work on the thread that created it. The answer is to do the work on another thread, NOT create another form on another 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