-
Oct 24th, 2013, 02:25 AM
#1
Thread Starter
Lively Member
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
-
Oct 24th, 2013, 02:37 AM
#2
Thread Starter
Lively Member
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.
-
Oct 24th, 2013, 03:52 PM
#3
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 :/
-
Oct 24th, 2013, 04:23 PM
#4
Re: How to load different form on different thread.
Originally Posted by green.pitch
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.
-
Oct 25th, 2013, 02:01 AM
#5
Thread Starter
Lively Member
Re: How to load different form on different thread.
Originally Posted by ident
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..?
Originally Posted by Niya
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
-
Oct 25th, 2013, 04:08 AM
#6
Re: How to load different form on different thread.
Originally Posted by green.pitch
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.
Originally Posted by green.pitch
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.
-
Oct 25th, 2013, 06:14 AM
#7
Thread Starter
Lively Member
Re: How to load different form on different thread.
Originally Posted by jmcilhinney
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,
-
Oct 25th, 2013, 06:24 AM
#8
Re: How to load different form on different thread.
Originally Posted by green.pitch
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|