多线程和进度条帮助 [英] Multithreading and progress bar help

查看:93
本文介绍了多线程和进度条帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的专家,我想开发一个使用google

搜索网络服务的客户端应用程序。

在我的MainForm中我有一个检索所有搜索结果的方法例如

GetGoogleResults()。

现在我希望在另一种形式中有一个很好的进度条,例如ProgressForm来执行搜索操作。

。 br />
i尝试这样做,但进度条不能工作,GUI冻结。

我知道如何处理单线程但我不知道如何在

多线程中做到这一点。

谁能给我一个如何做到这一点的例子。

谢谢

Dear experts, i want to develop an client application that consume the google
search web service.
In my MainForm i have a method to retrieve all the search result e.g.
GetGoogleResults().
Now i want to have a nice progress bar in another form e.g ProgressForm to
perform the search action.
i tried to do it but the progress bar won''t work and the GUI freezed.
I knew how to do with the single threading but i don''t know how to do it in
multiple threading.
Could anyone give me an example of how to do that.
Thank you

推荐答案

Popoxinhxan< Po ********* @ discussion.microsoft.com>写道:
Popoxinhxan <Po*********@discussions.microsoft.com> wrote:
亲爱的专家,我想开发一个使用google
搜索网络服务的客户端应用程序。
在我的MainForm中我有一个方法来检索所有的搜索结果,例如
GetGoogleResults()。
现在我希望在另一个表单中有一个很好的进度条,例如ProgressForm来执行搜索操作。
我试图这样做,但进度条赢了工作和GUI冻结。
我知道如何处理单线程但我不知道如何在多线程中执行它。
任何人都可以给我一个如何做到这一点的例子。
Dear experts, i want to develop an client application that consume the google
search web service.
In my MainForm i have a method to retrieve all the search result e.g.
GetGoogleResults().
Now i want to have a nice progress bar in another form e.g ProgressForm to
perform the search action.
i tried to do it but the progress bar won''t work and the GUI freezed.
I knew how to do with the single threading but i don''t know how to do it in
multiple threading.
Could anyone give me an example of how to do that.




http://www.pobox.com/~skeet/csharp/t...winforms.shtml


- -

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复对于小组,请不要给我发邮件



See http://www.pobox.com/~skeet/csharp/t...winforms.shtml

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这是我的程序,但我仍然不知道如何让进度条工作,

请帮帮我。该程序只需从

罢工铁业务网络下载网络服务列表

谢谢


--- MainForm.cs -------------------------------------------------- ---------------------------

使用System;

使用System.Drawing ;

使用System.Collections;

使用System.ComponentModel;

使用System.Windows.Forms;

使用System.Data;

使用System.Threading;


命名空间TestProgressBar

{

/ //< summary>

/// Form1的摘要说明。

///< / summary>

公共类MainForm: System.Windows.Forms.Form

{

private System.Windows.Forms.Button GetWebServices;

private System.Windows.Forms.Timer timer1;

private System.ComponentModel.IContainer组件;

private const string licenseKey =" ADBBB86C6FAA29E47337";

com.strikeiron。 www.WebServiceInfo [] wsList;

ProgressForm prg = new ProgressForm();


public MainForm()

{

//

// Windows表单设计器支持需要

//

InitializeComponent();


//

// TODO:添加任何构造函数代码在InitializeComponent调用之后

//

}


///< summary>

/ //清理正在使用的所有资源。

///< / summary>

protected override void Dispose(bool disposing)

{

if(处理)

{

if(components!= null)

{

components.Dispose();

}

}

base.Dispose(disposing);

}


#region Windows窗体设计器生成的代码

///< summary>

/// Designer的必需方法支持 - 不要修改

///使用代码编辑器的方法的内容。

///< / summary>

private void InitializeComponent()

{

this.components = new System.ComponentModel.Container();

this.GetWebServices = new System.Windows.Forms.Button();

this.timer1 = new System.Windows.Forms.Timer(this.components);

this.SuspendLayout();

//

// GetWebServices

//

this.GetWebServices.Location = new System.Drawing.Point(96,24);

this.GetWebServices.Name =" GetWebServices";

this.GetWebServices.Size = new System.Drawing.Size(120,23);

this.GetWebServices.TabIndex = 0;

this.GetWebServices.Text =" Get Web Services";

this.GetWebServices.Click + = new

系统。 EventHandler(this.GetWebServices_Click);

//

// timer1

//

this.timer1。 Tick + = new System.EventHandler(this.timer1_Tick);
//

// MainForm

//

this.AutoScaleBaseSize = new System.Drawing.Size(5 ,13);

this.ClientSize = new System.Drawing.Size(320,86);

this.Controls.Add(this.GetWebServices);

this.Name =" MainForm";

this.Text =" Form1";

this.ResumeLayout(false);


}

#endregion


///< summary>

///申请的主要入口点。

///< / summary>

[STAThread]

static void Main()

{

Application.Run(new MainForm());

}


private void GetWebServices_Click(object发件人,System.EventArgs e)

{

//显示进度表格

prg.Show();

线程t1 =新线程(新的ThreadStart(GetWebServiceListHandler));

t1.Start();

}


/ /这将获得罢工铁网服务业务列表etwork

private com.strikeiron。 www.WebServiceInfo [] GetWebServiceList()

{

com.strikeiron。 www.WebServiceInfo [] wsList;

com.strikeiron。 www.StrikeIronDirectoryService myService ;

com.strikeiron。 www.LicStatus liSta;

com.strikeiron。 www.RespStatus resSta;

myService = new com.strikeiron。 www.StrikeIronDirectoryService();

liSta = new com.strikeiron。 www.LicStatus();

resSta = new com.strikeiron。 www.RespStatus();


//获取列表

wsList = myService.GetAllPublicServices(licenseKey,out resSta,out liSta);


返回wsList;


}


private void GetWebServiceListHandler( )

{

//开始时间

timer1.Start();

this.wsList = GetWebServiceList ();

//停止时间

timer1.Stop();

prg.UpdateLabel(wsList.Length +" web services已找到);

}


//更新进度

private void timer1_Tick(对象发送者,System.EventArgs) e)

{

prg.UpdateProgress();

}



}

}

