Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created 1 application Reading from Excel and displaying those content in Each text box, I have used threading concept to wait(sleep from Given Input) for each line to read and display in text box.

Ques: I want to Pause the thread while running application(During RuntTime) when we click another button, I have tried from my side, but focus is not coming to us.

C#
// To Display in Text box.
private void DisplayText()
        {
        BoomStubAngle:
            int BoomStubAngle = 0;
            for (int i = j; i <= ds.Tables[0].Rows.Count; i++)
            {
                string BSAname = ds.Tables[0].Rows[i][BSA].ToString();
                txt_BSA.Text = BSAname.ToString();
                txt_BSA.Focus();
            }
        BoomForeAngle:
            int BoomForeAngle = 1;
            for (int i = j; i <= ds.Tables[0].Rows.Count; i++)
            {
                string BFAname = ds.Tables[0].Rows[i][BFA].ToString();
                txt_BFA.Text = BFAname.ToString();
                txt_BFA.Focus();
            }
        BoomForeHeadEndPressure:
            int BoomForeHeadEndPressure = 2;
            for (int i = j; i <= ds.Tables[0].Rows.Count; i++)
            {
               string BFname = ds.Tables[0].Rows[i][BFHEP].ToString();
                txt_BFHEP1.Text = BFname.ToString();
                txt_BFHEP1.Focus();
                j++;
              // Thread sleep Time-From User Input
                Thread.Sleep(Convert.ToInt32(Stimer));
            goto BoomStubAngle;
            }
        connclose:
            connExcel.Close();
        }
//??Iam not getting focus to this button in run time when thread is running.??
 private void button2_Click(object sender, EventArgs e)
        {
           
            startstop = false;

            //do
            //{
            //    Thread.Sleep(1000);
            //}
            //while (startstop == true);
            //DisplayText();

        }


What I have tried:

C#
// To Display in Text box.
private void DisplayText()
        {
        BoomStubAngle:
            int BoomStubAngle = 0;
            for (int i = j; i <= ds.Tables[0].Rows.Count; i++)
            {
                string BSAname = ds.Tables[0].Rows[i][BSA].ToString();
                txt_BSA.Text = BSAname.ToString();
                txt_BSA.Focus();
            }
        BoomForeAngle:
            int BoomForeAngle = 1;
            for (int i = j; i <= ds.Tables[0].Rows.Count; i++)
            {
                string BFAname = ds.Tables[0].Rows[i][BFA].ToString();
                txt_BFA.Text = BFAname.ToString();
                txt_BFA.Focus();
            }
        BoomForeHeadEndPressure:
            int BoomForeHeadEndPressure = 2;
            for (int i = j; i <= ds.Tables[0].Rows.Count; i++)
            {
               string BFname = ds.Tables[0].Rows[i][BFHEP].ToString();
                txt_BFHEP1.Text = BFname.ToString();
                txt_BFHEP1.Focus();
                j++;
              // Thread sleep Time-From User Input
                Thread.Sleep(Convert.ToInt32(Stimer));
            goto BoomStubAngle;
            }
        connclose:
            connExcel.Close();
        }
//??Iam not getting focus to this button in run time when thread is running.??
 private void button2_Click(object sender, EventArgs e)
        {
           
            startstop = false;

            //do
            //{
            //    Thread.Sleep(1000);
            //}
            //while (startstop == true);
            //DisplayText();

        }
Posted
Updated 17-Aug-16 23:41pm
v2
Comments
Mehdi Gholam 18-Aug-16 5:03am    
Reading excel, displaying and threads don't make sense.
vinodh muthusamy 18-Aug-16 5:08am    
Leave Reading Excel concept, I have asked for How to pause the thread, when application is run time.
Mehdi Gholam 18-Aug-16 5:22am    
You use Thread.Sleep().
vinodh muthusamy 18-Aug-16 5:24am    
Iam not able to get focus of button in run time
OriginalGriff 18-Aug-16 5:42am    
In this case, he's using Thread.Sleep on the UI thread in an infinite loop. :sigh:

1 solution

The problem would probably be that you only have one Thread: the only code you show that includes Thread.Sleep is running on the UI thread (or you would get cross threading exceptions when you access your textboxes) - so if the UI thread is asleep, UI controls don't work. Even if it did, if DisplayText is being called on the UI thread - which it is - then no other UI can function utill it's finished. Which it doesn't do...

That code is pretty poor: why are you loading the same control and setting focus to it repeatedly in a loop? The user won't see any updates until the method exits anyway, which once it starts it won't do. And goto? Why?
 
Share this answer
 
Comments
vinodh muthusamy 18-Aug-16 6:11am    
Based On my requirement, I made my looping structure, But all those looping are differs from excel.

Even after removing Goto Immediate, I couldnt able to focus the form
OriginalGriff 18-Aug-16 6:24am    
Read the rest of what I said...
vinodh muthusamy 18-Aug-16 6:38am    
Then What is the solution for 1 paragraph, Which you have mentioned.

Whether we can use Timer Control for this issue.
OriginalGriff 18-Aug-16 7:00am    
Basically, move the processing off the UI thread and onto a background thread to let the UI do it's thing independently. But you need to think pretty carefully about what you are trying to do and what you want to show the user when first.
At the moment, you don't show him anything for long enough to read even if the UI thread could run to display it to him. So work out what you are trying to do from a user perspective and and then start working out how to do it!

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