Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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>


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.

[Edited]Code is wrapped in "pre" tag[/Edited]
Posted
Updated 10-Nov-11 20:49pm
v3
Comments
Sergey Alexandrovich Kryukov 11-Nov-11 2:57am    
So much code, but pointless title (isn't it one of the purposes of method to be called repeatedly if needed?) and no question. Yes, you want this and that, but why you make a problem out of it.
--SA
Member 4354249 11-Nov-11 8:05am    
Hey, i think you haven't got my problem exactly, my problem is not about calling a method repeatedly but my problem is getting reply and control from worker thread to main thread, i.e. the result of "ExecuteNonQuery" should be passed to the main thread as well as i want to call "sendNextRequestFromList()" from Main thread again, since BeginInvoke uses its own thread pool, so i want to eliminate it.

1 solution

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

—SA
 
Share this answer
 
v2
Comments
Member 4354249 17-May-12 7:00am    
it works fine but the problem is main form gets hanged in the middle but wen i right click to close the exe then it resumes...
Sergey Alexandrovich Kryukov 17-May-12 16:37pm    
Use the debugger to see what's going on. If you face a real problem, don't hesitate to ask an advice...
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900