代表和线程 [英] Delegates and threads

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

问题描述

我很难理解这个以及我发现的任何代码样本

在线长达数公里且很难理解......请可以

有人给我一个简单的如何让代表传递给我的工作

类,以便我的主表单可以更新其进度条?我知道它有一些与bn / b
有关的begininvoke和endinvoke等等,但我只需要一个简单的

样本来理解约定。

代码从我的主要表格:

-----------------------------------

form1Delegate dlgate = new form1Delegate(IncreaseProgressBar);

//thing.countUpToaMillionOrSomething(new

form1Delegate(this.IncreaseProgressBar));

Thread thread = new Thread(new

ThreadStart(thing.countUpToaMillionOrSomething(dlg ate)));

thread.Start();

-----------------------------------


来自我的工人类的代码叫''thing''

------------------------------- ----

public static void countUpToaMillionOrSomething(Form1.form1Delegate

myMethodDelegate)

{

int x = 0;

而(x <50)

{

Thread.Sleep(70);

x + +;

myMethodDelegate();

}

}

-----------------------------------

我只能让它与以下行同步工作:


thing.countUpToaMillionOrSomething(new

form1Delegate(this.IncreaseProgressBar));


如果我尝试使用上面的线程示例进行编译,它会发出''方法名称

expected''并强调下面的代码:


thing.countUpToaMillionOrSomething(dlgate)


任何帮助非常感谢,

谢谢,

格兰特

I am having great difficulty understanding this and any code samples I find
online are kilometres long and complicated to understand...Please could
someone give me a simple exampe of how to get a delegate passed to my worked
class so that my main form can update its progress bar? I know it has
something to do with begininvoke and endinvoke etc, but I just need a simple
sample to understand the convention.
Code from my main form:
-----------------------------------
form1Delegate dlgate = new form1Delegate(IncreaseProgressBar);
//thing.countUpToaMillionOrSomething(new
form1Delegate(this.IncreaseProgressBar));
Thread thread = new Thread(new
ThreadStart(thing.countUpToaMillionOrSomething(dlg ate)));
thread.Start();
-----------------------------------

Code from my worker class called ''thing''
-----------------------------------
public static void countUpToaMillionOrSomething(Form1.form1Delegate
myMethodDelegate)
{
int x = 0;
while ( x < 50)
{
Thread.Sleep(70);
x ++ ;
myMethodDelegate();
}
}
-----------------------------------
I can only get it to work synchronously with the following line:

thing.countUpToaMillionOrSomething(new
form1Delegate(this.IncreaseProgressBar));

If I try compile with the thread example above it spews about ''Method name
expected'' and underlines the following bit of code:

thing.countUpToaMillionOrSomething(dlgate)

Any help greatly appreciated,
Thanks,
Grant

推荐答案

您不需要使用Thread类异步调用委托,但是,使用BeginInvoke调用它将执行

委托在ThreadPool线程上指向的方法。此外,ProgressBar(和任何其他WinForms控件)需要大部分

其成员在创建控件的线程上执行/访问。为了满足这个要求,一个适当的

例子不能太简单。


这里是最简单的同步更新进度条的方法在WinForms应用程序(正确的方式)。我假设每个

方法都包含在派生自System.Windows.Forms.Form的类中:


private delegate void UpdateProgressInvoker(int Position );

private System.Windows.Forms.ProgressBar progressBar;


///< summary>测试进度条异步更新代码< / summary>

public void DoSomeWork()

{

progressBar.Minimum = 0;

progressBar.Maximum = 100;


for(int i = 0; i< 10000; i ++)

{

//创建一个指向UpdateProgress方法

UpdateProgressInvoker del = new UpdateProgressInvoker(UpdateProgress);


//异步调用方法

del.BeginInvoke(( int)(i / 100),null,null);


//告诉应用程序处理挂起的消息

//(所以表单没有' '冻结,进度条直观地更新)

Application.DoEvents();

}

}

///< summary>

///此方法不直接更新进度条。相反,它确保

///< see cref =" UpdateProgressCore" />方法(实际完成工作)在创建ProgressBar的

///线程上执行。

///< / summary>

private void UpdateProgress(int Position)

