小弟才接触C#,现在有一个写好的查询函数,根据输入的一些条件,返回DATASET查询结果。但是我发现C#的多线程似乎只支持VOID的线程函数,无法进行值传递。 对于值传入已经有一些了解了,但是对于查询的结果传出还是不了解,希望高手解惑。100分,献上。多谢

解决方案 »

  1.   

    定义一个线程函数内外都可以访问的DATASET引用,然后在线程函数里将那个DATASET引用指向你的查询结果,这样在线程函数外就可以通过这个DATASET引用来使用查询结果了
      

  2.   


    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()
    {}
      

  3.   


    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)
    {}
      

  4.   

    谢谢楼上回答,小弟看代码还不是很清楚,"m"是什么?我把上面代码编译了一下,但是提示fun以及myCallback都缺少参数,再请教,多谢
      

  5.   

    报参数放在类里面,使用线程调用类的方法,
    try
    http://blog.csdn.net/zhzuo/archive/2004/06/10/22037.aspx
      

  6.   

    大概就那样了,你在msdn里搜索一下异步using System;
    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);
            
        }
    }
      

  7.   

    public CQueryThread()
    {
    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个参数,请高手解答,多谢
      

  8.   

    using System;
    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函数呢?该函数有输入输出
      

  9.   

    vs 2005有ParameterizedThreadStart 可实现参数的委托方法。你可以传引用参数进去。
      

  10.   

    这里介绍得详细:http://dev.csdn.net/develop/article/29/29874.shtm