-
Aug 19th, 2017, 11:58 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] Pass control Text value to a method in parallel thread ?
Hi,
I'm using stored procedure in parallel thread to speed things up. Problem is that stored procedure needs values from controls in UI thread. I can store control's Text into a string variable, but is there any direct way to pass value ?
this is my example (not stored procedure though):
Code:
string MyCtlText = Combobox1.Text;
var a = new Thread(() => GetData(MyCtlText));
a.Start();
public void GetData(string parameter_value)
{
DataTable dt = new DataTable();
try
{
using (OleDbCommand cmd = new OleDbCommand("SELECT * FROM MyTable WHERE Name = '" + parameter_value + "'", conn))
{
using (OleDbDataAdapter da = new OleDbDataAdapter())
{
da.SelectCommand = cmd;
da.Fill(dt);
}
//... binding controls and so on...
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
This example works, but not If I try "var a = new Thread(() => GetData(Combobox1.Text))"; - I get "Cross-thread operation not valid: Control 'Combobox1' accessed from a thread other than the thread it was created on.'" error.
I understand this error, but my stored procedures requires a lot of parameters which would mean a lot of string variables. I'm just looking for a shorter way to do this in parallel, If It can be done.
Thanks for help in advance !
-
Aug 19th, 2017, 12:01 PM
#2
Thread Starter
Fanatic Member
Re: Pass control Text value to a method in parallel thread ?
I'm sorry, didn't realize I was on VB.NET section. Moderators, please move thread in C# section
-
Aug 19th, 2017, 12:31 PM
#3
Re: Pass control Text value to a method in parallel thread ?
If all the parameters are strings, you could create an array instead of individual variables, eg:
string [] parameterValues = new string[2] { Combobox1.Text, Combobox2.Text };
Originally Posted by LuckyLuke82
I'm sorry, didn't realize I was on VB.NET section. Moderators, please move thread in C# section
Done
-
Aug 19th, 2017, 12:53 PM
#4
Thread Starter
Fanatic Member
Re: Pass control Text value to a method in parallel thread ?
Thanks for reply and moving thread si_the_geek
If all the parameters are strings, you could create an array instead of individual variables, eg:
string [] parameterValues = new string[2] { Combobox1.Text, Combobox2.Text };
No they aren't, that's why I asked. But still better than declaring variables separately, so thanks.
I was asking only to make sure If there is a way where you can do this directly from UI thread for control properties, but guess not.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|