事件或其他? [英] Event or other?

查看:56
本文介绍了事件或其他?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我是C#的新手,拥有Windows表单应用程序。主要表格

有一个进度条。在主窗体的一种方法中,我调用一个派生自Object的

类来处理文件。在

处理期间,我希望自定义类能够更新

进度条。


什么实现这一目标的最好方法是什么?我一直在阅读

活动/代表,我开始得到一个模糊的理解,但是我想要确保我不会贬低路径错误。


感谢您的任何建议/见解,

Steve

解决方案

< blockquote>


" Steve" < ki **** @ harpservices.comwrote in message

news:iv ************************** ****** @ 4ax.com ...


大家好,


我是新手C#并拥有Windows表单应用程序。主要表格

有一个进度条。在主窗体的一种方法中,我调用一个派生自Object的

类来处理文件。在

处理期间,我希望自定义类能够更新

进度条。


什么实现这一目标的最好方法是什么?我一直在阅读

活动/代表,我开始得到一个模糊的理解,但是我想要确保我不会贬低错误的道路。



你必须使用一个线程来执行操作,同时在

UI线程中运行进度条。

看一下Control.Invoke作为工作者之间的沟通方式

线程和UI


On Thu ,2007年9月13日21:36:25 +0200,Steve< ki **** @ harpservices.comwrote:


大家好,


我是C#中的新手并拥有Windows表单应用程序。主要表格

有一个进度条。在主窗体的一种方法中,我调用一个派生自Object的

类来处理文件。在

处理期间,我希望自定义类能够更新

进度条。


什么实现这一目标的最好方法是什么?我一直在阅读

活动/代表,我开始得到一个模糊的理解,但是我想要确保我不会贬低错误的道路。


感谢您的任何建议/见解,

Steve



Steve Steve


事件在这里应该可以正常工作,但是你还需要在BackgroundWorker或其他线程中开始处理,否则主线程没有时间处理事件。并且由于事件将被捕获到主线程以外的线程中,因此您还需要使用Invoke。下面的示例可能会给您一些想法。如果你不介意直接打电话给表格(这不是很好的OO练习,你可以跳过投掷活动并直接打电话给表格)


公共课MyForm:表单

{


< ...>


委托void ProgressBarDelegate(int value) ;

BackgroundWorker worker = new BackgroundWorker();

MyClass myClass = new MyClass();


private void Method()

{

myClass.MyEvent + = new MyClass.MyEventHandler(myClass_MyEvent);

worker.DoWork + = new DoWorkEventHandler(worker_DoWork);

worker.RunWorkerCompleted + = new RunWorkerCompletedEventHandler(worker_RunWorkerCom pleted);

worker.RunWorkerAsync();

}


void myClass_MyEvent(int value)

{

if(progressBar1.InvokeRequired)

progressBar1.Invoke(new ProgressBarDelegate(myClass_MyEvent) ,新对象[] {value});

其他

progressBar1.Value = value / 2;

}


void worker_DoWork(对象发送者,DoWorkEventArgs e)

{

myClass.Method();

}


void worker_RunWorkerCompleted(对象发送者,RunWorkerCompletedEventArgs e)

{

MessageBox.Show(" Done");

}

}


公共类MyClass

{

公共事件MyEventHandler MyEvent;

public delegate void MyEventHandler(int value);


public void Method()

{

for(int i = 0;我< 200; i ++)

{

System.Threading.Thread.Sleep(200);

MyEvent(i);

}

}

}


-

快乐编码!

