连接服务器时如何显示进度条 [英] how to show a progressbar while connecting to server

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

问题描述

我有一个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!");
           }

   }

推荐答案

在后台线程中运行此代码,并在调用前显示进度条,然后在返回后将其删除.如果在这里和网络上的后台线程上有可用的信息,那么数量是很大的.

一些其他的指针.

您不应该对连接字符串进行硬编码.在app.config文件中使用connectionString元素,就是这样.

不要对程序流使用异常处理,例如如何显示消息框.如果要使用异常处理然后再使用它,那么空捕获只会降低应用程序的性能.
Run this code in a Background thread and display the progress bar before calling then remove it after return. There is a considerable amount if info available on background threads here and on the web.

Some additional pointers.

You should not hard code the connection string. Use the connectionString element in app.config file, that''s what it is for.

Don''t use Exception handling for program flow such as how you are displaying a message box. If you are going to use exception handling then use it, an empty catch does nothing but decrease the performance of you app.


您好

尝试在其他thread中显示进度条.

您必须使用委托和控件的InvokeRequired.

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokerequired.aspx [ ^ ]


或使用BackgroundWorker

看看这个链接:

http://msdn.microsoft.com/en-us/library/system.componentmodel. backgroundworker.aspx [ ^ ]
Hello

Try to show the progress bar in a different thread.

You must use delegate and control''s InvokeRequired.

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokerequired.aspx[^]


Or use BackgroundWorker Class

look at this link:

http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx[^]


BackgroundWorker是最简单的.以下代码显示了如何完成此操作:
BackgroundWorker is the easiest. The following code shows how this is done:
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.


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

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