Results 1 to 4 of 4

Thread: [RESOLVED] Pass control Text value to a method in parallel thread ?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    575

    Resolved [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 !

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    575

    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

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    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 };

    Quote Originally Posted by LuckyLuke82 View Post
    I'm sorry, didn't realize I was on VB.NET section. Moderators, please move thread in C# section
    Done

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    575

    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
  •  



Click Here to Expand Forum to Full Width