如何从Task到主线程C#的答复结果 [英] How to get reply result from a Task to main thread C#

查看:452
本文介绍了如何从Task到主线程C#的答复结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在使用任务和异步回调来更新数据库,并且我想使用所有数据库连接来平均分配数据库请求,代码工作得很好.但是现在我无法从任务到主线程获取结果,所以给我可能的方法来执行此操作,任务完成后,我还想检查任务是否空闲,然后将该任务用于下一个数据库请求.


i am using task and Asynchronous call back to update the data base and i want to use all database connections to equally distribute the database request, the code is working well somewhat.but now i am unable to get the result from the task to main thread, so give me possible ways to do this operation and as soon as the task is finished, i also want to check task is free and then that task can be use for the next data base request.

public void UpdateDB()
        {
            int  res = -1,y=-1;
            IAsyncResult arr = null;
            SqlDataReader dr;
            while (DBPendingList.Count > 0)
            {
                int x = returnFreeThread();
                if (x == -1)
                    break;

                tsk[x] = new Task(() =>
                {
                    dr= thread_array[x].CallDB();
                    if (dr.HasRows)
                        thread_array[x].isRunning = false;
                });
                thread_array[x].CommandStr = DBPendingList[0];
                DBPendingList.RemoveAt(0);
                thread_array[x].isRunning = true;
                tsk[x].Start();
            }
            ret = new retFreeThreadDeligate(returnFreeThread);
            ret.BeginInvoke(new AsyncCallback(UpdateDBComplete), null);

           
        }

        public void UpdateDBComplete(IAsyncResult ar)
        {
            UpdateDB();
        }

        public int returnFreeThread()
        {

            free_thread_val = -1;

            for (int i = 0; i < maxthread; i++)
            {
                if (thread_array[i].isRunning)
                    continue;

                free_thread_val = i;
                break;
            }
            return free_thread_val;
        }




这是我的数据库连接类...... thread_array是ThreadDB类的数组实例...




this is my Database connection class .....thread_array is an array instance of a ThreadDB class...

public class ThreadDB
    {
        public int ThredId;
        public string CommandStr;
        public bool isRunning;
        public SqlCommand cmd;
        public SqlConnection con;
        public SqlDataReader result;
        public readonly object locker = new object();

        public void Init(string QH_id)
        {
            con = new SqlConnection(Connection String);
            cmd = new SqlCommand();
            cmd.Connection = con;
            con.Open();

        }

        public SqlDataReader CallDB()
        {
           try
            {
                lock (locker)
                {
                    cmd.CommandText = CommandStr;
                    result = cmd.ExecuteNonQuery();

                 }
            }
            catch (SqlException)
            {

            }

            return result;           
        }}

推荐答案

您必须创建一个事件,该事件将带有一个参数(您要从工作线程返回),并在以下位置引发该事件线程执行结束...在主线程中处理它.
you have to create an event, which will take an argument (which you want to return from worker thread), raise the event at the end of thread execution... handle it in main thread.


这篇关于如何从Task到主线程C#的答复结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