如何一次又一次地在主线程中调用函数. [英] How to call a function in main thread again and again.

查看:89
本文介绍了如何一次又一次地在主线程中调用函数.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
请检查我的代码,

Hi guys,
please check my code,

private void ButtonStartUp_Click(object sender, EventArgs e)
        {          
                        
            for (int i = 0; i < maxthread; i++)
            {
                QHThreadArray[i] = new TQHThread();
                QHThreadArray[i].ThreadNo = i;
                QHThreadArray[i].QH_id = QH_id;
                QHThreadArray[i].Start();
            }  
        }

private void Button_UpdateOS_Click(object sender, EventArgs e)
        {

           
                fileName = "osdays.csv";
                if (!File.Exists(fileName))
                {
                    MessageBox.Show(fileName + "File Does Not Exist!");
                }
                else if (File.Exists(fileName))
                {
                    //some code which reads contents from file and adds to a  List<string> named DBPendingList in a Query format which can be directly given //to data base for execution;

                    sendNextRequestFromList();
                }   
        }


 public static void sendNextRequestFromList()
        {
            while (DBPendingList.Count > 0)
            {
                int ThNum = -1;
                QH = null; //Where QH is an instance of TQHThread class
                for (int i = 0; i < maxthread; i++)
                {
                    if (QHThreadArray[i].Status == "Running")
                    {
                        continue;
                    }
                    if (QHThreadArray[i].Status == "Suspended")
                    {
                        QH = QHThreadArray[i];
                        ThNum = i;
                        break;
                    }
                }

                if (ThNum == -1)
                    break; // exit; used in Delphi code.

                if (QH != null)
                {
                    QH.RequestStr = DBPendingList[0];
                    DBPendingList.RemoveAt(0);
                    QH.Resume();
                }
            }

//Following "if" code is required to be eliminated, because the reason is given //below.
            if (DBPendingList.Count != 0)
            {
                ret = new retFreeThreadDelegate(sendNextRequestFromList);
                ret.BeginInvoke(null, null);
            }
        }

  public class TQHThread
    {
        public int ThreadNo;
        public string QH_id;
        public string RequestStr=null;
        public SqlCommand cmd;
        public SqlConnection con;
        public Thread t;
        public readonly object locker = new object();
        public int result = -1;
                       
        public void Start()
        {
            
            t = new Thread(new ThreadStart(this.Execute));
            t.Start();
        }

        public void Resume()
        {
            if (Status == "Suspended")
                t.Resume();
        }

        public void Suspend()
        {
            if (Status == "Running")
            {
               t.Suspend();
            }
        }

        public string Status
        {
            get
            {
                return t.ThreadState.ToString();
            }
        }

        public void Execute()
        {
            con = new SqlConnection(File.ReadAllText("Connection" + QH_id + ".udl"));
            cmd = new SqlCommand();
            cmd.Connection = con;
            con.Open();
            
            if (RequestStr == null)
            {
                Suspend();
            }

            while (this.Status != "Suspended")
            {
                try
                {
                    cmd.CommandText = RequestStr;
                    result=cmd.ExecuteNonQuery();                    
                }

                catch (SqlException)
                {

                }
                catch (InvalidOperationException)
                {

                }
                frmQuoteHandler.UpdateDBComplete(this);
            }            
        }
    }
we want to return result to the main thread in UpdateDBComplete().


 public static void UpdateDBComplete(TQHThread QH)
        {
            lock (locker)
            {
                if (QH != null)
                {
                    int x = QH.result;
                    QH.Suspend();
                }
            }
        }</string>



如果代码在无法得到结果后还是不能进行任何工作的情况下仍能正常工作,则我想在MAIN线程中再次调用"sendNextRequestFromList()",即我无法执行,因此需要您的建议. >
由于使用了BeginInvoke,因此暂时无法使用,但在逻辑上不正确,也不是我想要的方式.

并告诉我在获得结果或特定线程空闲时可以在MAIN线程中调用"sendNextRequestFromList()"的可能方法.

等待很快的答复,并在此先感谢.

代码包装在"pre"标签中.[/编辑]



in case code is working bit properly BUT AFTER GETTING RESULT OUT OF THREAD OR WHENEVER A THREAD IS FREE TO WORK, I want to call "sendNextRequestFromList()" again in the MAIN thread, i.e. I am unable to perform so need your suggestion.

For time being its working because of the BeginInvoke is used but its not the logically correct and the way I want.

and just tell me the possible ways that i can call "sendNextRequestFromList()" in the MAIN thread whenever result obtained or whenever a particular thread is free.

waiting for soon reply, and Thanks in advance.

Code is wrapped in "pre" tag[/Edited]

推荐答案

已批准.您可能在同一线程上多次调用一个方法.不用担心说谢谢",没关系. :-)

—SA
Approved. You may call a method more than once on the same thread. Don''t bother about saying "thank you", it''s OK. :-)

—SA


这篇关于如何一次又一次地在主线程中调用函数.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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