MultiThreaded Form

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

    MultiThreaded Form

    Does anyone have any sample code for a multi threaded form that refreshes
    itself while an extended operation is executing:

    Example: I am updating a local dataset which could potentially have 1000's
    of records. I would like to update a label in the meantime but the problem
    is that the form seems to be locked while this operation is executing.

    Any advice would be greatly appreciated.



  • Dmitriy Lapshin [C# / .NET MVP]

    #2
    Re: MultiThreaded Form

    Look at the "Safe, Simple Multithreading in Windows Forms" series by Chris
    Sells in MSDN.

    --
    Dmitriy Lapshin [C# / .NET MVP]
    X-Unity Test Studio

    Bring the power of unit testing to VS .NET IDE

    "Ken Saganowski" <ksaganowski@hi llier.com> wrote in message
    news:uoRcG6m6DH A.2404@TK2MSFTN GP11.phx.gbl...[color=blue]
    > Does anyone have any sample code for a multi threaded form that refreshes
    > itself while an extended operation is executing:
    >
    > Example: I am updating a local dataset which could potentially have[/color]
    1000's[color=blue]
    > of records. I would like to update a label in the meantime but the[/color]
    problem[color=blue]
    > is that the form seems to be locked while this operation is executing.
    >
    > Any advice would be greatly appreciated.
    >
    >
    >[/color]

    Comment

    • Joe Mayo [C# MVP]

      #3
      Re: MultiThreaded Form

      "Ken Saganowski" <ksaganowski@hi llier.com> wrote in message
      news:uoRcG6m6DH A.2404@TK2MSFTN GP11.phx.gbl...[color=blue]
      > Does anyone have any sample code for a multi threaded form that refreshes
      > itself while an extended operation is executing:
      >
      > Example: I am updating a local dataset which could potentially have[/color]
      1000's[color=blue]
      > of records. I would like to update a label in the meantime but the[/color]
      problem[color=blue]
      > is that the form seems to be locked while this operation is executing.[/color]

      Hi Ken,

      You can't make calls back onto your UI thread from a secondary thread with
      Windows Forms. Instead, need to use Control.Invoke, or one of a couple
      other specialized commands that let you marshal the call back to the main UI
      thread. Here's the first in a series of three articles that show you how to
      do this and why you are having the problems you see now:



      Joe
      --
      Welcome to C# Station!  This is a community site for people interested in applying .NET using the C# programming language.  We’ve been around since July 4th 2000 and have continued to grow over the years.  Items of interest include Articles, Books, Links, Documentation,  and Tutorials. More… Source Code If you would like to see an […]



      Comment

      • Ken Saganowski

        #4
        Re: MultiThreaded Form

        Thanks for the links. Articles contain needed information.

        Ken


        "Ken Saganowski" <ksaganowski@hi llier.com> wrote in message
        news:uoRcG6m6DH A.2404@TK2MSFTN GP11.phx.gbl...[color=blue]
        > Does anyone have any sample code for a multi threaded form that refreshes
        > itself while an extended operation is executing:
        >
        > Example: I am updating a local dataset which could potentially have[/color]
        1000's[color=blue]
        > of records. I would like to update a label in the meantime but the[/color]
        problem[color=blue]
        > is that the form seems to be locked while this operation is executing.
        >
        > Any advice would be greatly appreciated.
        >
        >
        >[/color]


        Comment

        • Ignacio Machin \( .NET/ C#  MVP \)

          #5
          Re: MultiThreaded Form

          Hi Ken,

          I just posted some code to do this, see the thread Re: Progress bar without
          GUI code in the model? a few post above your.

          IF you have any doubt post back.

          Cheers,

          --
          Ignacio Machin,
          ignacio.machin AT dot.state.fl.us
          Florida Department Of Transportation

          "Ken Saganowski" <ksaganowski@hi llier.com> wrote in message
          news:uoRcG6m6DH A.2404@TK2MSFTN GP11.phx.gbl...[color=blue]
          > Does anyone have any sample code for a multi threaded form that refreshes
          > itself while an extended operation is executing:
          >
          > Example: I am updating a local dataset which could potentially have[/color]
          1000's[color=blue]
          > of records. I would like to update a label in the meantime but the[/color]
          problem[color=blue]
          > is that the form seems to be locked while this operation is executing.
          >
          > Any advice would be greatly appreciated.
          >
          >
          >[/color]


          Comment

          • Bret Pehrson

            #6
            Re: MultiThreaded Form

            If you don't need interactivity, stay away from multithreading -- it isn't
            necessary. Simply add a call to Refresh() after you update the label text:

            Label label;
            label.Text = "Processing record " + rec_num.ToStrin g();
            label.Refresh() ;

            If you need interactivity w/ your user interface (e.g. you need to allow the
            user to cancel the operation mid-stream), you either have to resort to running
            the processing in a background thread (i.e. multithreaded) or occasionally
            allow messages to be processed (not multi-threaded):


            for (/* main processing loop*/)
            {
            // do one iteration of work

            // process any pending messages
            // Note: this uses the Win32 api, convert to .NET
            while (PeekMessage(.. ., PM_REMOVE))
            {
            TranslateMessag e(...);
            DispatchMessage (...);
            }

            // break out if user pressed cancel
            if (cancel_operati on)
            {
            break;
            }
            }

            Dig?


            Ken Saganowski wrote:[color=blue]
            >
            > Does anyone have any sample code for a multi threaded form that refreshes
            > itself while an extended operation is executing:
            >
            > Example: I am updating a local dataset which could potentially have 1000's
            > of records. I would like to update a label in the meantime but the problem
            > is that the form seems to be locked while this operation is executing.
            >
            > Any advice would be greatly appreciated.[/color]

            --
            Bret Pehrson
            mailto:bret@inf owest.com
            NOSPAM - Include this key in all e-mail correspondence <<38952rglkwdsl >>

            Comment

            Working...