在Windows无休止的循环下去窗体应用程序 [英] Looping Forever in a Windows Forms Application

查看:127
本文介绍了在Windows无休止的循环下去窗体应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的Visual C#砂我使用的是Windows窗体,而不是一个控制台应用程序。所以我不是在main()的工作,而是在表单文件。我也很新的C#很抱歉,如果我的一些问题是愚蠢的。



我基本上需要做的是,当我的程序启动我需要它保持循环下去。我会在哪里把这个代码,因为我没有一个main()?难道我把它在它的InitializeComponent()函数?我需要的循环程序启动后启动的权利。但是,我有我需要首先声明循环之前一些变量。所以基本上我需要声明的变量,然后无限循环开始。变量是全球性的。



其次,我需要的无限循环当用户按下一个按钮突破。我会怎么做呢?我在想的线中的内容:

 而(buttonIsPressed ==假)
{
/ /做我需要做的
}

不过,我实现了按钮的功能永远叫因为我陷在这个循环。我无法设置从按钮的功能变量,如果我永远达不到按钮的功能,由于在一个无限循环之中。有任何想法吗?我在想的线程,但我有一个线程完全没有经验,所以我有点不愿意尝试一下。






其他从评论:




一个聊天应用程序。当程序启动时,我需要它来保持
聆听。但是,当用户点击连接停止听
,而是启动



我创建一个聊天客户端的连接。所以基本上,当我的程序启动时,我
需要它来继续听。但是,当用户点击连接,它
需要停止听,而是发起连接



解决方案

这是人谁是从(完全)不同背景的一个自然的问题。



编程Windows窗体应用程序是事件驱动的。当Windows窗体应用程序启动时,加载窗体(检查的Program.cs文件),而且是隐藏你专注于你的程序的重要的主循环。



您不需要放任何东西在主回路中对事件(如按钮点击事件)作出回应。你只需通过创建按钮的Click事件)的事件处理程序处理按钮点击事件。您可以使用设计器或手动执行该代码。



当您创建来处理事件的方法(和这样的方法被调用的事件处理程序),它当引发该事件被自动调用/触发。例如,当用户点击该按钮,为您的窗体上按钮的Click事件的方法处理程序将被称为



这MSDN主题包含所有你需要的信息: 创建事件处理程序在Windows窗体。如果你需要进一步澄清,请咨询! :)



更新:创建一个事件处理程序,就像上面,也创造了Form_Loaded事件处理循环。一定要打电话的 Application.DoEvents(); 的循环。这种方式对于按钮单击事件处理程序可以处理(在处理程序添加代码来修改一个布尔值,会使循环的条件为假,取消循环)。



更新2:
为了使这一答案更完整,我提到,你也应该考虑一个新的线程,而不是UI一个正在运行的循环(从而避免需要使用的DoEvents,这有其负面的我的同龄人指出的)。下面的例子说明如何创建一个线程,并取消点击一个按钮:

  System.Threading.Thread吨; 
私人无效Form1_Load的(对象发件人,EventArgs五)
{
//创建并启动在负载情况下一个新的线程。
//传递一个方法,要在新的线程上运行。
T =新System.Threading.Thread(DoThisAllTheTime);
t.Start();
}

公共无效DoThisAllTheTime()
{
,而(真)
{
//你需要使用调用,因为新线程不能访问UI元素直接
MethodInvoker MI =委托(){this.Text = DateTime.Now.ToString(); };
this.Invoke(MI);
}
}

私人无效的button1_Click(对象发件人,EventArgs五)
{
//停止线程。
t.Suspend();
}



最后,可以考虑使用的 BackgroundWorker的它封装创建和管理线程你。


I am using Visual C# sand I'm using a Windows Form rather than a console application. Therefore I'm not working in Main (), but rather in the Form File. I'm also very new to C# so sorry if some of my questions are stupid.

What I basically need to do is when my program starts up I need it to keep looping forever. Where would I put this code since I don't have a Main ()? Do I put it in the function that has InitializeComponent() in it? I need the loop to start right after the program starts. However, I have some variables that I need declared first before the loop. So basically I need the variables to be declared and then the infinite loop to start. The variables are global.

Secondly, I need the infinite loop to break when the user presses a button. How would I do this? I was thinking something among the lines of:

while (buttonIsPressed == false)
{
   //do whatever I need to do
}

However, I realized the button's function will never be called since I am stuck in that loop. I can't set the variable from the button's function if I never reach the button's function due to being in an infinite loop. Any ideas? I was thinking about threads but I have absolutely no experience with threads so am a bit reluctant to try it out.


Additional, from comments:

A chat application. When the program starts I need it to keep listening. However, when the user clicks "connect" it stops listening and instead initiates a connection

I am creating a chat client. So basically when my program starts up, I need it to keep listening. However, when the user clicks "connect" it needs to stop listening and instead initiate a connection

解决方案

That's a natural question for someone who's coming from a (completely) different background.

Programming Windows Forms applications is event driven. When a Windows Forms application starts, a form is loaded (check Program.cs file) and there is a Main loop that is hidden from you to concentrate on the important things in your program.

You don't need to put anything in the Main loop to respond to an event (like a button clicked event). You just handle the button clicked event by creating an event handler for the Click event of the Button). You can use the designer or do that manually in the code.

When you create a method to handle an event (and as such the method is called an event handler) it is called automatically when the event is raised/triggered. For example a method handler for the Click event for the Button on your form would be called when the user clicked the button.

This MSDN topic contains all the information you need: Creating Event Handlers in Windows Forms. If you need more clarification, please ask! :)

UPDATE: Create an event handler just like above and also create a loop in the Form_Loaded event handler. Be sure to call Application.DoEvents(); inside the loop. This way the event handler for the button click can be handled (in the handler add code to modify a boolean that would make the loop's condition false to cancel the loop).

UPDATE 2: To make this answer more complete, I mention that you should also consider running the loop on a new thread, not the UI one (and therefore avoid needing to use DoEvents, which has its negatives as my peers pointed out). The following example shows how to create a Thread and cancel it on a Button Click:

    System.Threading.Thread t;
    private void Form1_Load(object sender, EventArgs e)
    {
        //create and start a new thread in the load event.
        //passing it a method to be run on the new thread.
        t = new System.Threading.Thread(DoThisAllTheTime);
        t.Start();
    }

    public void DoThisAllTheTime()
    {
        while (true)
        {
            //you need to use Invoke because the new thread can't access the UI elements directly
            MethodInvoker mi = delegate() { this.Text = DateTime.Now.ToString(); };
            this.Invoke(mi);
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //stop the thread.
        t.Suspend();
    } 

Finally, consider using a BackgroundWorker which encapsulates creating and managing Threads for you.

这篇关于在Windows无休止的循环下去窗体应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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