Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Moving value to textbox control from another thread

Moving value to textbox control from another thread

Scheduled Pinned Locked Moved C#
csharpgraphicshelp
16 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M musefan

    Dont know if is the best option but you can use a static property and then create an event handler that is fired when that property changes. i.e. thread sets property to required string Then your text box can listen for the event and load the static property value as its text

    T Offline
    T Offline
    The_Collector
    wrote on last edited by
    #5

    thanks for your idea but I had done it but it doesn't work.

    xxx

    1 Reply Last reply
    0
    • J J a a n s

      Check the following links... http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokerequired.aspx[^] http://msdn.microsoft.com/en-us/library/ms171728(VS.80).aspx[^]

      *jaans

      T Offline
      T Offline
      The_Collector
      wrote on last edited by
      #6

      thanks for your idea but I had tried the solutions recomended before and it still doesnt work... 1. the textbox control can't be seen in the calling class thread. 2. I incountered an error when i use a delegate & invoked method as advice by most of the experts.

      xxx

      1 Reply Last reply
      0
      • L Lost User

        "Synchronization in Threads When we have multiple threads that share data, we need to provide synchronized access to the data. We have to deal with synchronization issues related to concurrent access to variables and objects accessible by multiple threads at the same time. This is controlled by giving one thread a chance to acquire a lock on the shared resource at a time. We can think it like a box where the object is available and only one thread can enter into and the other thread is waiting outside the box until the previous one comes out." As quoted from a Code Project Article[^].

        The_Collector wrote:

        Thnx n advance to kind people who r ready to share thier knowledge

        I ain't that kind :)

        I are troll :)

        T Offline
        T Offline
        The_Collector
        wrote on last edited by
        #7

        Thank you again for ur quotes but it doesn't help me....

        xxx

        L 1 Reply Last reply
        0
        • T The_Collector

          Thank you again for ur quotes but it doesn't help me....

          xxx

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #8

          "A Textbox from another thread"? Textboxes should be confined to the UI-thread :)

          I are troll :)

          T 2 Replies Last reply
          0
          • L Lost User

            "A Textbox from another thread"? Textboxes should be confined to the UI-thread :)

            I are troll :)

            T Offline
            T Offline
            The_Collector
            wrote on last edited by
            #9

            i am attaching the complete codes on my post.... i don't know what to do anymore,,,, i'm stack.... tnx

            xxx

            1 Reply Last reply
            0
            • L Lost User

              "A Textbox from another thread"? Textboxes should be confined to the UI-thread :)

              I are troll :)

              T Offline
              T Offline
              The_Collector
              wrote on last edited by
              #10

              i don't want to be spoonfeed but at this point, i really need a solution.... tnx again

              xxx

              L 1 Reply Last reply
              0
              • T The_Collector

                i don't want to be spoonfeed but at this point, i really need a solution.... tnx again

                xxx

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #11

                I really need to stop smoking.. You were about to post code. I'll try to run your version on my machine tonight and see if I can find anything :)

                I are troll :)

                T 1 Reply Last reply
                0
                • L Lost User

                  I really need to stop smoking.. You were about to post code. I'll try to run your version on my machine tonight and see if I can find anything :)

                  I are troll :)

                  T Offline
                  T Offline
                  The_Collector
                  wrote on last edited by
                  #12

                  thanks a lot:::: below are my coding just to make sure :) : ------------------- form1.cs ------------------ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Threading; namespace webbrowser { public partial class WebBrowser : Form { public delegate void DisplayHandler(string stringMessage); public event DisplayMessage; public WebBrowser() { InitializeComponent(); } private void WebBrowser_Load(object sender, EventArgs e) { Thread threadSocket = new Thread(new ThreadStart(StartSocket)); threadSocket.Start(); } public static void StartSocket() { SocketServer mySocket = new SocketServer(); mySocket.SocketDataArrival += new SocketServer.SocketServerHandler(ShowDataArrival); mySocket.InititateServer(); } void DisplayMessage(string stringMessage) { TxtBoxMsg.Text = stringMessage; } static void ShowDataArrival(object a, SocketServerArgs e) { DisplayHandler d = new DisplayHandler(); d.Invoke(e.Message); } } } --------------------------- Class1.cs ----------- using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Net.Sockets; using System.IO; namespace webbrowser { public class SocketServer { public delegate void SocketServerHandler(object myObject, SocketServerArgs myArgs); public event SocketServerHandler SocketDataArrival; public void InititateServer() { StreamWriter streamWriter; StreamReader streamReader; NetworkStream networkStream; TcpListener tcpListener = new TcpListener(8000); tcpListener.Start(); Socket serverSocket = tcpListener.AcceptSocket(); try { if (serverSocket.Connected) { networkStream = new NetworkStream(serverSocket); streamWriter = new StreamWriter(networkStream); streamReader = new StreamReader(networkStream); SocketServerArgs myArgs = new SocketServerArgs(streamReader.ReadLine()); SocketDataArrival(this, myArgs); } } catch (SocketException ex) { Console.WriteLine(ex); } } public SocketServer() { } } public class SocketServerArgs : EventArgs { private string message; public SocketServerArgs(string message) { this.message = message; } public string Message { get { return message; } } } }

                  xxx

                  L 1 Reply Last reply
                  0
                  • T The_Collector

                    thanks a lot:::: below are my coding just to make sure :) : ------------------- form1.cs ------------------ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Threading; namespace webbrowser { public partial class WebBrowser : Form { public delegate void DisplayHandler(string stringMessage); public event DisplayMessage; public WebBrowser() { InitializeComponent(); } private void WebBrowser_Load(object sender, EventArgs e) { Thread threadSocket = new Thread(new ThreadStart(StartSocket)); threadSocket.Start(); } public static void StartSocket() { SocketServer mySocket = new SocketServer(); mySocket.SocketDataArrival += new SocketServer.SocketServerHandler(ShowDataArrival); mySocket.InititateServer(); } void DisplayMessage(string stringMessage) { TxtBoxMsg.Text = stringMessage; } static void ShowDataArrival(object a, SocketServerArgs e) { DisplayHandler d = new DisplayHandler(); d.Invoke(e.Message); } } } --------------------------- Class1.cs ----------- using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Net.Sockets; using System.IO; namespace webbrowser { public class SocketServer { public delegate void SocketServerHandler(object myObject, SocketServerArgs myArgs); public event SocketServerHandler SocketDataArrival; public void InititateServer() { StreamWriter streamWriter; StreamReader streamReader; NetworkStream networkStream; TcpListener tcpListener = new TcpListener(8000); tcpListener.Start(); Socket serverSocket = tcpListener.AcceptSocket(); try { if (serverSocket.Connected) { networkStream = new NetworkStream(serverSocket); streamWriter = new StreamWriter(networkStream); streamReader = new StreamReader(networkStream); SocketServerArgs myArgs = new SocketServerArgs(streamReader.ReadLine()); SocketDataArrival(this, myArgs); } } catch (SocketException ex) { Console.WriteLine(ex); } } public SocketServer() { } } public class SocketServerArgs : EventArgs { private string message; public SocketServerArgs(string message) { this.message = message; } public string Message { get { return message; } } } }

                    xxx

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #13

                    using System;
                    using System.Windows.Forms;
                    using System.Threading;

                    namespace webbrowser
                    {
                    public partial class WebBrowser : Form
                    {
                    public delegate void DisplayHandler(string stringMessage);
                    //public event DisplayMessage;

                        public WebBrowser()
                        {
                            InitializeComponent();
                        }
                    
                        private Thread threadSocket;
                        private SocketServer mySocket;
                    
                        private void WebBrowser\_Load(object sender, EventArgs e)
                        {
                    
                            threadSocket = new Thread(StartSocket);
                            threadSocket.Start();
                        }
                    
                        public void StartSocket()
                        {
                            mySocket = new SocketServer();
                            mySocket.SocketDataArrival += ShowDataArrival;
                            mySocket.InititateServer();
                        }
                    
                        public void DisplayMessage(string stringMessage)
                        {
                            TxtBoxMsg.Text = stringMessage;
                        }
                    
                        void ShowDataArrival(object a, SocketServerArgs e)
                        {
                            DisplayHandler d = DisplayMessage;
                            d.Invoke(e.Message);
                        }
                    }
                    

                    }

                    Something like this?

                    I are troll :)

                    T 2 Replies Last reply
                    0
                    • L Lost User

                      using System;
                      using System.Windows.Forms;
                      using System.Threading;

                      namespace webbrowser
                      {
                      public partial class WebBrowser : Form
                      {
                      public delegate void DisplayHandler(string stringMessage);
                      //public event DisplayMessage;

                          public WebBrowser()
                          {
                              InitializeComponent();
                          }
                      
                          private Thread threadSocket;
                          private SocketServer mySocket;
                      
                          private void WebBrowser\_Load(object sender, EventArgs e)
                          {
                      
                              threadSocket = new Thread(StartSocket);
                              threadSocket.Start();
                          }
                      
                          public void StartSocket()
                          {
                              mySocket = new SocketServer();
                              mySocket.SocketDataArrival += ShowDataArrival;
                              mySocket.InititateServer();
                          }
                      
                          public void DisplayMessage(string stringMessage)
                          {
                              TxtBoxMsg.Text = stringMessage;
                          }
                      
                          void ShowDataArrival(object a, SocketServerArgs e)
                          {
                              DisplayHandler d = DisplayMessage;
                              d.Invoke(e.Message);
                          }
                      }
                      

                      }

                      Something like this?

                      I are troll :)

                      T Offline
                      T Offline
                      The_Collector
                      wrote on last edited by
                      #14

                      hi, god day,,, i tried to run your corrected code but it will still give the same error "Cross-thread operation not valid: Control 'TxtBoxMsg' accessed from a thread other than the thread it was created on."

                      xxx

                      1 Reply Last reply
                      0
                      • L Lost User

                        using System;
                        using System.Windows.Forms;
                        using System.Threading;

                        namespace webbrowser
                        {
                        public partial class WebBrowser : Form
                        {
                        public delegate void DisplayHandler(string stringMessage);
                        //public event DisplayMessage;

                            public WebBrowser()
                            {
                                InitializeComponent();
                            }
                        
                            private Thread threadSocket;
                            private SocketServer mySocket;
                        
                            private void WebBrowser\_Load(object sender, EventArgs e)
                            {
                        
                                threadSocket = new Thread(StartSocket);
                                threadSocket.Start();
                            }
                        
                            public void StartSocket()
                            {
                                mySocket = new SocketServer();
                                mySocket.SocketDataArrival += ShowDataArrival;
                                mySocket.InititateServer();
                            }
                        
                            public void DisplayMessage(string stringMessage)
                            {
                                TxtBoxMsg.Text = stringMessage;
                            }
                        
                            void ShowDataArrival(object a, SocketServerArgs e)
                            {
                                DisplayHandler d = DisplayMessage;
                                d.Invoke(e.Message);
                            }
                        }
                        

                        }

                        Something like this?

                        I are troll :)

                        T Offline
                        T Offline
                        The_Collector
                        wrote on last edited by
                        #15

                        your suggested code did not run, however i still want to thank you for your effort done. your code really help a lot to solve my problem... I made a little adjustment on your corrected code.... again, thank you very much using System; using System.Windows.Forms; using System.Threading; namespace webbrowser { public partial class WebBrowser : Form { public delegate void DisplayHandler(string stringMessage); //public event DisplayMessage; public WebBrowser() { InitializeComponent(); } private Thread threadSocket; private SocketServer mySocket; private void WebBrowser_Load(object sender, EventArgs e) { threadSocket = new Thread(StartSocket); threadSocket.Start(); } public void StartSocket() { mySocket = new SocketServer(); mySocket.SocketDataArrival += ShowDataArrival; mySocket.InititateServer(); } public void DisplayMessage(string stringMessage) { this.TxtBoxMsg.Text = stringMessage; } void ShowDataArrival(object a, SocketServerArgs e) { //DisplayHandler d = DisplayMessage; //d.Invoke(e.Message); DisplayHandler d = new DisplayHandler(DisplayMessage); this.TxtBoxMsg.Invoke(d, new object[] { e.Message }); } } }

                        xxx

                        L 1 Reply Last reply
                        0
                        • T The_Collector

                          your suggested code did not run, however i still want to thank you for your effort done. your code really help a lot to solve my problem... I made a little adjustment on your corrected code.... again, thank you very much using System; using System.Windows.Forms; using System.Threading; namespace webbrowser { public partial class WebBrowser : Form { public delegate void DisplayHandler(string stringMessage); //public event DisplayMessage; public WebBrowser() { InitializeComponent(); } private Thread threadSocket; private SocketServer mySocket; private void WebBrowser_Load(object sender, EventArgs e) { threadSocket = new Thread(StartSocket); threadSocket.Start(); } public void StartSocket() { mySocket = new SocketServer(); mySocket.SocketDataArrival += ShowDataArrival; mySocket.InititateServer(); } public void DisplayMessage(string stringMessage) { this.TxtBoxMsg.Text = stringMessage; } void ShowDataArrival(object a, SocketServerArgs e) { //DisplayHandler d = DisplayMessage; //d.Invoke(e.Message); DisplayHandler d = new DisplayHandler(DisplayMessage); this.TxtBoxMsg.Invoke(d, new object[] { e.Message }); } } }

                          xxx

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #16

                          Well done :)

                          I are troll :)

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups