winform 新建一个线程打开窗口

lclc88com 2008-01-24 02:32:29
winform 新建一个线程打开窗口

就是要做的是,打开程序时,要加载的东西有点多.如果直接加载,程序会有一段时间"死掉"的感觉.我想开一个线程,同时启动两个窗体,就是类似欢迎窗口一样,这样,当我的主窗体,加载完了后,欢迎窗口就可以关闭了,



下面是我的代码:
private Thread calcThread; //声明一个线程
private Loading calcForm; //一个Loading窗体



calcForm.Show();
//把线程实例化,并设置优先级
calcThread = new Thread(new ThreadStart(aa.ll));
calcThread.Priority = ThreadPriority.Normal;
//启动线程
calcThread.Start();




问题:
calcThread = new Thread(new ThreadStart(aa.ll));

里面的ThreadStart参数写什么哦,我不用传参数的,



分不多,麻烦各位了,,
...全文
967 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
lalac 2008-01-24
  • 打赏
  • 举报
回复
lz试试BackgroundWorkor吧,很容易:


using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

namespace BackgroundWorkerExample
{
public class FibonacciForm : System.Windows.Forms.Form
{
private int numberToCompute = 0;
private int highestPercentageReached = 0;

private NumericUpDown numericUpDown1;
private Button startAsyncButton;
private Button cancelAsyncButton;
private ProgressBar progressBar1;
private Label resultLabel;
private BackgroundWorker backgroundWorker1;

public FibonacciForm()
{
InitializeComponent();
InitializeBackgoundWorker();
}

private void InitializeBackgoundWorker()
{
backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
}

private void startAsyncButton_Click(System.Object sender, System.EventArgs e)
{
resultLabel.Text = String.Empty;
this.numericUpDown1.Enabled = false;
this.startAsyncButton.Enabled = false;
this.cancelAsyncButton.Enabled = true;
numberToCompute = (int)numericUpDown1.Value;
highestPercentageReached = 0;
backgroundWorker1.RunWorkerAsync(numberToCompute);
}

private void cancelAsyncButton_Click(System.Object sender, System.EventArgs e)
{
this.backgroundWorker1.CancelAsync();
cancelAsyncButton.Enabled = false;
}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
e.Result = ComputeFibonacci((int)e.Argument, worker, e);
}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
MessageBox.Show(e.Error.Message);
}
else if (e.Cancelled)
{
resultLabel.Text = "Canceled";
}
else
{
resultLabel.Text = e.Result.ToString();
}

this.numericUpDown1.Enabled = true;
startAsyncButton.Enabled = true;

cancelAsyncButton.Enabled = false;
}

private void backgroundWorker1_ProgressChanged(object sender,ProgressChangedEventArgs e)
{
this.progressBar1.Value = e.ProgressPercentage;
}

long ComputeFibonacci(int n, BackgroundWorker worker, DoWorkEventArgs e)
{
// The parameter n must be >= 0 and <= 91.
// Fib(n), with n > 91, overflows a long.
if ((n < 0) || (n > 91))
{
throw new ArgumentException("value must be >= 0 and <= 91", "n");
}

long result = 0;

if (worker.CancellationPending)
{
e.Cancel = true;
}
else
{
if (n < 2)
{
result = 1;
}
else
{
result = ComputeFibonacci(n - 1, worker, e) + ComputeFibonacci(n - 2, worker, e);
}

int percentComplete = (int)((float)n / (float)numberToCompute * 100);
if (percentComplete > highestPercentageReached)
{
highestPercentageReached = percentComplete;
worker.ReportProgress(percentComplete);
}
}

return result;
}


