Threading with forms

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John

    Threading with forms

    Hi

    I am trying to create (not open) several forms in the background threads
    using the code given below at the end.

    1. Am I doing it correctly?

    2. How can I get handle sot these forms in the calling sub so I can access
    these forms after creation.

    3. How do I know when threads have finished forms creation so I can show the
    form if needed?

    Thanks

    Regards


    Code
    ====

    Private Sub Main()
    Dim t1 As New Thread(AddressO f CreateForm)
    t1.Start(GetTyp e(Form1))

    Dim t2 As New Thread(AddressO f CreateForm)
    t2.Start(GetTyp e(Form2))

    Dim t3 As New Thread(AddressO f CreateForm)
    t3.Start(GetTyp e(Form3))
    End Sub

    Public Sub CreateForm(ByVa l ft As Object)
    Dim t As Type = TryCast(ft, Type)
    If Not t Is Nothing Then
    Using f As Form = TryCast(Activat or.CreateInstan ce(t, False),
    Form)
    End Using
    End If
    End Sub


  • Cor Ligthert[MVP]

    #2
    Re: Threading with forms

    John,

    Forms are not meant to be used as child threads.

    A form is an UI which interact with a user, how would you let the user input
    data assynchonosly, we have more fingers, but there is always only one form
    active on your screen.

    Be aware that multithreading cost more processing time then single trheading
    as the threads need to be managed, therefore I don't see the sense in what
    you try to do.

    Cor

    "John" <info@nospam.in fovis.co.ukschr eef in bericht
    news:OOWU8tYrIH A.5580@TK2MSFTN GP04.phx.gbl...
    Hi
    >
    I am trying to create (not open) several forms in the background threads
    using the code given below at the end.
    >
    1. Am I doing it correctly?
    >
    2. How can I get handle sot these forms in the calling sub so I can access
    these forms after creation.
    >
    3. How do I know when threads have finished forms creation so I can show
    the form if needed?
    >
    Thanks
    >
    Regards
    >
    >
    Code
    ====
    >
    Private Sub Main()
    Dim t1 As New Thread(AddressO f CreateForm)
    t1.Start(GetTyp e(Form1))
    >
    Dim t2 As New Thread(AddressO f CreateForm)
    t2.Start(GetTyp e(Form2))
    >
    Dim t3 As New Thread(AddressO f CreateForm)
    t3.Start(GetTyp e(Form3))
    End Sub
    >
    Public Sub CreateForm(ByVa l ft As Object)
    Dim t As Type = TryCast(ft, Type)
    If Not t Is Nothing Then
    Using f As Form = TryCast(Activat or.CreateInstan ce(t, False),
    Form)
    End Using
    End If
    End Sub
    >

    Comment

    • Tom Shelton

      #3
      Re: Threading with forms

      On 2008-05-04, John <info@nospam.in fovis.co.ukwrot e:
      Hi
      >
      I am trying to create (not open) several forms in the background threads
      using the code given below at the end.
      >
      1. Am I doing it correctly?
      >
      2. How can I get handle sot these forms in the calling sub so I can access
      these forms after creation.
      >
      3. How do I know when threads have finished forms creation so I can show the
      form if needed?
      >
      Thanks
      >
      Regards
      >
      >
      Code
      >====
      >
      Private Sub Main()
      Dim t1 As New Thread(AddressO f CreateForm)
      t1.Start(GetTyp e(Form1))
      >
      Dim t2 As New Thread(AddressO f CreateForm)
      t2.Start(GetTyp e(Form2))
      >
      Dim t3 As New Thread(AddressO f CreateForm)
      t3.Start(GetTyp e(Form3))
      End Sub
      >
      Public Sub CreateForm(ByVa l ft As Object)
      Dim t As Type = TryCast(ft, Type)
      If Not t Is Nothing Then
      Using f As Form = TryCast(Activat or.CreateInstan ce(t, False), Form)
      ' Add this to show the form
      Application.Run (f)
      End Using
      End If
      End Sub
      >
      >
      Application.Run starts a new message pump on the current thread - and
      shows the form. As for access to the forms after creation... Well, if
      you intended to do that then you would need to increase the scope of you
      form variables, and that would greatly increase the complexity of the
      code. You would of course, have to make sure that all access to those
      forms was marshaled correctly (using Invoke or BeginInvoke/EndInvoke) -
      since as always, windows forms are NOT thread safe, and can not be
      directly accessed from other threads.

      Any logic on that thread would have to be in the form that is passed to
      applciation.run , since it will not return until the form is closed - in
      other words, it's a blocking method.

      Can you explain exactly what you are trying to accomplish? There is
      probably a simpler way of doing whatever it is your trying to do :)

      --
      Tom Shelton

      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: Threading with forms

        "John" <info@nospam.in fovis.co.ukschr ieb:
        I am trying to create (not open) several forms in the background threads
        using the code given below at the end.
        I suggest to open all form's in the application's main (UI) thread and use
        'Control.Invoke Required', 'Control.BeginI nvoke', and 'Control.Invoke ' to
        access the controls/forms from within the other threads.

        --
        M S Herfried K. Wagner
        M V P <URL:http://dotnet.mvps.org/>
        V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

        Comment

        • John

          #5
          Re: Threading with forms

          I have several forms and clients are complaining that they are sick of time
          that it takes to open one form after another. I was hoping that after first
          form is loaded the rest can start loading in background without being
          visible. That way when user needs them they are already loaded and become
          visible quicker.

          Thanks

          Regards

          "Tom Shelton" <tom_shelton@YO UKNOWTHEDRILLco mcast.netwrote in message
          news:uo$LCLbrIH A.3900@TK2MSFTN GP05.phx.gbl...
          On 2008-05-04, John <info@nospam.in fovis.co.ukwrot e:
          >Hi
          >>
          >I am trying to create (not open) several forms in the background threads
          >using the code given below at the end.
          >>
          >1. Am I doing it correctly?
          >>
          >2. How can I get handle sot these forms in the calling sub so I can
          >access
          >these forms after creation.
          >>
          >3. How do I know when threads have finished forms creation so I can show
          >the
          >form if needed?
          >>
          >Thanks
          >>
          >Regards
          >>
          >>
          >Code
          >>====
          >>
          > Private Sub Main()
          > Dim t1 As New Thread(AddressO f CreateForm)
          > t1.Start(GetTyp e(Form1))
          >>
          > Dim t2 As New Thread(AddressO f CreateForm)
          > t2.Start(GetTyp e(Form2))
          >>
          > Dim t3 As New Thread(AddressO f CreateForm)
          > t3.Start(GetTyp e(Form3))
          > End Sub
          >>
          > Public Sub CreateForm(ByVa l ft As Object)
          > Dim t As Type = TryCast(ft, Type)
          > If Not t Is Nothing Then
          > Using f As Form = TryCast(Activat or.CreateInstan ce(t, False),
          >Form)
          >
          ' Add this to show the form
          Application.Run (f)
          >
          > End Using
          > End If
          > End Sub
          >>
          >>
          >
          Application.Run starts a new message pump on the current thread - and
          shows the form. As for access to the forms after creation... Well, if
          you intended to do that then you would need to increase the scope of you
          form variables, and that would greatly increase the complexity of the
          code. You would of course, have to make sure that all access to those
          forms was marshaled correctly (using Invoke or BeginInvoke/EndInvoke) -
          since as always, windows forms are NOT thread safe, and can not be
          directly accessed from other threads.
          >
          Any logic on that thread would have to be in the form that is passed to
          applciation.run , since it will not return until the form is closed - in
          other words, it's a blocking method.
          >
          Can you explain exactly what you are trying to accomplish? There is
          probably a simpler way of doing whatever it is your trying to do :)
          >
          --
          Tom Shelton

          Comment

          • Mr. Arnold

            #6
            Re: Threading with forms


            "John" <info@nospam.in fovis.co.ukwrot e in message
            news:uOi385grIH A.1768@TK2MSFTN GP03.phx.gbl...
            >I have several forms and clients are complaining that they are sick of time
            >that it takes to open one form after another. I was hoping that after
            >first form is loaded the rest can start loading in background without being
            >visible. That way when user needs them they are already loaded and become
            >visible quicker.
            >
            Forms load is pretty fast. It's what you're doing during the form load that
            can slow things down. And sometimes one has to give the illusion of speed by
            not loading all the table rows into a datagrid but load a subset of data
            during the form load and other things one can do to give the illusion of
            speed.



            Comment

            • Steve Gerrard

              #7
              Re: Threading with forms

              John wrote:
              I have several forms and clients are complaining that they are sick
              of time that it takes to open one form after another. I was hoping
              that after first form is loaded the rest can start loading in
              background without being visible. That way when user needs them they
              are already loaded and become visible quicker.
              >
              It is probably the retrieval of data that is slow, not the form itself. You
              could try loading datasets in background, then hook them up to the form when it
              is opened.


              Comment

              Working...