多线程应用程序...... [英] multithreaded application ...

查看:79
本文介绍了多线程应用程序......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,

我开发了一个完美的应用程序但需要10-15秒才能加载。这与应用程序相比很多。

如何使其快速加载,我想显示它加载的进度。我想在后台加载所有控件。



我有10个文本框,1个组合框,1个图片框,6个按钮。

以下是代码 -

Hello friends,
I have developed one application which works perfect but takes 10-15 seconds to load. this is much compare to application.
how do i make it to load fast and i want to show progress i it loads. and i want to load all the controls in the background.

I have 10 textbox''s, 1 combobox, 1 picturebox, 6 butttons.
following is the code-

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        lockfield()
        txt_fname.Focus()
        btn_updt.Enabled = False
        DateTimePicker1.Enabled = False
        Dim dt As New DataTable
        Try
            con.ConnectionString = str
            'con.Open()
            Dim adp1 As New SqlDataAdapter("select rtrim(fname)+' '+rtrim(mname)+' '+rtrim(lname) as [name] from stud", con)
            adp1.Fill(ds1, "stud")
            dt = ds1.Tables("stud")
            Me.ComboBox1.DataSource = dt
            Me.ComboBox1.DisplayMember = "name"
            Me.ComboBox1.SelectedIndex = 0
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "My Friends")
        Finally
            'con.Close()
        End Try
    End Sub





这显示字段中的数据 -



this shows data in the fields-

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
       clearfields()
       ds.Clear()
       txt_add.DataBindings.Clear()
       txt_email.DataBindings.Clear()
       txt_fname.DataBindings.Clear()
       txt_lname.DataBindings.Clear()
       txt_m1.DataBindings.Clear()
       txt_m2.DataBindings.Clear()
       txt_mname.DataBindings.Clear()
       txt_notes.DataBindings.Clear()
       txt_ph1.DataBindings.Clear()
       txt_ph2.DataBindings.Clear()
       DateTimePicker1.DataBindings.Clear()

       Try
           con.ConnectionString = str
           'con.Open()
           adp = New SqlDataAdapter("select address,bdate,email,fname,lname,mname,mobile1,mobile2,phone1,phone2,note from stud where rtrim(fname)+' '+rtrim(mname)+' '+rtrim(lname)='" & ComboBox1.Text & "'", con)
           adp.Fill(ds, "stud")
           dt1 = ds.Tables("stud")
           txt_add.DataBindings.Add("Text", ds, "stud.address")
           txt_email.DataBindings.Add("Text", ds, "stud.email")
           txt_fname.DataBindings.Add("Text", ds, "stud.fname")
           txt_lname.DataBindings.Add("Text", ds, "stud.lname")
           txt_m1.DataBindings.Add("Text", ds, "stud.mobile1")
           txt_m2.DataBindings.Add("Text", ds, "stud.mobile2")
           txt_mname.DataBindings.Add("Text", ds, "stud.mname")
           txt_notes.DataBindings.Add("Text", ds, "stud.note")
           txt_ph1.DataBindings.Add("Text", ds, "stud.phone1")
           txt_ph2.DataBindings.Add("Text", ds, "stud.phone2")
           DateTimePicker1.DataBindings.Add("Text", ds, "stud.bdate")
       Catch ex As Exception
           MsgBox(ex.Message)
       Finally
           'con.Close()
       End Try

       ' Binding Contact Image to PictureBox

       Try
           con.ConnectionString = str
           con.Open()
           Dim cmd As New SqlCommand("select photo from stud where id=(select id from stud where rtrim(fname)+' '+rtrim(mname)+' '+rtrim(lname)='" & ComboBox1.Text & "')", con)
           PictureBox1.Image = Image.FromStream(New IO.MemoryStream(CType(cmd.ExecuteScalar, Byte())))
       Catch ex As Exception
           'MsgBox(ex.Message)
       Finally
           con.Close()
       End Try
   End Sub





当表单加载所有控件时显示分散,并在5-10秒后加载。我想要做的是向用户显示加载进度,并在后台加载表单。

你能告诉我如何使用后台工作程序/多线程执行此操作?





谢谢。



When the form loads all controls shows scattered and after 5-10 seconds the''re loaded. what i want to do is show loading progress to user and and in the background load the form.
Can you guys tell me how can i do this with background worker/ multithreading?


Thanks.

推荐答案

一般来说,您需要使用单独的线程进行加载。我不知道你的应用程序在这么长时间内做了什么,但无论做什么,都可以在后台完成。在此期间,您可以显示启动画面(静态或动画),因此最终用户不会觉得加载需要太长时间。这个启动画面可以在开始时显示。

由于你还没有提供任何细节,我还会假设另一个场景:大尺寸的应用程序(如5MB),需要很长时间载入。有一种方法可以解决这个问题。您需要创建一个小的加载器应用程序,它只显示您的启动画面,并启动加载大部分应用程序。此方法用于CD和DVD,其中autorun.inf文件通向此加载器,因此当插入CD / DVD时,将显示启动画面,同时加载软件。



为了显示进度,您需要每0.5秒创建一次WM_TIMER事件,并有一种方法来测量大型应用程序加载的进度。这可以通过多种方式完成,但同样,这取决于花费这些时间加载的原因。每个TIMER事件,您在一个等于所需时间的部分中提升进度条除以经过的时间。
Generally speaking you need to use separate threads for the loading. I don''t know what your application does during these long seconds, but whatever is done, can be done in the background. During that time you can display a Splash screen (static or animated one), so the end user will not feel like it takes too long to load. This splash screen can be shown at start.
Since you haven''t provided any details, I''ll also assume another scenario: Applications with a huge size (like 5MB), takes a long time to load. There is a way to solve that as well. You need to create a small "loader" application which will only display your Splash screen, and initiate the loading of the large portion of the application. This method is used for CDs and DVDs, where the autorun.inf file leads to this "loader" so when the CD / DVD is inserted, the Splash screen is displayed, while the software is loaded.

In order to display the progress, you need to create a WM_TIMER event, every 0.5 seconds, and have the a way to measure the progress of the large application loading. That can be done in several ways, but again, it depends on the reason for taking such time to load. Every TIMER event, you promote the progress bar in a portion that is equal to the entire time required divided by the time that elapsed.


如果您需要初始化控件而不挂起软件使用线程。

a简单线程:

if you need initialize controls without hanging the software use a thread .
a simple thread :
ThreadStart ts = new ThreadStart(Initialier); // initializer is that method wich must intialize your controls
Thread t1 = new Thread(ts);
t1.Start();



但是,这只是防止挂!但是你有用户等待的问题!

因此像backgroundworker和progressbar控件这样的组件可以在你的线程完成工作之前显示动画很有用!请查看以下链接:

如何:使用背景工作者

BackgroundWorker Class

背景工作者

进度条



希望有所帮助!

祝你好运!


but this ,just prevent hanging ! but you have user waiting issue !
so some component like backgroundworker and progressbar control ,can be useful to show an animation until your thread done work! so check this links :
How to: Use a Background Worker
BackgroundWorker Class
backgroundworker
progressbar

hope help!
good luck!


这篇关于多线程应用程序......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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