熟悉WINFORM编程的请进,如何在一个EXE正在执进某个复杂业务时,EXE不处于假死状态,其它区域还能响应用户的操作??
有个业务很复杂也很耗时,在执行过程中EXE处于一种假死状态,用户对其进行别的操作都不能响应,请问用什么方法解决,另,在这个业务执行过程中,有个进度条在标识着业务进行的程度,如何能保证进度条也能正常工作而且程序还能在此期间响应别的操作 --------------------编程问答-------------------- 类似与winrar压缩时的,点"后台"的情况.可以用多线程实现 --------------------编程问答-------------------- 最简单的办法,把那很复杂也很耗时的业务放到BackgroundWorker组件里面执行 --------------------编程问答-------------------- 用多线程来做.业务处理用新线程来处理 --------------------编程问答-------------------- 子线程 --------------------编程问答-------------------- 对,多线程,如果需要完成复杂运算之后通知主线程的话还需要委托
例子
namespace DbTools
{
internal class DbToolFunc
{
public delegate void ErrorHandler(string errorMessage);
public event ErrorHandler errorMessage;
public delegate void DataCompletedHandler(DataSet data);
public event DataCompletedHandler dataCompleted;
private Thread _threadShow;
public DbToolFunc() { }
public DbToolFunc(string xlsPathName)
{
_xlsPathName = xlsPathName;
}
private string _xlsPathName = null;
public string xlsFileName
{
set { this._xlsPathName = value; }
}
private DataSet _data = null;
public DataSet ImportData
{
set { this._data = value; }
}
private DataSet _errordata = null;
public DataSet ErrorData
{
get { return this._errordata; }
}
private void GetDataProc()
{
OleDbConnection objConn = null;
try
{
DataSet ds = new DataSet();
if (String.IsNullOrEmpty(_xlsPathName))
{
if (this.errorMessage != null) { if (this.errorMessage != null) this.errorMessage("文件名没有指定!"); }
return;
}
if (!File.Exists(_xlsPathName))
{
if (this.errorMessage != null) { if (this.errorMessage != null) this.errorMessage("文件不存在!"); }
return;
}
string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source="
+ _xlsPathName + ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";
objConn = new OleDbConnection(strConn);
objConn.Open();
DataTable schemaTable = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
OleDbDataAdapter sqlada = new OleDbDataAdapter();
foreach (DataRow dr in schemaTable.Rows)
{
string strSql = "Select * From [" + dr[2].ToString().Trim() + "]";
OleDbCommand objCmd = new OleDbCommand(strSql, objConn);
sqlada.SelectCommand = objCmd;
sqlada.Fill(ds, dr[2].ToString().Trim());
}
objConn.Close();
if (this.dataCompleted != null) this.dataCompleted(ds);
}
catch (Exception ex)
{
objConn.Close();
if (this.errorMessage != null) this.errorMessage(ex.Message);
}
}
public void GetDataSetFromExcel()
{
if (this._threadShow != null && this._threadShow.ThreadState == ThreadState.Running)
{
return;
}
this._threadShow = new Thread(new ThreadStart(GetDataProc));
this._threadShow.Start();
}
}
主线程中
public partial class DbMain : Form
{
private DbToolFunc dbTool;
public DbMain()
{
InitializeComponent();
Init();
}
private void Init()
{
dbTool = new DbToolFunc();
dbTool.errorMessage += new DbToolFunc.ErrorHandler(dbTool_errorMessage);
dbTool.dataCompleted += new DbToolFunc.DataCompletedHandler(dbTool_dataCompleted);
}
void dbTool_dataCompleted(DataSet data)
{
if (this.InvokeRequired)
{
DbToolFunc.DataCompletedHandler d = new DbToolFunc.DataCompletedHandler(dbTool_dataCompleted);
this.Invoke(d, new object[] { data });
}
else
{
this.DbView.DataSource = data.Tables[0];
dbTool.ImportData = data;
}
}
void dbTool_errorMessage(string errorMessage)
{
if (this.InvokeRequired)
{
DbToolFunc.ErrorHandler d = new DbToolFunc.ErrorHandler(dbTool_errorMessage);
this.Invoke(d, new object[] { errorMessage });
}
else
{
labError.Text = errorMessage;
}
}
}
补充:.NET技术 , C#