{

//检查Fo​​rm.InvokeRequired属性是否为true。这将表明

//当前正在执行的线程不是创建ProgressBar的线程。

//要同步更新进度条,我们需要调用

//主线程上的方法。

// [this]必须是对Form对象的引用。

if( !this.InvokeRequired)

{

//创建指向执行实际更新的UpdateProgressCore方法的指针

UpdateProgressInvoker del = new UpdateProgressInvoker( UpdateProgressCore);


//在应用程序的主线程上异步调用方法。

//通过使用Form.Invoke,我们确保进度bar会正确更新。

this.Invoke(del,new object [] {Position});

}

else

//当前正在执行的线程是ProgressBar的主题,

//所以通常更新进度条

UpdateProgressCore(Position);

}


///< summary>

///不要直接调用此方法。相反,请调用< see cref =" UpdateProgress" /> ;.

///< / summary>

private void UpdateProgressCore(int Position)

{

progressBar.Value =位置;

}

-

Dave Sexton
dave @ www..jwaonline..com

-------------------- -------------------------------------------------- -

" Grant" < GP ***** @ hotmail.com>在消息新闻中写道:Oo ************** @tk2msftngp13.phx.gbl ...
You don''t need to use the Thread class to invoke the delegate asynchronously, however, invoking it using BeginInvoke will execute
the method that the delegate points to on a ThreadPool thread. Also, ProgressBar (and any other WinForms control) requires most of
its members to be executed/accessed on the thread that the control was created on. To account for this requirement, a proper
example can''t be too simple.

Here''s the simplest approach to updating a progress bar aynchronously in a WinForms app (the proper way). I''m assuming that each
method is contained by a class derived from System.Windows.Forms.Form:

private delegate void UpdateProgressInvoker(int Position);
private System.Windows.Forms.ProgressBar progressBar;

/// <summary>Test the progress bar asynchronous update code</summary>
public void DoSomeWork()
{
progressBar.Minimum = 0;
progressBar.Maximum = 100;

for (int i = 0; i < 10000; i++)
{
// Create a pointer to the UpdateProgress method
UpdateProgressInvoker del = new UpdateProgressInvoker(UpdateProgress);

// Invoke the method asynchronously
del.BeginInvoke((int) (i / 100), null, null);

// Tell the app to handle pending messages
// (so the form doesn''t freeze and the progress bar is updated visually)
Application.DoEvents();
}
}
/// <summary>
/// This method does not update the progress bar directly. Instead, it ensures that the
/// <see cref="UpdateProgressCore" /> method (which actually does the work) is executed on the
/// thread that the ProgressBar was created on.
/// </summary>
private void UpdateProgress(int Position)
{
// Check if the Form.InvokeRequired property is true. This will indicate that
// the currently executing thread is not the thread that the ProgressBar was created on.
// To update the progress bar synchronously, we will need to invoke the method on the
// main thread.
// [this] must be a reference to a Form object.
if (!this.InvokeRequired)
{
// Create a pointer to the UpdateProgressCore method which performs the actual update
UpdateProgressInvoker del = new UpdateProgressInvoker(UpdateProgressCore);

// Invoke the method asynchronously on the main thread of the application.
// By using Form.Invoke, we ensure that our progress bar will be updated properly.
this.Invoke(del, new object[] { Position });
}
else
// Currently executing thread is the ProgressBar''s thread,
// so update the progress bar normally
UpdateProgressCore(Position);
}

/// <summary>
/// Do not call this method directly. Instead, call <see cref="UpdateProgress" />.
/// </summary>
private void UpdateProgressCore(int Position)
{
progressBar.Value = Position;
}
--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
"Grant" <gp*****@hotmail.com> wrote in message news:Oo**************@tk2msftngp13.phx.gbl...
我很难理解这个以及我发现的任何代码示例在线长达数公里且很难理解......请有人能给我一个简单的例子,说明如何让代表传递给我的工作班,以便我的主表格可以更新其进度条吗?我知道它与begininvoke和endinvoke等有关,但我只需要一个简单的样本来理解约定。

来自我主表的代码:
--- --------------------------------
form1Delegate dlgate = new form1Delegate(IncreaseProgressBar);
/ /thing.countUpToaMillionOrSomething(new form1Delegate(this.IncreaseProgressBar));
线程thread = new Thread(new ThreadStart(thing.countUpToaMillionOrSomething(dlg ate)));
thread.Start();
-----------------------------------

