Delay.cs螺纹方法与返回值在C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace LearnThread
{
class Delay
{
public int Convert()
{
int ErrorCode = 1;
//something
//takes long time. about 9 hours.
return ErrorCode;
}
}
}
Form1.cs的
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace LearnThread
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnStart_Click(object sender, EventArgs e)
{
Delay delay = new Delay();
Thread t = new Thread(delay.Convert);
//something
MessageBox.Show("Success");
}
}
}
时滞延迟=新的延迟();在这里是错误的,因为它期望返回值。因为它包含errorcode,所以我需要返回值。我怎样才能做到这一点?后台工作者比Thread更好?请帮忙。 (当delay.Convert()正在运行时,我不应该失去对窗体的控制。)
只是一个警告。这些代码片段完成这项工作。因为在C#中设置ErrorCode为1是非常容易的任务。但是,如果您在“Convert()”方法中花费太多时间(假设中端PC需要5秒),您会在btnStart_Click()中得到ErrorCode错误。因为你没有调用线程连接,所以它远离主线程,并且你没有等待线程完成某些操作......所以,在“// something”的行中,你应该为此做点什么:) – 2012-07-08 02:43:03