Hello,
I am new to C#. I want to know one thing. How can I get value from text box(or update text box value from another thread) while working on C# Threading. I tried but cant able to get. Below is the code. I want to use text box values in public void Run() function as for unlimited value and then further use in private void AddString(String s) for showing as GUI.

private void AddString(String s)
		{
            n = hi.Text; 
             
            int val = Convert.ToInt32(m);
            int val2 = Convert.ToInt32(s);
            int val3 = Convert.ToInt32(n);
            int sum = 0;
            sum = sum + val3 + val2;
            listBox1.Items.Add(sum);
           
		}
        
        public void Run()
        {   String s;
        int a = 2;
        s=a.ToString();
              while (true)
            {
                Thread.Sleep(400);
                m_form.Invoke(m_form.m_DelegateAddString, new Object[] { s });
                    // check if thread is cancelled
                if (m_EventStop.WaitOne(0, true))
                {   // clean-up operations may be placed here
                       // inform main thread that this thread stopped
                   m_EventStopped.Set();
                    return;
                }
            }
        }

Hope to hear soon.
Regards.
Nasir

Recommended Answers

All 3 Replies

if (textbox1.InvokeRequired)
            {
                //txtFilePath.Invoke(ur delegate );
            }
            else
            {
                //call ur method
            }

Hello,
Thanks for yours reply. I tried bt I am getting error at if (this.textBox1.InvokeRequired); while debugging....and break on this point.

////define public

public delegate void SetText(string text);

 //////


private void AddString(String s)
        {
           // n = hi.Text; 
            int val = Convert.ToInt32(m);
            int val2 = Convert.ToInt32(s);
            int val3 = Convert.ToInt32(n);
            int sum;
            sum = val2;
            listBox1.Items.Add(sum);

        }

        public void Run()
        {
            string s;// double v = 2;
        int a = 2;
        //hi.Text = v.ToString();

        s = a.ToString();
              while (true)
            {
                Thread.Sleep(400);
                m_form.Invoke(m_form.m_DelegateAddString, new Object[] { s });
                this.SetTextBoxValue("BOB");
                string textBoxValue = this.hi.Text;


                    // check if thread is cancelled
                if (m_EventStop.WaitOne(0, true))
                {   // clean-up operations may be placed here
                       // inform main thread that this thread stopped
                   m_EventStopped.Set();
                    return;
                }
            }
        }
        public void SetTextBoxValue(string textValue)
        {

            if (this.hi.InvokeRequired)
            {

                SetText del = new SetText(this.SetTextBoxValue);

                this.hi.Invoke(del, new object[] { textValue });

            }

            else
            {

                this.hi.Text = textValue;

            }

        }

Look I can't look at your code but I'll give you an example

delegate void Hello();

        void SetHello()
        {
            txtBox.Text = "Hello";
        }

        void Perform()
        {
            Hello setHelloDelegateInstance = new Hello(SetHello);
            if (txtBox.InvokeRequired)
            {
                txtBox.Invoke(setHelloDelegateInstance, new object[] { });
            }
            SetHello();
        }

        private void btn_Click(object sender, EventArgs e)
        {
            Thread t = new Thread(new ThreadStart(Perform));
            t.Start();
        }

You can also code your fetch textbox's text in the same mannar.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.