来自我的工人类的代码叫''东西''
-----------------------------------
公共静态void countUpToaMillionOrSomething(Form1.form1Delegate myMethodDelegate)
{x / 0}
while(x <50)
{
Thread.Sleep(70);
x ++;
myMethodDelegate();
}
}
--------------------- --------------

我只能让它与以下行同步工作:

thing.countUpToaMillionOrSomething(new form1Delegate(this.IncreaseProgressBar));

如果我尝试使用上面的线程示例进行编译,它会发出''Method name expected''并强调下面的代码:

thing.countUpToaMillionOrSomething(dlgate)

任何帮助非常感谢,
谢谢,
格兰特
I am having great difficulty understanding this and any code samples I find online are kilometres long and complicated to
understand...Please could someone give me a simple exampe of how to get a delegate passed to my worked class so that my main form
can update its progress bar? I know it has something to do with begininvoke and endinvoke etc, but I just need a simple sample to
understand the convention.
Code from my main form:
-----------------------------------
form1Delegate dlgate = new form1Delegate(IncreaseProgressBar);
//thing.countUpToaMillionOrSomething(new form1Delegate(this.IncreaseProgressBar));
Thread thread = new Thread(new ThreadStart(thing.countUpToaMillionOrSomething(dlg ate)));
thread.Start();
-----------------------------------

Code from my worker class called ''thing''
-----------------------------------
public static void countUpToaMillionOrSomething(Form1.form1Delegate myMethodDelegate)
{
int x = 0;
while ( x < 50)
{
Thread.Sleep(70);
x ++ ;
myMethodDelegate();
}
}
-----------------------------------
I can only get it to work synchronously with the following line:

thing.countUpToaMillionOrSomething(new form1Delegate(this.IncreaseProgressBar));

If I try compile with the thread example above it spews about ''Method name expected'' and underlines the following bit of code:

thing.countUpToaMillionOrSomething(dlgate)

Any help greatly appreciated,
Thanks,
Grant



看起来很棒 - 除了我不明白为什么你打电话给DoEvents - 这有什么帮助?


问候


Richard Blewett - DevelopMentor
http:// www。 dotnetconsult.co.uk/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/< #m ******* *******@TK2MSFTNGP12.phx.gbl>


您不需要使用Thread类异步调用委托,但是,使用BeginInvoke调用它将执行

委托指向ThreadPool线程的方法。此外,ProgressBar(和任何其他WinForms控件)需要大部分

其成员在创建控件的线程上执行/访问。为了满足这个要求,一个适当的

例子不能太简单。


这里是最简单的同步更新进度条的方法在WinForms应用程序(正确的方式)。我假设每个

方法都包含在派生自System.Windows.Forms.Form的类中:


private delegate void UpdateProgressInvoker(int Position );

private System.Windows.Forms.ProgressBar progressBar;


///< summary>测试进度条异步更新代码< / summary>

public void DoSomeWork()

{

progressBar.Minimum = 0;

progressBar.Maximum = 100;


for(int i = 0; i< 10000; i ++)

{

//创建一个指向UpdateProgress方法

UpdateProgressInvoker del = new UpdateProgressInvoker(UpdateProgress);


//异步调用方法

del.BeginInvoke(( int)(i / 100),null,null);


//告诉应用程序处理挂起的消息

//(所以表单没有' '冻结,进度条直观地更新)

Application.DoEvents();

}

}

///< summary>

///此方法不直接更新进度条。相反,它确保

///< see cref =" UpdateProgressCore" />方法(实际完成工作)在创建ProgressBar的

///线程上执行。

///< / summary>

private void UpdateProgress(int Position)

{

//检查Fo​​rm.InvokeRequired属性是否为true。这将表明

//当前正在执行的线程不是创建ProgressBar的线程。

//要同步更新进度条,我们需要调用

//主线程上的方法。

// [this]必须是对Form对象的引用。

if( !this.InvokeRequired)

{

//创建指向执行实际更新的UpdateProgressCore方法的指针

UpdateProgressInvoker del = new UpdateProgressInvoker( UpdateProgressCore);


//在应用程序的主线程上异步调用方法。

//通过使用Form.Invoke,我们确保进度bar会正确更新。

this.Invoke(del,new object [] {Position});

}

else

//当前正在执行的线程是ProgressBar的主题,

//所以通常更新进度条

UpdateProgressCore(Position);

}


