csharp进度条-进行过程中访问其他组件 [英] csharp progress bar - accessing another components while progression

查看:224
本文介绍了csharp进度条-进行过程中访问其他组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们,

在最新的c Sharp应用程序中,我使用进度条来演示任务的进度,但是问题是在进度过程中,直到进度条达到最大值,我才能访问其他按钮或关闭类似于任务的窗口.请帮助

谢谢

Hi friends,

In my latest c sharp application, I use a progress bar to demonstrate the progression of a task, but the problem is during the progression I can not access other buttons or close window like tasks until the progress bar reaches the maximum value. plz help

thanks

推荐答案

从您的描述中我怀疑您不使用线程.
没有它们,您的GUI将在整个过程中被锁定,因此,如果您仍然希望GUI做出响应,则必须使用线程.
From the description your giving I suspect you don''t use threads.
Without them your GUI will be locked for the duration of the progress so if you still want your GUI to respond you''ll have to use threading.


从技术上讲,您没有创建一个新线程.相反,您可以调用Application.DoEvents()来强制处理窗口消息(鼠标单击,按键等等).那会是这样的:
Technically, you don''t have to create a new thread. Instead, you can call Application.DoEvents() to force window messages (mouse clicks, key presses, and so on) to be processed. That would go something like this:
// On a form with 2 buttons and a progress bar.
public partial class Form1 : Form
{
	bool closing = false;
	public Form1()
	{
		InitializeComponent();
	}
	private void button1_Click(object sender, EventArgs e)
	{
		for (int i = 0; i < 100; i++)
		{
			if (closing) break;
			progressBar1.Value = i;
			System.Threading.Thread.Sleep(100);
			Application.DoEvents();
		}
		if (!closing) MessageBox.Show("Done!");
	}
	private void button2_Click(object sender, EventArgs e)
	{
		MessageBox.Show("Did it");
	}
	private void Form1_FormClosing(object sender, FormClosingEventArgs e)
	{
		closing = true;
	}
}


但是,我建议您学习如何使用线程.


However, I would recommend you learn how to use threads instead.


Application.DoEvents() EVIL ,因为它会导致重新进入. 不要使用它!

这可能会有所帮助: http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx [ ^ ].

尼克
Application.DoEvents() is EVIL because it causes re-entrancy. Do NOT use it!

This might help: http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx[^].

Nick


这篇关于csharp进度条-进行过程中访问其他组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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