Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

i need someone to help me do the following :

private void btnChangeLength_Click(object sender, EventArgs e)
       {
          <pre>   ComboBox senderComboBox = (ComboBox)sender;
               if (senderComboBox.SelectedIndex > -1)
               {
                   spinner.show();



                   await Task.Run(() => LoadInstitutions());

                   spinner.hide();
                }

//CODE TO GENERATE NEW LENGTH
}



I need to do some threading so i will show a spinner until the code is ready.

What I have tried:

private void LoadInstitutions()
       {
          this.Invoke((MethodInvoker)delegate ()
           {
               string selectedValue = cmbEnviroment.SelectedItem.ToString().Trim();
               ConnectionClass connectionClass = new ConnectionClass();
               connectionClass.Connection(selectedValue);
            //more code to do




Can someone help me do the threading properly ?
Posted
Updated 24-Jul-18 5:25am
v2
Comments
Richard MacCutchan 24-Jul-18 9:20am    
Why do you think you need threading?
dan!sh 24-Jul-18 9:20am    
You need to show the code you have tried. It is highly unlikely that someone will do your work here for you.
Joe Doe234 24-Jul-18 9:24am    
i will in a minute.
dan!sh 24-Jul-18 9:33am    
Now, you are showing spinner on the screen on the UI thread until the processing is complete. This means user will not be able to do any further action until the processing is complete. If that is the case, you do not need threading at all. Also, how can sender by combobox when the event handler appears to be for button click?
Richard MacCutchan 24-Jul-18 9:37am    
Use a progress bar, it is much more user friendly.

1 solution

As Richard suggested, use a ProgressBar - if you combine that with a BackgroundWorker[^] then it becomes pretty trivial, as the worker provides Progress and Completed events which are handled on the UI thread so you can access the ProgressBar directly and update it's status.
 
Share this answer
 
v2
Comments
Joe Doe234 25-Jul-18 1:21am    
any good tutorial i can follow ? and i dont need threadin with backgroundworker right ?
OriginalGriff 25-Jul-18 1:51am    
The BackgroundWorker class encapsulates threading to make it easier for you - it's a simple way to get your processing done on a new thread while your progress gets reported on the original - UI - thread via events. Just follow the link in the answer and it includes an example with the description.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900