///< summary>

///不要直接调用此方法。相反,请调用< see cref =" UpdateProgress" /> ;.

///< / summary>

private void UpdateProgressCore(int Position)

{

progressBar.Value =位置;

}

-

Dave Sexton
dave @ www..jwaonline..com

-------------------- -------------------------------------------------- -

" Grant" < GP ***** @ hotmail.com>在消息新闻中写道:Oo ************** @tk2msftngp13.phx.gbl ...
That looks great - apart from I don''t understand why you call DoEvents - how does this help?

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<#m**************@TK2MSFTNGP12.phx.gbl>

You don''t need to use the Thread class to invoke the delegate asynchronously, however, invoking it using BeginInvoke will execute
the method that the delegate points to on a ThreadPool thread. Also, ProgressBar (and any other WinForms control) requires most of
its members to be executed/accessed on the thread that the control was created on. To account for this requirement, a proper
example can''t be too simple.

Here''s the simplest approach to updating a progress bar aynchronously in a WinForms app (the proper way). I''m assuming that each
method is contained by a class derived from System.Windows.Forms.Form:

private delegate void UpdateProgressInvoker(int Position);
private System.Windows.Forms.ProgressBar progressBar;

/// <summary>Test the progress bar asynchronous update code</summary>
public void DoSomeWork()
{
progressBar.Minimum = 0;
progressBar.Maximum = 100;

for (int i = 0; i < 10000; i++)
{
// Create a pointer to the UpdateProgress method
UpdateProgressInvoker del = new UpdateProgressInvoker(UpdateProgress);

// Invoke the method asynchronously
del.BeginInvoke((int) (i / 100), null, null);

// Tell the app to handle pending messages
// (so the form doesn''t freeze and the progress bar is updated visually)
Application.DoEvents();
}
}
/// <summary>
/// This method does not update the progress bar directly. Instead, it ensures that the
/// <see cref="UpdateProgressCore" /> method (which actually does the work) is executed on the
/// thread that the ProgressBar was created on.
/// </summary>
private void UpdateProgress(int Position)
{
// Check if the Form.InvokeRequired property is true. This will indicate that
// the currently executing thread is not the thread that the ProgressBar was created on.
// To update the progress bar synchronously, we will need to invoke the method on the
// main thread.
// [this] must be a reference to a Form object.
if (!this.InvokeRequired)
{
// Create a pointer to the UpdateProgressCore method which performs the actual update
UpdateProgressInvoker del = new UpdateProgressInvoker(UpdateProgressCore);

// Invoke the method asynchronously on the main thread of the application.
// By using Form.Invoke, we ensure that our progress bar will be updated properly.
this.Invoke(del, new object[] { Position });
}
else
// Currently executing thread is the ProgressBar''s thread,
// so update the progress bar normally
UpdateProgressCore(Position);
}

/// <summary>
/// Do not call this method directly. Instead, call <see cref="UpdateProgress" />.
/// </summary>
private void UpdateProgressCore(int Position)
{
progressBar.Value = Position;
}
--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
"Grant" <gp*****@hotmail.com> wrote in message news:Oo**************@tk2msftngp13.phx.gbl...
我很难理解这个以及我发现的任何代码示例在线长达数公里且很难理解......请有人能给我一个简单的例子,说明如何让代表传递给我的工作班,以便我的主表格可以更新其进度条吗?我知道它与begininvoke和endinvoke等有关,但我只需要一个简单的样本来理解约定。

来自我主表的代码:
--- --------------------------------
form1Delegate dlgate = new form1Delegate(IncreaseProgressBar);
/ /thing.countUpToaMillionOrSomething(new form1Delegate(this.IncreaseProgressBar));
线程thread = new Thread(new ThreadStart(thing.countUpToaMillionOrSomething(dlg ate)));
thread.Start();
-----------------------------------

