小弟才接触C#,现在有一个写好的查询函数,根据输入的一些条件,返回DATASET查询结果。但是我发现C#的多线程似乎只支持VOID的线程函数,无法进行值传递。 对于值传入已经有一些了解了,但是对于查询的结果传出还是不了解,希望高手解惑。100分,献上。多谢
解决方案 »
- 窗体事件和线程(在线等)
- 如何点击datagridview显示的一行数据,显示到texebox中,然后进行修改。
- 难题:如何绑定n张图片到DataGridView中?WinForm模式
- 那位兄台知道, 一个控制台程序, 如何使其运行后不在任务栏显示控制台窗口?
- 急~VS2008DEBUG模式启动程序,遇到错误,不报错,直接停掉~~
- 请问怎么向与DEV的gridcontrol绑定的bingdingsource中插入一行啊?
- SQL SERVER语句问题已经纠结好久了,跪求大神指点 ...
- 请问如何比较两种颜色是一样的?
- 为什么远程桌面在窗口模式下注册的热键能用,全屏模式下注册热键就没用呢?
- 水晶报表当中值的问题,请大家帮忙!!!
- 第三方控件 DevExpress中XtraGrid.GridControl设置DataView数据源时会更新DataView的值,为什么?
- [求助]WinForm中如何用Urldecode啊..........???????????????????????
delegate DataSet myDelegate() 声明一个委托
myDelegate Delegate1 = new myDelegate(fun); 实例化委托AsyncCallback a = new AsyncCallback(myCallback); 实例化异步回调委托 m.bgeinInvoke(s,a,null) 调用委托void myCallback(IAsyncResult i)
{
DataSet ss = m.EndInvoke(i); 获取返回值
}pulic DataSet fun()
{}
delegate dataset myDelegate(string s) 声明一个委托
myDelegate Delegate1 = new myDelegate(fun); 实例化委托AsyncCallback a = new AsyncCallback(myCallback); 实例化异步回调委托 m.bgeinInvoke(s,a,null) 调用委托void myCallback(IAsyncResult i)
{
DataSet ss = m.EndInvoke(i); 获取返回值
}public fun(string s)
{}
try
http://blog.csdn.net/zhzuo/archive/2004/06/10/22037.aspx
using System.Threading; public class AsyncDemo {
// The method to be executed asynchronously.
//
public DataSet TestMethod() {
return new DataSet();
}
}// The delegate must have the same signature as the method
// you want to call asynchronously.
public delegate DataSet AsyncDelegate();public class AsyncMain {
// Asynchronous method puts the thread id here.
private static int threadId; static void Main(string[] args) {
// Create an instance of the test class.
AsyncDemo ad = new AsyncDemo(); // Create the delegate.
AsyncDelegate dlgt = new AsyncDelegate(ad.TestMethod);
// Initiate the asychronous call. Include an AsyncCallback
// delegate representing the callback method, and the data
// needed to call EndInvoke.
IAsyncResult ar = dlgt.BeginInvoke(3000,
out threadId,
new AsyncCallback(CallbackMethod),
dlgt ); Console.WriteLine("Press Enter to close application.");
Console.ReadLine();
}
// Callback method must have the same signature as the
// AsyncCallback delegate.
static void CallbackMethod(IAsyncResult ar) {
// Retrieve the delegate.
AsyncDelegate dlgt = (AsyncDelegate) ar.AsyncState; // Call EndInvoke to retrieve the results.
DataSet ret = dlgt.EndInvoke(out threadId, ar);
}
}
{
int ThreadNum = 5; for(int i=0; i< ThreadNum; i++)
{
// Create an instance of the test class.
CQuery qr = new CQuery();
// Create the delegate.
AsyncDelegate dlgt = new AsyncDelegate(qr.QueryResultDS); //qr.QueryResultDS(m_QIndex,m_QKeyword,m_QSTime,m_QETime,
//m_QSort,m_QSortType,m_QFileSize,m_QFileSizeType,m_QMaxCount)
IAsyncResult ar = dlgt.BeginInvoke(m_QIndex,m_QKeyword,m_QSTime,m_QETime,m_QSort,m_QSortType,m_QFileSize,m_QFileSizeType,m_QMaxCount,dlgt,null);//以下省略
} }
请问我每个线程都要AsyncDelegate dlgt = new AsyncDelegate(qr.QueryResultDS)一下么?最后我传参数的时候编译显示BeginInvoke没有取到11个函数值是怎么回事?我的QueryResultDS函数有9个参数,请高手解答,多谢
using System.Data;
using ADODB;
using System.Threading;namespace IndexSearchLibrary
{
/// <summary>
/// CQueryThread 的摘要说明。
/// </summary>
public class CQueryThread
{ //分析数组个数
public String[,] m_QIndex;//查询的索引库,格式为{{服务器名1,索引库名1},{服务器名1,索引库名2}...}以数组形式传递
public string m_QKeyword;//查询的关键词组合
public DateTime m_QSTime;//查询的起始时间
public DateTime m_QETime;//查询的终止时间
public string m_QSort;//排序字段
public string m_QSortType;//排序方式:升序或者降序
public int m_QFileSize;//返回文件大小
public string m_QFileSizeType;//返回文件大小的限制类型 >=(大于等于)或者<=(小于等于)或者>(无限制,此时m_QFileSize=0)
public int m_QMaxCount;//返回最大条数 public delegate DataSet AsyncDelegate(String[,] m_qIndex,string m_qKeyword,DateTime m_qSTime,DateTime m_qETime,
string m_qSort,string m_qSortType,int m_qFileSize,string m_qFileSizeType,int m_qMaxCount); public DataSet CQueryThreadMeth()
{
//Thread [] myThread = new Thread[20];
//int ThreadNum = 1;
DataSet myDt; // Create an instance of the test class.
CQuery qr = new CQuery();
// Create the delegate.
//myThread[0] = new Thread(new ThreadStart(qr.QueryResultDS));
AsyncDelegate dlgt = new AsyncDelegate(qr.QueryResultDS); //qr.QueryResultDS(m_QIndex,m_QKeyword,m_QSTime,m_QETime,
//m_QSort,m_QSortType,m_QFileSize,m_QFileSizeType,m_QMaxCount) //myThread[0].IsBackground = true;
//myThread[0].Start();
IAsyncResult ar = dlgt.BeginInvoke(m_QIndex,m_QKeyword,m_QSTime,m_QETime,m_QSort,m_QSortType,m_QFileSize,m_QFileSizeType,m_QMaxCount,null,dlgt);
myDt = dlgt.EndInvoke(ar);
return(myDt); //
// TODO: 在此处添加构造函数逻辑
//
}
}
}
请大虾帮忙,委托是写好了,但是还是不知道怎么搞多线程。我该怎样才能多线程起qr.QueryResultDS函数呢?该函数有输入输出