在Visual Studio C#winforms中进行线程化 [英] Threading in visual studio C# winforms

查看:73
本文介绍了在Visual Studio C#winforms中进行线程化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



i需要有人帮我做以下事情:



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 ?

推荐答案

正如Richard建议的那样,使用ProgressBar - 如果你将它与 BackgroundWorker [ ^ ]然后它变得非常简单,因为worker提供了在UI线程上处理的Progress和Completed事件,因此您可以直接访问ProgressBar并更新它的状态。
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.


这篇关于在Visual Studio C#winforms中进行线程化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