| | |
fetch value of text box outside thread
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2008
Posts: 2
Reputation:
Solved Threads: 0
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.
Hope to hear soon.
Regards.
Nasir
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.
C# Syntax (Toggle Plain 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 = 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
c# Syntax (Toggle Plain Text)
if (textbox1.InvokeRequired) { //txtFilePath.Invoke(ur delegate ); } else { //call ur method }
Last edited by Ramy Mahrous; May 22nd, 2008 at 1:11 pm.
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
•
•
Join Date: May 2008
Posts: 2
Reputation:
Solved Threads: 0
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;
}
}
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
You can also code your fetch textbox's text in the same mannar.
C# Syntax (Toggle Plain Text)
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(); }
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
![]() |
Other Threads in the C# Forum
- Previous Thread: connection access
- Next Thread: database
| Thread Tools | Search this Thread |
.net access ado.net algorithm array barchart bitmap box broadcast buttons c# check checkbox client combobox connection console control conversion csharp custom database datagrid datagridview dataset datetime degrees deployment developer development draganddrop drawing editing encryption enum event excel file form format forms function gdi+ hospitalmanagementinformationsystem httpwebrequest image imageprocessing index input install java label list listbox mandelbrot math mouseclick mysql operator oracle path photoshop picturebox pixelinversion post priviallages. programming radians regex remote remoting richtextbox rows serialization server sleep socket sql statistics stream string table temperature text textbox thread time timer txt update uploadatextfile usercontrol validation visualstudio webbrowser windows windowsformsapplication winforms wpf xml






