我要在winfrom界面中实现一个数据加载等待界面。我使用的方法是:                ThreadStart threadDelegate = new ThreadStart(Waiting);
                if(newThread != null &&  newThread.ThreadState != ThreadState.Aborted)
                {
                   newThread.Abort();
                }
                newThread = new Thread(threadDelegate);
                newThread.Start();                //操作事件
                doLoading();                if (newThread != null && newThread.IsAlive)
                {                    newThread.Abort(); //销毁线程,自动回收托管资源
                }        private void Waiting()
        {            Frm_Wait frm_wait1 = new Frm_Wait(); //申明一个等待界面
            frm_wait1.Location = new Point(525, 277);
            frm_wait1.ShowDialog(); //线程等待           }
但是每次加载了时间的时候,都会出现frm_wait1这个界面没有完全销毁,或者frm_wait1这个界面的图片变成红色的矩形框,有时候则会报GDI一般性错误,如果数据事件doLoading执行的时间太短了,则会出现系统退出。
请求各位大哥大姐帮帮忙,怎么界面这个数据加载等待界面的问题

解决方案 »

  1.   

    你用 backgroundWorker 很简单的就能实现了例子
      

  2.   

    看看这个!
    你需要先注册codeproject网站的用户,用email地址很快。

    源代码和实现说明下图实现的是拷贝大文件时等待的画面,你可以自己修改后,点击拷贝,就是你点击加载数据源的情况,然后显示进度条,或者一个动画的窗体都可以!
      

  3.   

    http://topic.csdn.net/u/20090804/15/1fdb9202-22bf-47b8-8650-3f9596d82ff3.html
      

  4.   

    楼主,你在doLoading之后只是关掉了线程而已,但是那个form你并没有close,所以可能会有问题,你要主动的去关闭等待的form,而不是等待销毁。 线程关闭并不代表form就被关闭了,这个要区别开了。
    另外,你这样的写法是需要线程间通信的,就是说,你新开启的线程要知道doLoading之后再结束form
      

  5.   

    progressbar实现进度条
    http://topic.csdn.net/u/20091203/11/a1371284-07c3-4ed2-bb5e-4f522c57189f.html
      

  6.   

    给你参考,你可以参考我这个来改写.这样你就更熟悉Thread是怎么操作的了
    一个Process 类using System;
    using System.Collections.Generic;
    using System.Text;
    using CommonData.Forms;
    using System.Threading;
    using System.Windows.Forms;namespace CommonData
    {
        public class AdaProcess
        {
            private delegate void ProgressFormInvoke(string pcProcessName, string pcProcessInfo, int piProgressValue);
            private ProgressDialog _ProgressForm = null;
            private Thread _Thread = null;
            private System.Windows.Forms.Timer _ProcessTimer;        public AdaProcess()
            { 
                _ProcessTimer = new System.Windows.Forms.Timer();
                _ProcessTimer.Enabled = false;
                _ProcessTimer.Interval = 200;
                _ProcessTimer.Tick += new EventHandler(ProcessTimer_Tick);
            }
            public void Start(string pcProcessName, ThreadStart poThreadStart, bool plIsDialog)
            {
                string _ProcessName = pcProcessName;
                if (_Thread != null)
                {
                    return;
                }            this._ProcessTimer.Enabled = true;
                this._ProcessTimer.Start();
                this._Thread = new Thread(poThreadStart);
                this._Thread.SetApartmentState(ApartmentState.STA);
                this._Thread.Start();            if (_ProgressForm == null)
                {
                    _ProgressForm = new ProgressDialog(this);
                    _ProgressForm.Text = pcProcessName;
                    _ProgressForm.ResetStepInfo(pcProcessName, "", 0);
                    if (plIsDialog)
                    {
                        _ProgressForm.ShowDialog();
                    }
                    else
                    {
                        _ProgressForm.Show();
                    }
                }
            }
            private void ProcessTimer_Tick(object sender, EventArgs e)
            {
                if (_Thread != null && !_Thread.IsAlive)
                {
                    ResetProcessInfo("", "", 100);
                    _ProcessTimer.Stop();
                    _ProcessTimer.Enabled = false;
                    this.Stop();
                }
            }
            public void ResetProcessInfo(string pcProcessName, string pcProcessInfo, int piProgressValue)
            {
                if (_ProgressForm != null)
                {
                    ProgressFormInvoke loInvoke = new ProgressFormInvoke(_ProgressForm.ResetStepInfo);
                    _ProgressForm.Invoke(loInvoke, new object[] { pcProcessName, pcProcessInfo, piProgressValue });
                }
            }
            public void Stop()
            {
                if (_Thread == null)
                {
                    return;
                }
                try
                {
                    _Thread.Abort();
                    _ProgressForm.Invoke(new MethodInvoker(_ProgressForm.Close));
                }
                catch (Exception)
                {
                }
                finally
                {
                    _ProgressForm = null;
                    _Thread = null;
                }
            }
            public override string ToString()
            {
                return "AdaProcess";
            }
        }
    }
      

  7.   

    然后是一个显示进度条的Form
    ProgressDialog.Designer.csnamespace CommonData.Forms
    {
        partial class ProgressDialog
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;        /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }        #region Windows Form Designer generated code        /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.progressBar = new System.Windows.Forms.ProgressBar();
                this.labProcessName = new System.Windows.Forms.Label();
                this.labProcessPlan = new System.Windows.Forms.Label();
                this.btnCancel = new System.Windows.Forms.Button();
                this.labStep = new System.Windows.Forms.Label();
                this.SuspendLayout();
                // 
                // progressBar
                // 
                this.progressBar.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                            | System.Windows.Forms.AnchorStyles.Right)));
                this.progressBar.ForeColor = System.Drawing.Color.Lime;
                this.progressBar.Location = new System.Drawing.Point(5, 60);
                this.progressBar.Name = "progressBar";
                this.progressBar.Size = new System.Drawing.Size(459, 16);
                this.progressBar.TabIndex = 0;
                this.progressBar.Value = 100;
                // 
                // labProcessName
                // 
                this.labProcessName.Font = new System.Drawing.Font("SimSun", 10F);
                this.labProcessName.Location = new System.Drawing.Point(6, 3);
                this.labProcessName.Name = "labProcessName";
                this.labProcessName.Size = new System.Drawing.Size(480, 30);
                this.labProcessName.TabIndex = 1;
                this.labProcessName.Text = "Name";
                // 
                // labProcessPlan
                // 
                this.labProcessPlan.Font = new System.Drawing.Font("SimSun", 10F);
                this.labProcessPlan.Location = new System.Drawing.Point(6, 37);
                this.labProcessPlan.Name = "labProcessPlan";
                this.labProcessPlan.Size = new System.Drawing.Size(480, 17);
                this.labProcessPlan.TabIndex = 2;
                this.labProcessPlan.Text = "Info";
                // 
                // btnCancel
                // 
                this.btnCancel.Location = new System.Drawing.Point(407, 79);
                this.btnCancel.Name = "btnCancel";
                this.btnCancel.Size = new System.Drawing.Size(75, 23);
                this.btnCancel.TabIndex = 4;
                this.btnCancel.Text = "Cancel";
                this.btnCancel.UseVisualStyleBackColor = true;
                this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
                // 
                // labStep
                // 
                this.labStep.Location = new System.Drawing.Point(466, 61);
                this.labStep.Name = "labStep";
                this.labStep.Size = new System.Drawing.Size(25, 14);
                this.labStep.TabIndex = 5;
                this.labStep.Text = "00%";
                this.labStep.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
                // 
                // ProgressDialog
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(494, 108);
                this.ControlBox = false;
                this.Controls.Add(this.labStep);
                this.Controls.Add(this.btnCancel);
                this.Controls.Add(this.labProcessPlan);
                this.Controls.Add(this.labProcessName);
                this.Controls.Add(this.progressBar);
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
                this.MaximizeBox = false;
                this.MaximumSize = new System.Drawing.Size(500, 140);
                this.MinimizeBox = false;
                this.MinimumSize = new System.Drawing.Size(500, 140);
                this.Name = "ProgressDialog";
                this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
                this.Text = "Process Status";
                this.ResumeLayout(false);        }        #endregion        private System.Windows.Forms.ProgressBar progressBar;
            private System.Windows.Forms.Label labProcessName;
            private System.Windows.Forms.Label labProcessPlan;
            private System.Windows.Forms.Button btnCancel;
            private System.Windows.Forms.Label labStep;
        }
    }
      

  8.   


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace CommonData.Forms
    {
        public partial class ProgressDialog : Form
        {
            private AdaProcess _Process;        public ProgressDialog(AdaProcess poProcess)
            {
                InitializeComponent();            this._Process = poProcess;
            }
            public void ResetStepInfo(string pcProcessName, string pcProcessInfo, int piProgressValue)
            {
                this.labProcessName.Text = pcProcessName;
                this.labProcessPlan.Text = pcProcessInfo;
                this.progressBar.Value = piProgressValue;
                this.labStep.Text = piProgressValue + "%";
                this.Text = pcProcessName;
            }
            private void btnCancel_Click(object sender, EventArgs e)
            {
                if (_Process != null)
                {
                    if (AppMsg.ShowQuestionOkCancel("You will stop current thread! Are you sure?"))
                    {
                        _Process.Stop();
                    }
                }
            }
        }
    }
      

  9.   

    感谢大家,问题也基本解决。根据8楼 的思想,在doing 后,调用支线程来进行关闭页面,然后再关闭进程。就不会出现问题了。我采用了委托调用等待页面的一个方法,这个方法是关闭页面。
                    //调用支线程关闭等待页面
                    if (frm_wait != null)
                    {
                        setwait loInvoke = new setwait(frm_wait.SetClose);
                        frm_wait.Invoke(loInvoke, new object[] {true});
                    }