Threading Question. Passing parameters to a function.

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

    #1

    Threading Question. Passing parameters to a function.

    Hi,

    Having a slight problem with how to pass parameters to functions between
    threads.

    I have a form with a progress bar and a description label on it. It has a
    public function
    IncrementProgre ssBar() that just calls performstep on the progress bar.


    On my main form I create a thread which creates the progress bar form

    Then using

    MethodInvoker mi = new MethodInvoker(m _ProgressBar.In crementProgress Bar);

    this.BeginInvok e(mi);

    I can get this function called wherever is appropriate.

    All is fine. But I want a function that takes a string as a parameter so I
    can dynamically update the description.

    Any idea how to do this? I've searched google and found nothing so far.

    I've tried

    MethodInvoker mi = new MethodInvoker(m _ProgressBar.Up dateDescription );

    object[] args = {m_txtUpdateDes cription.Text};

    this.BeginInvok e(m_ProgressBar .UpdateTitle, args);

    but the first line doesn't compile.



    Rollasoc










  • rollasoc

    #2
    Re: Threading Question. Passing parameters to a function.

    Sorry cut and pasted the wrong bit of code. (which obviously doesn't work)

    It shoul have been
    I've tried

    MethodInvoker mi = new MethodInvoker(m _ProgressBar.Up dateDescription );

    object[] args = {m_txtUpdateDes cription.Text};

    this.BeginInvok e(m_ProgressBar .UpdateDescript ion, args);

    but the first line doesn't compile.


    "rollasoc" <hotmail@hotmai l.com> wrote in message
    news:udySx2OtDH A.1884@TK2MSFTN GP10.phx.gbl...[color=blue]
    > Hi,
    >
    > Having a slight problem with how to pass parameters to functions between
    > threads.
    >
    > I have a form with a progress bar and a description label on it. It has a
    > public function
    > IncrementProgre ssBar() that just calls performstep on the progress bar.
    >
    >
    > On my main form I create a thread which creates the progress bar form
    >
    > Then using
    >
    > MethodInvoker mi = new MethodInvoker(m _ProgressBar.In crementProgress Bar);
    >
    > this.BeginInvok e(mi);
    >
    > I can get this function called wherever is appropriate.
    >
    > All is fine. But I want a function that takes a string as a parameter so[/color]
    I[color=blue]
    > can dynamically update the description.
    >
    > Any idea how to do this? I've searched google and found nothing so far.
    >
    > I've tried
    >
    > MethodInvoker mi = new MethodInvoker(m _ProgressBar.Up dateDescription );
    >
    > object[] args = {m_txtUpdateDes cription.Text};
    >
    > this.BeginInvok e(m_ProgressBar .UpdateTitle, args);
    >
    > but the first line doesn't compile.
    >
    >
    >
    > Rollasoc
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >[/color]


    Comment

    • Adrian Vinca [MSFT]

      #3
      Re: Threading Question. Passing parameters to a function.

      I don't understand why you need to use MethodInvoke in order to call your
      function (It seems you are trying a combination of threading and
      reflection). You could use something like the next sample, where I am using
      the member variable "someMessag e" as a way of passing the string to the new
      thread. Perhaps you will have to add some synchronization to the
      someMessage variable.


      public class MyClass
      {
      private string someMessage;

      public void DoSomething()
      {
      //....
      this.someMessag e = "Some Message";
      new Thread(new ThreadStart(thi s.MyThreadFunct ion())).Start() ;

      }

      public void MyThreadFunctio n()
      {
      DoSomething(thi s.someMessage);
      }
      }

      You could also consider sending some notifications to update the
      slider/message.
      [color=blue]
      >
      >Sorry cut and pasted the wrong bit of code. (which obviously doesn't work)
      >
      >It shoul have been
      >I've tried
      >
      >MethodInvoke r mi = new MethodInvoker(m _ProgressBar.Up dateDescription );
      >
      >object[] args = {m_txtUpdateDes cription.Text};
      >
      > this.BeginInvok e(m_ProgressBar .UpdateDescript ion, args);
      >
      >but the first line doesn't compile.
      >
      >
      >"rollasoc" <hotmail@hotmai l.com> wrote in message
      >news:udySx2OtD HA.1884@TK2MSFT NGP10.phx.gbl.. .[color=green]
      >> Hi,
      >>
      >> Having a slight problem with how to pass parameters to functions between
      >> threads.
      >>
      >> I have a form with a progress bar and a description label on it. It has[/color][/color]
      a[color=blue][color=green]
      >> public function
      >> IncrementProgre ssBar() that just calls performstep on the progress bar.
      >>
      >>
      >> On my main form I create a thread which creates the progress bar form
      >>
      >> Then using
      >>
      >> MethodInvoker mi = new MethodInvoker(m _ProgressBar.In crementProgress Bar);
      >>
      >> this.BeginInvok e(mi);
      >>
      >> I can get this function called wherever is appropriate.
      >>
      >> All is fine. But I want a function that takes a string as a parameter so[/color]
      >I[color=green]
      >> can dynamically update the description.
      >>
      >> Any idea how to do this? I've searched google and found nothing so far.
      >>
      >> I've tried
      >>
      >> MethodInvoker mi = new MethodInvoker(m _ProgressBar.Up dateDescription );
      >>
      >> object[] args = {m_txtUpdateDes cription.Text};
      >>
      >> this.BeginInvok e(m_ProgressBar .UpdateTitle, args);
      >>
      >> but the first line doesn't compile.
      >>
      >>
      >>
      >> Rollasoc[/color]
      >[/color]

      Adrian Vinca [MSFT], Developer Division
      --------------------------------------------------------------------
      This reply is provided "AS IS", without warranty (express or implied).

      Note: For the benefit of the community-at-large, all responses to this
      message are best directed to the newsgroup/thread from which they
      originated.

      Comment

      • rollasoc

        #4
        Re: Threading Question. Passing parameters to a function.

        Your right. There was no need really. I was going on what few articles I
        could find. I have since found some very useful articles on the microsoft
        site.

        Completely sorted the whole thing with delegates and using BeginInvoke on
        them.

        Works great now.

        Rollasoc


        "Adrian Vinca [MSFT]" <adrian_vinca@o nline.microsoft .com> wrote in message
        news:Amw#yueuDH A.1412@cpmsftng xa06.phx.gbl...[color=blue]
        > I don't understand why you need to use MethodInvoke in order to call your
        > function (It seems you are trying a combination of threading and
        > reflection). You could use something like the next sample, where I am[/color]
        using[color=blue]
        > the member variable "someMessag e" as a way of passing the string to the[/color]
        new[color=blue]
        > thread. Perhaps you will have to add some synchronization to the
        > someMessage variable.
        >
        >
        > public class MyClass
        > {
        > private string someMessage;
        >
        > public void DoSomething()
        > {
        > //....
        > this.someMessag e = "Some Message";
        > new Thread(new ThreadStart(thi s.MyThreadFunct ion())).Start() ;
        >
        > }
        >
        > public void MyThreadFunctio n()
        > {
        > DoSomething(thi s.someMessage);
        > }
        > }
        >
        > You could also consider sending some notifications to update the
        > slider/message.
        >[color=green]
        > >
        > >Sorry cut and pasted the wrong bit of code. (which obviously doesn't[/color][/color]
        work)[color=blue][color=green]
        > >
        > >It shoul have been
        > >I've tried
        > >
        > >MethodInvoke r mi = new MethodInvoker(m _ProgressBar.Up dateDescription );
        > >
        > >object[] args = {m_txtUpdateDes cription.Text};
        > >
        > > this.BeginInvok e(m_ProgressBar .UpdateDescript ion, args);
        > >
        > >but the first line doesn't compile.
        > >
        > >
        > >"rollasoc" <hotmail@hotmai l.com> wrote in message
        > >news:udySx2OtD HA.1884@TK2MSFT NGP10.phx.gbl.. .[color=darkred]
        > >> Hi,
        > >>
        > >> Having a slight problem with how to pass parameters to functions[/color][/color][/color]
        between[color=blue][color=green][color=darkred]
        > >> threads.
        > >>
        > >> I have a form with a progress bar and a description label on it. It[/color][/color][/color]
        has[color=blue]
        > a[color=green][color=darkred]
        > >> public function
        > >> IncrementProgre ssBar() that just calls performstep on the progress bar.
        > >>
        > >>
        > >> On my main form I create a thread which creates the progress bar form
        > >>
        > >> Then using
        > >>
        > >> MethodInvoker mi = new[/color][/color][/color]
        MethodInvoker(m _ProgressBar.In crementProgress Bar);[color=blue][color=green][color=darkred]
        > >>
        > >> this.BeginInvok e(mi);
        > >>
        > >> I can get this function called wherever is appropriate.
        > >>
        > >> All is fine. But I want a function that takes a string as a parameter[/color][/color][/color]
        so[color=blue][color=green]
        > >I[color=darkred]
        > >> can dynamically update the description.
        > >>
        > >> Any idea how to do this? I've searched google and found nothing so[/color][/color][/color]
        far.[color=blue][color=green][color=darkred]
        > >>
        > >> I've tried
        > >>
        > >> MethodInvoker mi = new MethodInvoker(m _ProgressBar.Up dateDescription );
        > >>
        > >> object[] args = {m_txtUpdateDes cription.Text};
        > >>
        > >> this.BeginInvok e(m_ProgressBar .UpdateTitle, args);
        > >>
        > >> but the first line doesn't compile.
        > >>
        > >>
        > >>
        > >> Rollasoc[/color]
        > >[/color]
        >
        > Adrian Vinca [MSFT], Developer Division
        > --------------------------------------------------------------------
        > This reply is provided "AS IS", without warranty (express or implied).
        >
        > Note: For the benefit of the community-at-large, all responses to this
        > message are best directed to the newsgroup/thread from which they
        > originated.
        >[/color]


        Comment

        Working...