--------- ProgressForm.cs -------------------- ------------


使用System;

使用System.Drawing;

使用System .Collections;

使用System.ComponentModel;

使用System.Windows.Forms;

名称空间TestProgressBar

{

///< summary>

/// Form2的摘要说明。< br $>
///< / summary>

公共类ProgressForm:System.Windows.Forms.Form

{

private System.Windows.Forms.ProgressBar progressBar1;

private System.Windows.Forms.Label label1;

///< summary>

///所需的设计变量。

///< / summary>

private System.ComponentModel.Container components = null;


public ProgressForm()

{

//

// Windows表单设计器支持需要

//

InitializeComponent();


//

// TODO:在InitializeComponent调用后添加任何构造函数代码< br $>
//

InitProgressBar();

}


///< summary>

///清理正在使用的所有资源。

///< / summary>

protected override void Dispose(bool disposing)

{

if(disposing)

{

if(components!= null)

{

components.Dispose();

}

}

base.Dispose(disposing);

}


#region Windows窗体设计器生成的代码

///< summary> ;

/// Designer支持所需的方法 - 不要使用代码编辑器修改

///此方法的内容。

///< / summary>

private void InitializeComponent()

{

this.progressBar1 = new System.Windows.Forms.ProgressBar ();

this.label1 = new System.Windows.Forms.Label();

this.SuspendLayout();

//

// progressBar1

//

this.progressBar1.Location = new System.Drawing.Point(24,40);

this.progressBar1.Name =" progressBar1";

this.progressBar1.Size = new System.Drawing.Size(240,23);

this.progressBar1.TabIndex = 0;

//

// label1

//

this.label1.Location = new System.Drawing.Point(24,16);

this.label1.Name =" label1";

this.label1.Size = new System.Drawing.Size(240,23);

this.label1.TabIndex = 1;

this.label1.Text =" Please wait;; < br $>
//

// ProgressForm

//

this.AutoScaleBaseSize = new System.Drawing.Size(5 ,13);

this.ClientSize = new System.Drawing.Size(296,86);

this.Controls.Add(this.label1);

this.Controls.Add(this.progressBar1);

this.Name =" ProgressForm";

this.Text =" Form2" ;;

this.ResumeLayout(false);


}

#endregion


public void UpdateProgress()

{

if(progressBar1.Value> 0&& progressBar1.Value< 100)

progressBar1.Value ++;

}


private void InitProgressBar()

{

progressBar1.Minimum = 1;

progressBar1.Maximum = 100;

}


public void UpdateLabel(string value)

{

label1.Text = value;

}

}


}


---------- StrikeIron Web服务--------
http://www.strikeiron.com/webservice...vice。 asmx?wsdl

Ignacio Machin(.NET / C#MVP)"写道:
Here is my program but i still don''t know how to make the progress bar work,
please help me. The program simply download the list of web services from
strike iron business network
Thank you

---MainForm.cs-----------------------------------------------------------------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;

namespace TestProgressBar
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class MainForm: System.Windows.Forms.Form
{
private System.Windows.Forms.Button GetWebServices;
private System.Windows.Forms.Timer timer1;
private System.ComponentModel.IContainer components;
private const string licenseKey = "ADBBB86C6FAA29E47337";
com.strikeiron.www.WebServiceInfo [] wsList;
ProgressForm prg = new ProgressForm();

public MainForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.GetWebServices = new System.Windows.Forms.Button();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//
// GetWebServices
//
this.GetWebServices.Location = new System.Drawing.Point(96, 24);
this.GetWebServices.Name = "GetWebServices";
this.GetWebServices.Size = new System.Drawing.Size(120, 23);
this.GetWebServices.TabIndex = 0;
this.GetWebServices.Text = "Get Web Services";
this.GetWebServices.Click += new
System.EventHandler(this.GetWebServices_Click);
//
// timer1
//
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(320, 86);
this.Controls.Add(this.GetWebServices);
this.Name = "MainForm";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new MainForm());
}

