Attaching a thread

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

    #1

    Attaching a thread

    How does one attach a thread so it can update the UI of a form?

    The other thing is I thought .IsBackground makes the thread active so it
    doesn't stop looping until the main thread dies. This doesn't seem to be the
    case however with it just dying after one run.

    I recall there being a Win32 API by the name AttachThreadInp ut, is this the
    answer?

    I'm using the code below to launch the thread.

    Dim MyBackgroundThr ead As System.Threadin g.Thread

    MyBackgroundThr ead = New System.Threadin g.Thread(Addres sOf
    mybackgroundche ck)

    MyBackgroundThr ead.SetApartmen tState(Threadin g.ApartmentStat e.STA)

    MyBackgroundThr ead.IsBackgroun d = True

    MyBackgroundThr ead.Start()

    Thanks,

    Adam


  • Herfried K. Wagner [MVP]

    #2
    Re: Attaching a thread

    "Adam Honek" <AdamHonek@Webm aster2001.frees erve.co.uk> schrieb:[color=blue]
    > How does one attach a thread so it can update the UI of a form?[/color]

    'Control.{Invok eRequired, BeginInvoke, Invoke}':

    Multithreading in Windows Forms applications
    <URL:http://dotnet.mvps.org/dotnet/faqs/?id=multithread ing&lang=en>
    [color=blue]
    > The other thing is I thought .IsBackground makes the thread active so it
    > doesn't stop looping until the main thread dies. This doesn't seem to be
    > the case however with it just dying after one run.[/color]

    'IsBackground' will cause the thread to be killed when the main thread it is
    belonging to terminates. You'll add additional logic such as a loop or
    'Thread.Sleep' to your thread in order to prevent it from terminating.

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

    Comment

    • Adam Honek

      #3
      Re: Attaching a thread

      1) MS has changed .resume and .suspend in .NET 2.0 but I can't
      find how it's done now.

      2) I can't see any info there regarding how to update a form in a different
      thread. It seems a global variable must be used.

      Still need to check if possibly any .begininvoke might do it.

      Adam


      "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
      news:upUNEmBdGH A.4312@TK2MSFTN GP05.phx.gbl...[color=blue]
      > "Adam Honek" <AdamHonek@Webm aster2001.frees erve.co.uk> schrieb:[color=green]
      >> How does one attach a thread so it can update the UI of a form?[/color]
      >
      > 'Control.{Invok eRequired, BeginInvoke, Invoke}':
      >
      > Multithreading in Windows Forms applications
      > <URL:http://dotnet.mvps.org/dotnet/faqs/?id=multithread ing&lang=en>
      >[color=green]
      >> The other thing is I thought .IsBackground makes the thread active so it
      >> doesn't stop looping until the main thread dies. This doesn't seem to be
      >> the case however with it just dying after one run.[/color]
      >
      > 'IsBackground' will cause the thread to be killed when the main thread it
      > is belonging to terminates. You'll add additional logic such as a loop or
      > 'Thread.Sleep' to your thread in order to prevent it from terminating.
      >
      > --
      > M S Herfried K. Wagner
      > M V P <URL:http://dotnet.mvps.org/>
      > V B <URL:http://classicvb.org/petition/>[/color]


      Comment

      • José Manuel Agüero

        #4
        Re: Attaching a thread

        Hello Adam,
        1) Resume and Suspend methods are still there, although you shouldn't use them: it's recommended the use of AutoResetEvent or other synchronization mechanisms, so that your thread decides when it can become suspended.
        2)Herfried has provided you with a link for that purpose. You use Invoke or BeginInvoke methods if you want to interact with the user interface. Anyway don't hesitate to use a global variable if you need it.

        Regards.


        "Adam Honek" <AdamHonek@Webm aster2001.frees erve.co.uk> escribió en el mensaje news:OYlflhJdGH A.4276@TK2MSFTN GP03.phx.gbl...
        | 1) MS has changed .resume and .suspend in .NET 2.0 but I can't
        | find how it's done now.
        |
        | 2) I can't see any info there regarding how to update a form in a different
        | thread. It seems a global variable must be used.
        |
        | Still need to check if possibly any .begininvoke might do it.
        |
        | Adam
        |
        |
        | "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
        | news:upUNEmBdGH A.4312@TK2MSFTN GP05.phx.gbl...
        | > "Adam Honek" <AdamHonek@Webm aster2001.frees erve.co.uk> schrieb:
        | >> How does one attach a thread so it can update the UI of a form?
        | >
        | > 'Control.{Invok eRequired, BeginInvoke, Invoke}':
        | >
        | > Multithreading in Windows Forms applications
        | > <URL:http://dotnet.mvps.org/dotnet/faqs/?id=multithread ing&lang=en>
        | >
        | >> The other thing is I thought .IsBackground makes the thread active so it
        | >> doesn't stop looping until the main thread dies. This doesn't seem to be
        | >> the case however with it just dying after one run.
        | >
        | > 'IsBackground' will cause the thread to be killed when the main thread it
        | > is belonging to terminates. You'll add additional logic such as a loop or
        | > 'Thread.Sleep' to your thread in order to prevent it from terminating.
        | >
        | > --
        | > M S Herfried K. Wagner
        | > M V P <URL:http://dotnet.mvps.org/>
        | > V B <URL:http://classicvb.org/petition/>

        Comment

        Working...