使用(object sender,eventargs)调用函数 [英] calling function with (object sender , eventargs )

查看:117
本文介绍了使用(object sender,eventargs)调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我有一个用C#编写的应用程序

并且它有一个包含一个函数的类(带有此参数的计时器对象发送者,EventArgs e),现在是

我想通过另一个类调用此函数,因此我创建了该类的一个实例
当我用timer_Tick(null , eventargs.empty )
调用此函数时 或(object sender ,eventargs.empty )

用于参数"object sender , event args e"
错误发生了?


有人有解决办法吗?
谢谢

解决方案

不要直接调用此类函数:它们是事件处理程序,应在适当的时候由框架调用.
相反,请创建一个您调用的私有方法,该方法将被事件处理程序调用.

这样,您无需提供发送者或EventArgs. 但是,仍然没有EventArgs.Empty!

但是请注意,您尝试做的事情可能是错误的,并且在以后学习线程时可能很危险.

我怀疑您实际上是想在一个类中创建一个事件,然后在另一个类中订阅该事件-但我不知道您要实现的目标,因此我不建议您如何做. br/>

[edit]已修复错误:确实存在EventArgs.Empty(感谢GParkings)-OriginalGriff [/edit]


而不是直接调用此函数,而是使用委托创建一个事件并在适当的时间调用


hi I have an application that hs been written with C#

and it has a class that it contains a function(timer with this arguments object sender, EventArgs e) and now

I want to call this function by another class thus i create an instance of this class
when i call this function with timer_Tick(null , eventargs.empty )
or (object sender ,eventargs.empty )

for arguments "object sender , event args e"
error has occured?


anybody have a solution?
thanks

解决方案

Don''t call such functions directly: they are Event handlers and should be called by the framework at the appropriate time.
Instead, create a private method which you call, and which the event handler calls.

That way you do not need to provide the sender or EventArgs. However, there is no EventArgs.Empty anyway!

Be aware though that what you are trying to do it probably wrong, and may be dangerous later, when you learn about threading.

I suspect that you actually want to create an event in one class, and subscribe to it in the other - but I don''t know what you are trying to achieve, so I can''t advise you how to do it.


[edit]Removed error: there is indeed an EventArgs.Empty (thanks GParkings) - OriginalGriff[/edit]


instead of directly calling such function create one event using delegate and call in appropriate time


这篇关于使用(object sender,eventargs)调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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