private void GetWebServices_Click(object sender, System.EventArgs e)
{
//Show the progressform
prg.Show();
Thread t1 = new Thread(new ThreadStart(GetWebServiceListHandler));
t1.Start();
}

//This will get the list of strike iron web service business network
private com.strikeiron.www.WebServiceInfo [] GetWebServiceList()
{
com.strikeiron.www.WebServiceInfo [] wsList;
com.strikeiron.www.StrikeIronDirectoryService myService;
com.strikeiron.www.LicStatus liSta;
com.strikeiron.www.RespStatus resSta;
myService = new com.strikeiron.www.StrikeIronDirectoryService();
liSta = new com.strikeiron.www.LicStatus();
resSta = new com.strikeiron.www.RespStatus();

//Get the list
wsList = myService.GetAllPublicServices(licenseKey,out resSta,out liSta);

return wsList;

}

private void GetWebServiceListHandler()
{
//start the time
timer1.Start();
this.wsList = GetWebServiceList();
//stop the time
timer1.Stop();
prg.UpdateLabel(wsList.Length + "web services have been found");
}

//Update the progress
private void timer1_Tick(object sender, System.EventArgs e)
{
prg.UpdateProgress();
}


}
}
---------ProgressForm.cs--------------------------------

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace TestProgressBar
{
/// <summary>
/// Summary description for Form2.
/// </summary>
public class ProgressForm : System.Windows.Forms.Form
{
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.Label label1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public ProgressForm ()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
InitProgressBar();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(24, 40);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(240, 23);
this.progressBar1.TabIndex = 0;
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(240, 23);
this.label1.TabIndex = 1;
this.label1.Text = "Please wait";
//
// ProgressForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(296, 86);
this.Controls.Add(this.label1);
this.Controls.Add(this.progressBar1);
this.Name = "ProgressForm";
this.Text = "Form2";
this.ResumeLayout(false);

}
#endregion

public void UpdateProgress()
{
if(progressBar1.Value>0 && progressBar1.Value<100)
progressBar1.Value++;
}

private void InitProgressBar()
{
progressBar1.Minimum = 1;
progressBar1.Maximum = 100;
}

public void UpdateLabel(string value)
{
label1.Text = value;
}
}

}

----------StrikeIron Web services--------
http://www.strikeiron.com/webservice...vice.asmx?wsdl
"Ignacio Machin ( .NET/ C# MVP )" wrote:


看看档案,我发布了一些代码,就像两周前一样。
>
搜索ProgressBar ThreadIgnacio Machin之类的东西,别担心。
我为你做了这个:):

http://groups.google.com/groups?q=Pr...phx.gbl&rnum=2

欢呼,

- Ignacio Machin,
ignacio.machin at dot.state.fl.us
佛罗里达州交通

Popoxinhxan <宝********* @ discussions.microsoft.com>在消息中写道
新闻:DB ********************************** @ microsof t.com。 ..
Hi,

Take a look at the archives, I posted some code to do this like two weeks
ago.

Search for something like ProgressBar Thread "Ignacio Machin", dont worry,.
I did that for you :) :

http://groups.google.com/groups?q=Pr...phx.gbl&rnum=2

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Popoxinhxan" <Po*********@discussions.microsoft.com> wrote in message
news:DB**********************************@microsof t.com...
亲爱的专家,我想开发一个使用
Dear experts, i want to develop an client application that consume the


google


google

搜索网络服务的客户端应用程序。
在我的MainForm我有一个检索所有搜索结果的方法,例如GetGoogleResults()。
现在我希望在另一个表单中有一个很好的进度条,例如ProgressForm来执行搜索操作。 />我试图这样做,但进度条不能工作,GUI冻结。
我知道如何处理单线程但我不知道该怎么做
search web service.
In my MainForm i have a method to retrieve all the search result e.g.
GetGoogleResults().
Now i want to have a nice progress bar in another form e.g ProgressForm to
perform the search action.
i tried to do it but the progress bar won''t work and the GUI freezed.
I knew how to do with the single threading but i don''t know how to do it


多线程。
任何人都可以举例说明如何做到这一点。
谢谢
multiple threading.
Could anyone give me an example of how to do that.
Thank you




Popoxinhxan< Po ********* @ discussion.microsoft.com>写道:
Popoxinhxan <Po*********@discussions.microsoft.com> wrote:
这是我的程序,但我仍然不知道如何使进度条工作,
请帮助我。该程序只需从
罢工铁业务网络下载网络服务列表
Here is my program but i still don''t know how to make the progress bar work,
please help me. The program simply download the list of web services from
strike iron business network




您还没有使用Control.Invoke或BeginInvoke。您是否阅读了我指出的

页面,
http://www.pobox.com/~skeet/csharp/t...winforms.shtml





-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复该群组,请不要也寄给我



You''re still not using Control.Invoke or BeginInvoke. Did you read the
page I pointed you at,
http://www.pobox.com/~skeet/csharp/t...winforms.shtml

?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这篇关于多线程和进度条帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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