来自我的工人类的代码叫''东西''
-----------------------------------
公共静态void countUpToaMillionOrSomething(Form1.form1Delegate myMethodDelegate)
{x / 0}
while(x <50)
{
Thread.Sleep(70);
x ++;
myMethodDelegate();
}
}
--------------------- --------------

我只能让它与以下行同步工作:

东西.countUpToaMillionOrSomething(new form1Delegate(this.IncreaseProgressBar));

如果我尝试使用上面的线程示例进行编译,它会发出''Method name expected''并强调下面的代码:

thing.countUpToaMillionOrSomething(dlgate)

任何帮助非常感谢,
谢谢,
格兰特
I am having great difficulty understanding this and any code samples I find online are kilometres long and complicated to
understand...Please could someone give me a simple exampe of how to get a delegate passed to my worked class so that my main form
can update its progress bar? I know it has something to do with begininvoke and endinvoke etc, but I just need a simple sample to
understand the convention.
Code from my main form:
-----------------------------------
form1Delegate dlgate = new form1Delegate(IncreaseProgressBar);
//thing.countUpToaMillionOrSomething(new form1Delegate(this.IncreaseProgressBar));
Thread thread = new Thread(new ThreadStart(thing.countUpToaMillionOrSomething(dlg ate)));
thread.Start();
-----------------------------------

Code from my worker class called ''thing''
-----------------------------------
public static void countUpToaMillionOrSomething(Form1.form1Delegate myMethodDelegate)
{
int x = 0;
while ( x < 50)
{
Thread.Sleep(70);
x ++ ;
myMethodDelegate();
}
}
-----------------------------------
I can only get it to work synchronously with the following line:

thing.countUpToaMillionOrSomething(new form1Delegate(this.IncreaseProgressBar));

If I try compile with the thread example above it spews about ''Method name expected'' and underlines the following bit of code:

thing.countUpToaMillionOrSomething(dlgate)

Any help greatly appreciated,
Thanks,
Grant



[microsoft.public.dotnet.languages.csharp]



[microsoft.public.dotnet.languages.csharp]


注释掉Application.DoEvents,你会看到它的帮助。


与Windows使用消息循环的事实有关。所以WinForms控件的方法调用并不完全是实时。

消息循环就像一个队列,意思是先进先出。您的整个Windows应用程序依赖于来自队列的消息

到达主窗口过程以处理事件,例如System.Windows.Forms.Form.Paint事件。没有这条消息,

表格永远不会刷新,也不会是儿童控制(即进度条)。用户操作也是消息,因此

窗口将冻结。 up,不允许你与它交互,而DoSomeWork则是方法在主窗口上执行

线程。


换句话说:

1. Application.Run启动消息循环

2.你的表格(主应用程序窗口)处理循环上的消息

3.我的方法(DoSomeWork)阻止线程,不允许它接受消息运行

4. Application.DoEvents允许表单处理当前队列中的消息,有效地更新表单,而

DoSomeWork正在做一些工作:)


-

Dave Sexton
dave @ www..jwaonline..com

-------------------------------------- ---------------------------------

" Richard Blewett [DevelopMentor]" < RI ****** @ NOSPAMdevelop.com>在消息新闻中写道:uZ ************** @ TK2MSFTNGP15.phx.gbl ...
Comment out Application.DoEvents and you''ll see how it helps.

Has to do with the fact that Windows uses a "Message Loop" so method invocations for WinForms controls aren''t exactly "real-time".
The message loop is like a queue, meaning first-in first-out. Your whole windows app depends on the messages from the queue
reaching the main window procedure in order to process events, such as System.Windows.Forms.Form.Paint event. Without this message,
the form will never refresh, and neither will it''s child controls (i.e. progress bar). User actions are also messages, so the
window would "freeze" up, not allowing you to interact with it, while the "DoSomeWork" method is executing on the main window
thread.

In other words:
1. Application.Run starts a message loop
2. Your Form (main application window) processes messages on the loop
3. My method (DoSomeWork) blocks the thread, not allowing it to accept messages while it is running
4. Application.DoEvents allows the Form to process messages currently in the queue, effectively updating the Form while
DoSomeWork is doing some work :)