Morten Wennevik [C#MVP]


2007年9月13日星期四16:05:20 -0400,Ignacio Machin \(.NET / C#MVP)

\)" < machin TA laceupsolutions.comwrote:


>

" Steve" < ki **** @ harpservices.com写信息
新闻:iv ****************************** **@4ax.com ..


>大家好,

我是C#的新手,拥有Windows表单应用程序。主要表格
有一个进度条。在主窗体的一种方法中,我调用一个派生自Object的
类来处理文件。在
处理期间,我希望自定义课程能够更新
进度条。

实现这一目标的最佳方法是什么?我一直在阅读
活动/代表,我开始得到一个模糊的理解,但我想确保我不会走错路。


你必须使用一个线程来执行操作,同时在
UI线程中进度条正在运行。
看看Control。调用工作者
线程和UI之间的通信方式



如果可以,我想避免多线程。我没有使用

管理线程的经验,而且看起来应该只是引用表单上的一个组件很复杂。


Hi All,

I''m a newbee in C# and have a Windows form application. The main form
has a progress bar. In one of the methods of the main form, I call a
class derived from Object which processes a file. During the
processing, I''d like the custom class to be able to update the
progress bar.

What''s the best way to make this happen? I''ve been reading up on
events/delegates and am beginning to get a vague understanding, but I
wanted to make sure I''m not going down the wrong path.

Thanks for any suggestions/insight,
Steve

解决方案

Hi,

"Steve" <ki****@harpservices.comwrote in message
news:iv********************************@4ax.com...

Hi All,

I''m a newbee in C# and have a Windows form application. The main form
has a progress bar. In one of the methods of the main form, I call a
class derived from Object which processes a file. During the
processing, I''d like the custom class to be able to update the
progress bar.

What''s the best way to make this happen? I''ve been reading up on
events/delegates and am beginning to get a vague understanding, but I
wanted to make sure I''m not going down the wrong path.

You have to use a thread to perform the operation and in the meantime in the
UI thread the progress bar is running.
Take a look at Control.Invoke as the way to communication between the worker
thread and the UI


On Thu, 13 Sep 2007 21:36:25 +0200, Steve <ki****@harpservices.comwrote:

Hi All,

I''m a newbee in C# and have a Windows form application. The main form
has a progress bar. In one of the methods of the main form, I call a
class derived from Object which processes a file. During the
processing, I''d like the custom class to be able to update the
progress bar.

What''s the best way to make this happen? I''ve been reading up on
events/delegates and am beginning to get a vague understanding, but I
wanted to make sure I''m not going down the wrong path.

Thanks for any suggestions/insight,
Steve

Hi Steve

An event should work fine here, but you also need to start your processing in a BackgroundWorker or another thread or the main thread won''t get time to process the event. And since the event will get caught in a thread other than the main thread, you need to use Invoke as well. The sample below might give you some ideas. If you don''t mind having the classcall the form directly (which isn''t really good OO practice, you can skip throwing the event and call the form directly)

public class MyForm : Form
{

<...>

delegate void ProgressBarDelegate(int value);
BackgroundWorker worker = new BackgroundWorker();
MyClass myClass = new MyClass();

private void Method()
{
myClass.MyEvent += new MyClass.MyEventHandler(myClass_MyEvent);
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCom pleted);
worker.RunWorkerAsync();
}

void myClass_MyEvent(int value)
{
if (progressBar1.InvokeRequired)
progressBar1.Invoke(new ProgressBarDelegate(myClass_MyEvent), new object[] { value });
else
progressBar1.Value = value / 2;
}

void worker_DoWork(object sender, DoWorkEventArgs e)
{
myClass.Method();
}

void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("Done");
}
}

public class MyClass
{
public event MyEventHandler MyEvent;
public delegate void MyEventHandler(int value);

public void Method()
{
for (int i = 0; i < 200; i++)
{
System.Threading.Thread.Sleep(200);
MyEvent(i);
}
}
}

--
Happy coding!
Morten Wennevik [C# MVP]


On Thu, 13 Sep 2007 16:05:20 -0400, "Ignacio Machin \( .NET/ C# MVP
\)" <machin TA laceupsolutions.comwrote:

>Hi,

"Steve" <ki****@harpservices.comwrote in message
news:iv********************************@4ax.com.. .

>Hi All,

I''m a newbee in C# and have a Windows form application. The main form
has a progress bar. In one of the methods of the main form, I call a
class derived from Object which processes a file. During the
processing, I''d like the custom class to be able to update the
progress bar.

What''s the best way to make this happen? I''ve been reading up on
events/delegates and am beginning to get a vague understanding, but I
wanted to make sure I''m not going down the wrong path.


You have to use a thread to perform the operation and in the meantime in the
UI thread the progress bar is running.
Take a look at Control.Invoke as the way to communication between the worker
thread and the UI

I''d like to avoid multi-threading if I can. I have no experience in
managing threads and it doesn''t seem like it should be that
complicated to just reference a component on a form.


这篇关于事件或其他?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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