连接服务器时如何显示进度条

连接服务器时如何显示进度条

本文介绍了连接服务器时如何显示进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows窗体应用程序,该应用程序正在远程使用数据库.

连接需要花费很多时间,因此我想显示一个进度条,直到它连接到服务器为止.我该怎么办?

我有一个用于打开连接的课程.这是:

I have a windows form application which is using a database on remote.

It takes to much time to connect, so I want to show a progressbar untill it''s connecting to server. How can I do this?

I have a class for opening the connection. Here it is:

public void openconn()
   {
       if (con == null)

        con = new SqlConnection("Data Source=69.10.00.000;Initial Catalog=property;user ID=******; password= ******");
       if (con.State == ConnectionState.Closed)
           try
           {
               con.Open();
           }
           catch
           {
               System.Windows.Forms.MessageBox.Show("Server not found!! please check your internet connection!");
           }

   }

推荐答案



public MainWindow()
{
    InitializeComponent();

    var bw = new BackgroundWorker();
    bw.DoWork += new DoWorkEventHandler(bw_DoWork);
    bw.WorkerSupportsCancellation = true;
    bw.WorkerReportsProgress = true;
    bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
    bw.RunWorkerAsync();

}

void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    //Update UI here
    var newText = e.UserState;
}

void bw_DoWork(object sender, DoWorkEventArgs e)
{
    var bw = (BackgroundWorker) sender;
    while (!bw.CancellationPending)
    {
        //Do RS-232 stuff here
        var newText = "Put new information here";
        bw.ReportProgress(1, newText);
    }
}




您的问题将是提供进步.我唯一可以告诉您的好主意是一次只记录几条记录.因此,获取记录计数,然后执行适当的跳过并接受",最后报告进度更改.另一个选择是猜测将花费多长时间,并让另一个BackgoundWorker报告进度.




Your problem will be is providing progress. The only good idea I can tell you is to only a few of the records at a time. So get the count of records, and then do an appropriate Skip and Take, reporting progress change in the end. The other option is to guess how long it will take, and have another BackgoundWorker that will report progress.


这篇关于连接服务器时如何显示进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 20:45