C# - 在运行应用程序时暂停线程 [英] C#-pause thread while running application

查看:79
本文介绍了C# - 在运行应用程序时暂停线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了1个应用程序从Excel中读取并在每个文本框中显示这些内容,我使用线程概念等待(从给定输入休眠)每行读取并显示在文本框中。



问题:我想在运行应用程序时暂停线程(在RuntTime期间),当我们点击另一个按钮时,我已经尝试过了,但是焦点不是来找我们。



  //  在文本中显示框。 
私有 void DisplayText()
{
BoomStubAngle:
int BoomStubAngle = 0 ;
for int i = j; i < = ds.Tables [ 0 ]。Rows.Count; i ++)
{
string BSAname = ds.Tables [ 0 ]。行[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 ++)
{
字符串 BFAname = ds.Tables [ 0 ]。行[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 ++)
{
字符串 BFname = ds.Tables [ 0 ]。行[i] [BFHEP] .ToString();
txt_BFHEP1.Text = BFname.ToString();
txt_BFHEP1.Focus();
j ++;
// 线程休眠时间 - 来自用户输入
Thread.Sleep(转换.ToInt32(STIMER));
goto BoomStubAngle;
}
connclose:
connExcel.Close();
}
// ??当线程是运行时,我没有在此按钮上获得焦点运行。??
私有 void button2_Click( object sender,EventArgs e)
{

startstop = false ;

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

}





我尝试过:



  //  要在文本框中显示。 
private void DisplayText()
{
BoomStubAngle:
in t BoomStubAngle = 0 ;
for int i = j; i < = ds.Tables [ 0 ]。Rows.Count; i ++)
{
string BSAname = ds.Tables [ 0 ]。行[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 ++)
{
字符串 BFAname = ds.Tables [ 0 ]。行[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 ++)
{
字符串 BFname = ds.Tables [ 0 ]。行[i] [BFHEP] .ToString();
txt_BFHEP1.Text = BFname.ToString();
txt_BFHEP1.Focus();
j ++;
// 线程休眠时间 - 来自用户输入
Thread.Sleep(转换.ToInt32(STIMER));
goto BoomStubAngle;
}
connclose:
connExcel.Close();
}
// ??当线程是运行时,我没有在此按钮上获得焦点运行。??
私有 void button2_Click( object sender,EventArgs e)
{

startstop = false ;

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

}

解决方案

问题可能是你只有一个线程:你展示的唯一代码包括Thread.Sleep在UI线程上运行(或者你会遇到交叉线程异常时你访问你的文本框) - 所以如果UI线程是睡着了,UI控件不起作用。即使它确实如此,如果在UI线程上调用DisplayText - 它就是 - 那么没有其他UI可以运行,直到它完成。它不做...



该代码非常差:为什么要加载相同的控件并在循环中重复设置焦点?在方法退出之前,用户将不会看到任何更新,一旦启动它就不会。并且转到?为什么呢?

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.

// 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:

// 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();

        }

解决方案

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?


这篇关于C# - 在运行应用程序时暂停线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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