6 Replies - 278 Views - Last Post: 22 October 2019 - 01:06 AM Rate Topic: -----

#1 nilesh7136   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 129
  • Joined: 03-January 13

Thread Issue - Form close immediately when using Thread to open Form

Posted 18 October 2019 - 11:03 PM

Hello Helpers...

I am working on my question answer project, i have two form one of my Main Form where i have one textbox and one button, textbox store URL and button work to open new form each time using thread.
FrmMain Code
Imports System.Threading

Public Class frmMain
    Private Sub btnOpenBrowser_Click(sender As Object, e As EventArgs) Handles btnOpenBrowser.Click
        Dim t = New Thread(AddressOf MultiThreadBrowser)
        t.SetApartmentState(ApartmentState.STA)

        t.Start()
    End Sub

    Public Sub MultiThreadBrowser()
        Dim f2 As New FrmWebBroswer

        f2.WebBrowser1.Navigate(txtURL.Text)
        f2.Show()
    End Sub

End Class


and Next form is showing browser from this main form. and issue is form open but close immediately what i am missing please help me.

Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: Thread Issue - Form close immediately when using Thread to open Form

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15330
  • View blog
  • Posts: 61,444
  • Joined: 12-June 08

Re: Thread Issue - Form close immediately when using Thread to open Form

Posted 19 October 2019 - 08:04 AM

Any exceptions?
Was This Post Helpful? 0
  • +
  • -

#3 nilesh7136   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 129
  • Joined: 03-January 13

Re: Thread Issue - Form close immediately when using Thread to open Form

Posted 19 October 2019 - 09:29 AM

View Postmodi123_1, on 19 October 2019 - 08:04 AM, said:

Any exceptions?


No Sir, no any exceptions
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15330
  • View blog
  • Posts: 61,444
  • Joined: 12-June 08

Re: Thread Issue - Form close immediately when using Thread to open Form

Posted 19 October 2019 - 09:57 AM

Hmm. If I remember right you can't really multithread the UI in winforms. I want to say all child forms branch from the original parent form.

You would want to multithread processing or functions, but not UI.
Was This Post Helpful? 0
  • +
  • -

#5 nilesh7136   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 129
  • Joined: 03-January 13

Re: Thread Issue - Form close immediately when using Thread to open Form

Posted 19 October 2019 - 10:27 AM

View Postmodi123_1, on 19 October 2019 - 09:57 AM, said:

Hmm. If I remember right you can't really multithread the UI in winforms. I want to say all child forms branch from the original parent form.

You would want to multithread processing or functions, but not UI.


Sir Can you explain it, i am new to thread.

Thank you
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15330
  • View blog
  • Posts: 61,444
  • Joined: 12-June 08

Re: Thread Issue - Form close immediately when using Thread to open Form

Posted 19 October 2019 - 10:29 AM

From what I remember, the winform UI is a single thread. All new UI forms spawn form that thread. You should not be trying to create a new ui form in a background task. You would use that to do code processing not UI.
Was This Post Helpful? 0
  • +
  • -

#7 maceysoftware   User is offline

  • Member Title
  • member icon

Reputation: 383
  • View blog
  • Posts: 1,632
  • Joined: 07-September 13

Re: Thread Issue - Form close immediately when using Thread to open Form

Posted 22 October 2019 - 01:06 AM

While your technically correct Modi and I would stress to read what Modi is saying compared to what I am. It is possible to launch forms in different threads in WinForms.

The main reason I personally do this, is because the application I work with is multi-module, with a lot of back screen showing as dialog's, running each module in its own UI Thread allows the end-user to have dialog open on module 1 and still work on module 2 at the same time.

But basically the reason why your form is instant dying is because you assign the thread to run a method (like you have too) which shows the form as a show(), after you have shown your form the next line is 'End Sub' that is the end of the thread and therefore the thread is terminated, disposing of any objects inside it, I.E. your form.

Now if you changed your .Show() to .ShowDialog() the 'End Sub' will not be hit until you come out of your form, personally I wouldn't recommend this, just trying to show you what is happening really. the reason i wouldn't recommend this approach is because if it works (i haven't tested it) the form on your main UI and your child Thread now will be sharing the same message loop, this is what queue's up events and for me stops two dialog's me from doing work on one module while another module has a dialog open, having this control forms on two different threads may work, but I am unsure.

And with that last line, I give you the final tip in how to achieve what you want, you don't want to just create the form on a new thread, you want to give it, it's own message loop to truly allow forms to go multi-threaded.

Remember you cannot let the threads talk by default, it will result in a CrossThreadException being raised. so if you want this new form to pass something back or to directly modify the form it is called on, this is the wrong approach.

This is also not the answer if a screen is freezing due to loading times, while it will stop the rest of your application from freezing, you really need to find out what is causing the freeze and put that on the background thread, for example reading from a database or processing of some kind.

This post has been edited by maceysoftware: 22 October 2019 - 01:07 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1