• C# winform 多线程中创建等待窗体


    1.首先创建一个WinForm窗体,可讲窗体的FormBorderStyle属性设置为"None",将窗体的标题栏去掉。窗体中可放一个PictureBox控件和两个Label控件。其中PictureBox控件存放加载等待的图片。一个Label控件可放置Text文本。

    WinForm代码如下:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;
    namespace Test
    {
        public partial class WaitForm : Form
        {
            public WaitForm()
            {            
                InitializeComponent();
                SetText("");
            }
    
            private delegate void SetTextHandler(string text);
            public void SetText(string text)
            {
                if (this.label2.InvokeRequired)
                {
                    this.Invoke(new SetTextHandler(SetText), text);
                }
                else
                {
                    this.label2.Text = text;
                }
            }
        }
    }

    2.新建一个类WaitFormService,代码如下“
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;
    using System.Windows.Forms;
    
    namespace Test
    {
    
        public class WaitFormService
        {
            public static void CreateWaitForm()
            {
                WaitFormService.Instance.CreateForm();
            }
    
            public static void CloseWaitForm()
            {
                WaitFormService.Instance.CloseForm();
            }
    
            public static void SetWaitFormCaption(string text)
            {
                WaitFormService.Instance.SetFormCaption(text);
            }
    
            private static WaitFormService _instance;
            private static readonly Object syncLock = new Object();
    
            public static WaitFormService Instance
            {
                get 
                {
                    if (WaitFormService._instance == null)
                    {
                        lock (syncLock)
                        {
                            if (WaitFormService._instance == null)
                            {
                                WaitFormService._instance = new WaitFormService();
                            }
                        }
                    }
                    return WaitFormService._instance;
                }
            }
    
            private WaitFormService()
            {
            }
    
            private Thread waitThread;
            private WaitForm waitForm;
    
            public void CreateForm()
            {
                if (waitThread != null)
                {
                    try
                    {
                        waitThread.Abort();
                    }
                    catch (Exception)
                    {
                    }
                }
    
                waitThread = new Thread(new ThreadStart(delegate()
                {
                    waitForm = new WaitForm();
                    Application.Run(waitForm);
                }));
                waitThread.Start();
            }
    
            public void CloseForm()
            {
                if (waitThread != null)
                {
                    try
                    {
                        waitThread.Abort();
                    }
                    catch (Exception)
                    {
                    }
                }
            }
    
            public void SetFormCaption(string text)
            {
                if (waitForm != null)
                {
                    try
                    {
                        waitForm.SetText(text);
                    }
                    catch (Exception)
                    {
                    }
                }
            }
        }
    }
    3.调用如下:
      try
        {
            WaitFormService.CreateWaitForm();
            Assembly asmb = Assembly.GetExecutingAssembly();
            WaitFormService.CloseWaitForm();
        }
        catch (Exception ex)
        {
            WaitFormService.CloseWaitForm();
        }
     


  • 相关阅读:
    dedecms织梦自定义表单发送到邮箱-用163邮箱发送邮件
    DedeCMS实现自定义表单提交后发送指定QQ邮箱法
    什么是授权码,它又是如何设置?
    telnet配置和telnet用法
    linux利用sendmail发送邮件的方法
    SSH会话连接超时问题
    dede 提交表单 发送邮件
    a 标签中 写页面刷新代码
    织梦dede在首页调用留言本
    PHP通过Thrift操作Hbase
  • 原文地址:https://www.cnblogs.com/514687800/p/3096505.html
Copyright © 2020-2023  润新知