dispatch

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

    dispatch

    Hi,

    How can I dispatch something from a worker thread to the UI thread ?

    Thanks in advance
    greetings


  • Fred Mellender

    #2
    Re: dispatch


    "Jhon" <Jhon@a.a> wrote in message
    news:K9Cfb.5803 5$fQ7.2348178@p hobos.telenet-ops.be...[color=blue]
    > Hi,
    >
    > How can I dispatch something from a worker thread to the UI thread ?
    >
    > Thanks in advance
    > greetings
    >
    >[/color]


    Read about Form.Invoke. The following code should give you the idea:

    public class Foo
    {
    public MyFormClass TheForm; //has a TextBox called textBox; inherits
    //from System.Windows. Forms.Form

    public delegate void UIMessageHandle r(String msg);
    public static UIMessageHandle r OnUIMsgProxy;



    public Foo(MyFormClass form)
    {

    OnUIMsgProxy = new UIMessageHandle r(OnUIMsg);
    TheForm = form;
    }

    public static void OnUIMsg(String msg) //invoked on UI thread
    {
    //add new msg to the top of the current text
    String curText = TheForm.textBox .Text;
    if (curText.Length > 28000)
    curText = curText.Substri ng(0,25000);
    TheForm.textBox .Text = msg + "\r\n" + curText;
    TheForm.textBox .Refresh();
    }


    /*
    To insert text to the UI from another thread, the other thread calls:
    Foo.UIMsgInvoke ("here is some text for the UI");
    /*


    public static void UIMsgInvoke(Str ing str)
    {
    //invoke the message writer on the UI thread
    Object [] msg = new Object[1];
    msg[0] = str;

    if (TheForm.IsHand leCreated)
    TheForm.Invoke( OnUIMsgProxy, msg);

    }
    }


    Comment

    • Fred Mellender

      #3
      Re: dispatch


      "Jhon" <Jhon@a.a> wrote in message
      news:K9Cfb.5803 5$fQ7.2348178@p hobos.telenet-ops.be...[color=blue]
      > Hi,
      >
      > How can I dispatch something from a worker thread to the UI thread ?
      >
      > Thanks in advance
      > greetings
      >
      >[/color]


      Read about Form.Invoke. The following code should give you the idea:

      public class Foo
      {
      public MyFormClass TheForm; //has a TextBox called textBox; inherits
      //from System.Windows. Forms.Form

      public delegate void UIMessageHandle r(String msg);
      public static UIMessageHandle r OnUIMsgProxy;



      public Foo(MyFormClass form)
      {

      OnUIMsgProxy = new UIMessageHandle r(OnUIMsg);
      TheForm = form;
      }

      public static void OnUIMsg(String msg) //invoked on UI thread
      {
      //add new msg to the top of the current text
      String curText = TheForm.textBox .Text;
      if (curText.Length > 28000)
      curText = curText.Substri ng(0,25000);
      TheForm.textBox .Text = msg + "\r\n" + curText;
      TheForm.textBox .Refresh();
      }


      /*
      To insert text to the UI from another thread, the other thread calls:
      Foo.UIMsgInvoke ("here is some text for the UI");
      /*


      public static void UIMsgInvoke(Str ing str)
      {
      //invoke the message writer on the UI thread
      Object [] msg = new Object[1];
      msg[0] = str;

      if (TheForm.IsHand leCreated)
      TheForm.Invoke( OnUIMsgProxy, msg);

      }
      }


      Comment

      • Jhon

        #4
        Re: dispatch

        that will do it,
        Thanks,
        Greetings

        "Fred Mellender" <nospamPlease_f redm@frontierne t.net> wrote in message
        news:i3Dfb.97$c s.13@news01.roc .ny...[color=blue]
        >
        > "Jhon" <Jhon@a.a> wrote in message
        > news:K9Cfb.5803 5$fQ7.2348178@p hobos.telenet-ops.be...[color=green]
        > > Hi,
        > >
        > > How can I dispatch something from a worker thread to the UI thread ?
        > >
        > > Thanks in advance
        > > greetings
        > >
        > >[/color]
        >
        >
        > Read about Form.Invoke. The following code should give you the idea:
        >
        > public class Foo
        > {
        > public MyFormClass TheForm; //has a TextBox called textBox; inherits
        > //from System.Windows. Forms.Form
        >
        > public delegate void UIMessageHandle r(String msg);
        > public static UIMessageHandle r OnUIMsgProxy;
        >
        >
        >
        > public Foo(MyFormClass form)
        > {
        >
        > OnUIMsgProxy = new UIMessageHandle r(OnUIMsg);
        > TheForm = form;
        > }
        >
        > public static void OnUIMsg(String msg) //invoked on UI thread
        > {
        > //add new msg to the top of the current text
        > String curText = TheForm.textBox .Text;
        > if (curText.Length > 28000)
        > curText = curText.Substri ng(0,25000);
        > TheForm.textBox .Text = msg + "\r\n" + curText;
        > TheForm.textBox .Refresh();
        > }
        >
        >
        > /*
        > To insert text to the UI from another thread, the other thread calls:
        > Foo.UIMsgInvoke ("here is some text for the UI");
        > /*
        >
        >
        > public static void UIMsgInvoke(Str ing str)
        > {
        > //invoke the message writer on the UI thread
        > Object [] msg = new Object[1];
        > msg[0] = str;
        >
        > if (TheForm.IsHand leCreated)
        > TheForm.Invoke( OnUIMsgProxy, msg);
        >
        > }
        > }
        >
        >[/color]


        Comment

        • Jhon

          #5
          Re: dispatch

          that will do it,
          Thanks,
          Greetings

          "Fred Mellender" <nospamPlease_f redm@frontierne t.net> wrote in message
          news:i3Dfb.97$c s.13@news01.roc .ny...[color=blue]
          >
          > "Jhon" <Jhon@a.a> wrote in message
          > news:K9Cfb.5803 5$fQ7.2348178@p hobos.telenet-ops.be...[color=green]
          > > Hi,
          > >
          > > How can I dispatch something from a worker thread to the UI thread ?
          > >
          > > Thanks in advance
          > > greetings
          > >
          > >[/color]
          >
          >
          > Read about Form.Invoke. The following code should give you the idea:
          >
          > public class Foo
          > {
          > public MyFormClass TheForm; //has a TextBox called textBox; inherits
          > //from System.Windows. Forms.Form
          >
          > public delegate void UIMessageHandle r(String msg);
          > public static UIMessageHandle r OnUIMsgProxy;
          >
          >
          >
          > public Foo(MyFormClass form)
          > {
          >
          > OnUIMsgProxy = new UIMessageHandle r(OnUIMsg);
          > TheForm = form;
          > }
          >
          > public static void OnUIMsg(String msg) //invoked on UI thread
          > {
          > //add new msg to the top of the current text
          > String curText = TheForm.textBox .Text;
          > if (curText.Length > 28000)
          > curText = curText.Substri ng(0,25000);
          > TheForm.textBox .Text = msg + "\r\n" + curText;
          > TheForm.textBox .Refresh();
          > }
          >
          >
          > /*
          > To insert text to the UI from another thread, the other thread calls:
          > Foo.UIMsgInvoke ("here is some text for the UI");
          > /*
          >
          >
          > public static void UIMsgInvoke(Str ing str)
          > {
          > //invoke the message writer on the UI thread
          > Object [] msg = new Object[1];
          > msg[0] = str;
          >
          > if (TheForm.IsHand leCreated)
          > TheForm.Invoke( OnUIMsgProxy, msg);
          >
          > }
          > }
          >
          >[/color]


          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: dispatch

            Fred Mellender <nospamPlease_f redm@frontierne t.net> wrote:[color=blue]
            > Read about Form.Invoke. The following code should give you the idea:[/color]

            <snip>

            Just to be a bit more precise, it's not Form.Invoke but Control.Invoke.
            This is handy as it means you don't need to find out which form a
            control belongs to before invoking something on its UI thread.

            --
            Jon Skeet - <skeet@pobox.co m>
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            If replying to the group, please do not mail me too

            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: dispatch

              Fred Mellender <nospamPlease_f redm@frontierne t.net> wrote:[color=blue]
              > Read about Form.Invoke. The following code should give you the idea:[/color]

              <snip>

              Just to be a bit more precise, it's not Form.Invoke but Control.Invoke.
              This is handy as it means you don't need to find out which form a
              control belongs to before invoking something on its UI thread.

              --
              Jon Skeet - <skeet@pobox.co m>
              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

              If replying to the group, please do not mail me too

              Comment

              • Jhon

                #8
                Re: dispatch


                "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                news:MPG.19ea27 a330f662fa9897e 2@msnews.micros oft.com...[color=blue]
                > Fred Mellender <nospamPlease_f redm@frontierne t.net> wrote:[color=green]
                > > Read about Form.Invoke. The following code should give you the idea:[/color]
                >
                > <snip>
                >
                > Just to be a bit more precise, it's not Form.Invoke but Control.Invoke.
                > This is handy as it means you don't need to find out which form a
                > control belongs to before invoking something on its UI thread.[/color]

                oh, I see
                that's usefull to know

                thanks!

                [color=blue]
                >
                > --
                > Jon Skeet - <skeet@pobox.co m>
                > http://www.pobox.com/~skeet
                > If replying to the group, please do not mail me too[/color]


                Comment

                • Jhon

                  #9
                  Re: dispatch


                  "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                  news:MPG.19ea27 a330f662fa9897e 2@msnews.micros oft.com...[color=blue]
                  > Fred Mellender <nospamPlease_f redm@frontierne t.net> wrote:[color=green]
                  > > Read about Form.Invoke. The following code should give you the idea:[/color]
                  >
                  > <snip>
                  >
                  > Just to be a bit more precise, it's not Form.Invoke but Control.Invoke.
                  > This is handy as it means you don't need to find out which form a
                  > control belongs to before invoking something on its UI thread.[/color]

                  oh, I see
                  that's usefull to know

                  thanks!

                  [color=blue]
                  >
                  > --
                  > Jon Skeet - <skeet@pobox.co m>
                  > http://www.pobox.com/~skeet
                  > If replying to the group, please do not mail me too[/color]


                  Comment

                  Working...