Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all

here is my code .. i want to pause the thread and resume it when i need

here is my code
C#
private void sendbutton_Click(object sender, EventArgs e)
        {
            

            if (listView1.Items.Count == 0)
            {
                MessageBox.Show("Please select Channels");
                return;
            }
            if (listView2.Items.Count == 0)
            {
                MessageBox.Show("Please select whatsapp number");
                return;
            }
            k = "";

            countoperation = 0;
            progressBar2.Value = 1;
            progressBar2.Visible = true;
            progressBar2.Maximum = listView2.Items.Count;
            sendbutton.Enabled = false;
            ThreadPausee.Enabled = true;
            th2 = new Thread(new ThreadStart(sendmessageoperation));
            th2.Start();






        }

        private void sendmessageoperation()
        {

            for (int i = 0; i < listView1.Items.Count; i++)
            {
                if (listView2.Items.Count == 0)
                {
                    break;
                }


                sendwithinvoke(i);
                Thread.Sleep(300);



            }
            Invoke(new Action(() =>
            {
                textBox1.Clear();
                progressBar2.Visible = false;
            }));
            k += "\r\n" + "Total Sent " + countoperation;
            savefile(SentSucssesChannel, true, k);

            if (th2.IsAlive == true)
                th2.Abort();
            
        }
 private void sendwithinvoke(int i)
        {

            if (InvokeRequired)
            {
                this.Invoke(new updatesend(sendwithinvoke), new object[] { i });
                return;
            }


            for (int j = 0; j < Convert.ToInt32(textBox2.Text); j++)
            {
                  //Do something
            }
}

private void ThreadPausee_Click(object sender, EventArgs e)
        {
            try
            {
                if (ThreadPausee.Text == "Pause")
                {
                    if (th2.IsAlive == true)
                    {
                        //Here i want to pause the th2 thread
                        ThreadPausee.Text = "Resume";
                        return;
                    }
                }
                else if (ThreadPausee.Text == "Resume")
                {
                    //Here i want to Resume the th2 thread
                    ThreadPausee.Text = "Pause";
                    return;
                }

            }
            catch (Exception)
            {
                
                throw;
            }
           
           
        }
Posted
Comments
Tomas Takac 11-Nov-14 5:30am    
This sounds wrong. Please could yopu explain why do you want to pause the thread? What are you trying to acomplish?
beljk 11-Nov-14 5:54am    
i need to pause the work and continua it when i need

1 solution

Add a class level bool: call it pauseRequired and set it to false.
Then in your click handler set it to true or false appropriately.
Then change this:
C#
Thread.Sleep(300);

To this:
C#
do
                  Thread.Sleep(300);
               while(pauseRequired);
In theory, you should use a lock on the variable, but in this case it's irrelevant since it's being changed by only one thread, and monitored by the other.

BTW: You would probably be better off using a BackgroundWorker for this as it has progress reporting built in.
 
Share this answer
 
Comments
beljk 11-Nov-14 5:50am    
i don't want sleep .... i want to pause the thread because when sleep in a specific time , i can't to resume it when i need .. only resume when sleep end
OriginalGriff 11-Nov-14 6:05am    
The problem is that what you want to do is not what you think!
Sleep suspends the task for a specified period - in this case 0.3 seconds - which is about the only way of truly "pausing" a task. If you don't Sleep, it runs continuously, so

while (waiting);

would execute machine cycles until the variable "waiting" changed value. That's bad. It uses thread time, it uses processor time, and it wastes power - which can be significant in some case. Many processors "clock themselves down" when they haven't got a lot to do - that loop prevents that.

Instead:

while (waiting) Sleep(300);

Means that the system suspends the task for 0.3 of a second, and then checks if "waiting" has changed. If it hasn't, it suspends for another 0.3 seconds, and so up. When it has, it immediately continues.

That's what my code does - it pauses for 0.3 seconds (just like yours did) and then keeps pausing until the UI task lets it continue.

Try it: you'll see what I mean.

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