#region Windows Form Designer generated code
private void InitializeComponent()
{
this.numericUpDown1 = new NumericUpDown();
this.startAsyncButton = new Button();
this.cancelAsyncButton = new Button();
this.resultLabel = new Label();
this.progressBar1 = new ProgressBar();
this.backgroundWorker1 = new BackgroundWorker();
((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();
this.SuspendLayout();
//
// numericUpDown1
//
this.numericUpDown1.Location = new Point(16, 16);
this.numericUpDown1.Maximum = new Decimal(new int[]{91, 0, 0, 0});
this.numericUpDown1.Minimum = new Decimal(new int[] { 1, 0, 0, 0});
this.numericUpDown1.Name = "numericUpDown1";
this.numericUpDown1.Size = new Size(80, 20);
this.numericUpDown1.TabIndex = 0;
this.numericUpDown1.Value = new Decimal(new int[] { 1, 0, 0, 0});
//
// startAsyncButton
//
this.startAsyncButton.Location = new Point(16, 72);
this.startAsyncButton.Name = "startAsyncButton";
this.startAsyncButton.Size = new Size(120, 23);
this.startAsyncButton.TabIndex = 1;
this.startAsyncButton.Text = "Start Async";
this.startAsyncButton.Click += new EventHandler(this.startAsyncButton_Click);
//
// cancelAsyncButton
//
this.cancelAsyncButton.Enabled = false;
this.cancelAsyncButton.Location = new Point(153, 72);
this.cancelAsyncButton.Name = "cancelAsyncButton";
this.cancelAsyncButton.Size = new Size(119, 23);
this.cancelAsyncButton.TabIndex = 2;
this.cancelAsyncButton.Text = "Cancel Async";
this.cancelAsyncButton.Click += new EventHandler(this.cancelAsyncButton_Click);
//
// resultLabel
//
this.resultLabel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.resultLabel.Location = new Point(112, 16);
this.resultLabel.Name = "resultLabel";
this.resultLabel.Size = new Size(160, 23);
this.resultLabel.TabIndex = 3;
this.resultLabel.Text = "(no result)";
this.resultLabel.TextAlign = ContentAlignment.MiddleCenter;
//
// progressBar1
//
this.progressBar1.Location = new Point(18, 48);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new Size(256, 8);
this.progressBar1.Step = 2;
this.progressBar1.TabIndex = 4;
//
// backgroundWorker1
//
this.backgroundWorker1.WorkerReportsProgress = true;
this.backgroundWorker1.WorkerSupportsCancellation = true;
//
// FibonacciForm
//
this.ClientSize = new System.Drawing.Size(292, 118);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.resultLabel);
this.Controls.Add(this.cancelAsyncButton);
this.Controls.Add(this.startAsyncButton);
this.Controls.Add(this.numericUpDown1);
this.Name = "FibonacciForm";
this.Text = "Fibonacci Calculator";
((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();
this.ResumeLayout(false);

}
#endregion

[STAThread]
static void Main()
{
Application.Run(new FibonacciForm());
}
}
}
lclc88com 2008-01-24
  • 打赏
  • 举报
回复
搞定..

谢谢..yh95 和benyouyong

结帖给分~~~
n1980212 2008-01-24
  • 打赏
  • 举报
回复
所有的界面操作都要在主线程里的 其它线程只能Invoke
你说的这个不需要多线程 在OnLoad()中显示欢迎就可以了
benyouyong 2008-01-24
  • 打赏
  • 举报
回复
private void ll()
{
Loading frm = new Loading();
frm.ShowDialog(this)
}

这里的this表示当前窗体。把this去掉就可以了。
sjm2003 2008-01-24
  • 打赏
  • 举报
回复
a.ll是什么
朝圆夜不圆 2008-01-24
  • 打赏
  • 举报
回复
Thread th;

public void str()
{
//form2是你要显示的欢迎窗口
Form2 frm = new Form2();
frm.ShowDialog();
}

private void Form1_Load(object sender, EventArgs e)
{
th = new Thread(new ThreadStart(str));
th.Start();
//这里写你要加载的CODE
th.Abort();

}
lclc88com 2008-01-24
  • 打赏
  • 举报
回复
欢迎窗口只是个意思,

我现在做的是"获取网络连接情况"这个功能..

ping网址有时会要很多时间...

我想弄一个窗口出来,显示正在获取信息..
he_8134 2008-01-24
  • 打赏
  • 举报
回复
搞欢迎窗口用不着多线程啊~~~

欢迎窗口显示

"主窗口"加载

完了关闭欢迎窗口显示"主窗口"
lclc88com 2008-01-24
  • 打赏
  • 举报
回复
如果把aa.ll

改成this.ll

private void ll()
{
Loading frm = new Loading();
frm.Show();
}

这样,没反应呢,可以编译成功,启动也可以,就是没窗体显示

如果,是用
frm.ShowDialog(this)

在任务管理器上到是可以看到一个窗体,(没有标题,什么都没有,也显示不出来.只在后台)

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