日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

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

發布時間:2023/12/18 C# 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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();
? ? }
?

總結

以上是生活随笔為你收集整理的C# winform 多线程中创建等待窗体的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。