--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
"Richard Blewett [DevelopMentor]" <ri******@NOSPAMdevelop.com> wrote in message news:uZ**************@TK2MSFTNGP15.phx.gbl...
看起来很棒 - 除了我不明白为什么你打电话给DoEvents - 这有什么帮助呢?



Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog

nntp://news.microsoft.com/microsoft .public.dotnet.languages.csharp /< #m ************** @ TK2MSFTNGP12.phx.gbl>

你不需要使用Thread类以异步方式调用委托,但是,使用BeginInvoke调用它将执行委托指向ThreadPool线程的方法。此外,ProgressBar(和任何其他WinForms控件)需要在创建控件的线程上执行/访问其大多数成员。为了解释这个要求,一个正确的例子不能太简单。

这是在WinForms应用程序中异步更新进度条的最简单方法(正确的方法) )。我假设每个
方法都包含在派生自System.Windows.Forms.Form的类中:

私有委托void UpdateProgressInvoker(int Position);
私有系统.Windows.Forms.ProgressBar progressBar;

///< summary>测试进度条异步更新代码< / summary>
public void DoSomeWork()
{
progressBar.Minimum = 0;
progressBar.Maximum = 100;

for(int i = 0; i< 10000; i ++)
{
/ /创建指向UpdateProgress方法的指针
UpdateProgressInvoker del = new UpdateProgressInvoker(UpdateProgress);

//异步调用方法
del.BeginInvoke((int)(i / 100 ),null,null);

//告诉应用程序处理待处理的消息
//(因此表单不会冻结,进度条会直观地更新)
Application.DoEvents();
}
}

///< summary>
///此方法不直接更新进度条。相反,它确保
///< see cref =" UpdateProgressCore" />方法(实际完成工作)在创建ProgressBar的
///线程上执行。
///< / summary>
private void UpdateProgress(int Position)
{
//检查Fo​​rm.InvokeRequired属性是否为true。这将表明
//当前正在执行的线程不是创建ProgressBar的线程。
//要同步更新进度条,我们需要调用
//主线程。
// [this]必须是对Form对象的引用。
if(!this.InvokeRequired)
{
//创建一个指向执行实际更新的UpdateProgressCore方法
UpdateProgressInvoker del = new UpdateProgressInvoker(UpdateProgressCore);

//在应用程序的主线程上异步调用方法。
//使用Form.Invoke,我们确保我们的进度条将正确更新。
this.Invoke(del,new object [] {Position});
}

/ / /当前正在执行的线程是ProgressBar的主题,
//所以通常更新进度条
UpdateProgressCore(位置);
}

///< ;摘要>
///不要直接调用此方法。相反,请调用< see cref =" UpdateProgress" /> ;.
///< / summary>
private void UpdateProgressCore(int Position)
{
progressBar.Value = Position;
}
-
Dave Sexton
dave @ www..jwaonline..com
------------------------------------------------ -----------------------
格兰特 < GP ***** @ hotmail.com>在消息新闻中写道:Oo ************** @tk2msftngp13.phx.gbl ...
That looks great - apart from I don''t understand why you call DoEvents - how does this help?

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<#m**************@TK2MSFTNGP12.phx.gbl>

You don''t need to use the Thread class to invoke the delegate asynchronously, however, invoking it using BeginInvoke will execute
the method that the delegate points to on a ThreadPool thread. Also, ProgressBar (and any other WinForms control) requires most of
its members to be executed/accessed on the thread that the control was created on. To account for this requirement, a proper
example can''t be too simple.

Here''s the simplest approach to updating a progress bar aynchronously in a WinForms app (the proper way). I''m assuming that each
method is contained by a class derived from System.Windows.Forms.Form:

private delegate void UpdateProgressInvoker(int Position);
private System.Windows.Forms.ProgressBar progressBar;

/// <summary>Test the progress bar asynchronous update code</summary>
public void DoSomeWork()
{
progressBar.Minimum = 0;
progressBar.Maximum = 100;

for (int i = 0; i < 10000; i++)
{
// Create a pointer to the UpdateProgress method
UpdateProgressInvoker del = new UpdateProgressInvoker(UpdateProgress);

// Invoke the method asynchronously
del.BeginInvoke((int) (i / 100), null, null);

// Tell the app to handle pending messages
// (so the form doesn''t freeze and the progress bar is updated visually)
Application.DoEvents();
}
}
/// <summary>
/// This method does not update the progress bar directly. Instead, it ensures that the
/// <see cref="UpdateProgressCore" /> method (which actually does the work) is executed on the
/// thread that the ProgressBar was created on.
/// </summary>
private void UpdateProgress(int Position)
{
// Check if the Form.InvokeRequired property is true. This will indicate that
// the currently executing thread is not the thread that the ProgressBar was created on.
// To update the progress bar synchronously, we will need to invoke the method on the
// main thread.
// [this] must be a reference to a Form object.
if (!this.InvokeRequired)
{
// Create a pointer to the UpdateProgressCore method which performs the actual update
UpdateProgressInvoker del = new UpdateProgressInvoker(UpdateProgressCore);

// Invoke the method asynchronously on the main thread of the application.
// By using Form.Invoke, we ensure that our progress bar will be updated properly.
this.Invoke(del, new object[] { Position });
}
else
// Currently executing thread is the ProgressBar''s thread,
// so update the progress bar normally
UpdateProgressCore(Position);
}

/// <summary>
/// Do not call this method directly. Instead, call <see cref="UpdateProgress" />.
/// </summary>
private void UpdateProgressCore(int Position)
{
progressBar.Value = Position;
}
--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
"Grant" <gp*****@hotmail.com> wrote in message news:Oo**************@tk2msftngp13.phx.gbl...
我很难理解这个以及我发现的任何代码示例在线长达数公里且很难理解......请有人能给我一个简单的例子,说明如何让代表传递给我的工作班,以便我的主表格可以更新其进度条吗?我知道它与begininvoke和endinvoke等有关,但我只需要一个简单的样本来理解约定。

来自我主表的代码:
--- --------------------------------
form1Delegate dlgate = new form1Delegate(IncreaseProgressBar);
/ /thing.countUpToaMillionOrSomething(new form1Delegate(this.IncreaseProgressBar));
线程thread = new Thread(new ThreadStart(thing.countUpToaMillionOrSomething(dlg ate)));
thread.Start();
-----------------------------------

来自我的工人类的代码叫''东西''
-----------------------------------
公共静态void countUpToaMillionOrSomething(Form1.form1Delegate myMethodDelegate)
{x / 0}
while(x <50)
{
Thread.Sleep(70);
x ++;
myMethodDelegate();
}
}
--------------------- --------------

我只能让它与以下行同步工作:

东西.countUpToaMillionOrSomething(new form1Delegate(this.IncreaseProgressBar));

如果我尝试使用上面的线程示例进行编译,它会发出''Method name expected''并强调下面的代码:

thing.countUpToaMillionOrSomething(dlgate)

任何帮助非常感谢,
谢谢,
格兰特
I am having great difficulty understanding this and any code samples I find online are kilometres long and complicated to
understand...Please could someone give me a simple exampe of how to get a delegate passed to my worked class so that my main form
can update its progress bar? I know it has something to do with begininvoke and endinvoke etc, but I just need a simple sample to
understand the convention.
Code from my main form:
-----------------------------------
form1Delegate dlgate = new form1Delegate(IncreaseProgressBar);
//thing.countUpToaMillionOrSomething(new form1Delegate(this.IncreaseProgressBar));
Thread thread = new Thread(new ThreadStart(thing.countUpToaMillionOrSomething(dlg ate)));
thread.Start();
-----------------------------------

Code from my worker class called ''thing''
-----------------------------------
public static void countUpToaMillionOrSomething(Form1.form1Delegate myMethodDelegate)
{
int x = 0;
while ( x < 50)
{
Thread.Sleep(70);
x ++ ;
myMethodDelegate();
}
}
-----------------------------------
I can only get it to work synchronously with the following line:

thing.countUpToaMillionOrSomething(new form1Delegate(this.IncreaseProgressBar));

If I try compile with the thread example above it spews about ''Method name expected'' and underlines the following bit of code:

thing.countUpToaMillionOrSomething(dlgate)

Any help greatly appreciated,
Thanks,
Grant


[microsoft.public.dotnet.languages.csharp]



[microsoft.public.dotnet.languages.csharp]



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

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