如何逐个调用c#中的两个方法 [英] how to call two methods in c# one by one

查看:59
本文介绍了如何逐个调用c#中的两个方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在win应用程序中有两种方法。



I have two methods in my win app.

private void zim()
{
//code
}











and

private void zimtim()
{
//code
}









我必须访问button_Click event.but zimtim();必须在zim()10秒后调用;



任何帮助?





I have to access both methods on button_Click event.but zimtim(); must be called after 10 seconds of zim();

any help?

推荐答案

你描述的内容并不完全是一个接一个。



我<罢工>几乎肯定(在OP的评论回复之后非常肯定我对问题[END EDIT]的评论)这个要求毫无意义,但你应该调用一个方法,然后为你需要的延迟设置一个计时器,并在timer事件处理程序中调用第二个方法。请注意,此方法将与您的UI异步调用,因此您必须准备好处理它。



计时器的选择取决于几个因素,包括您正在使用的UI。例如,对于 System.Windows.Forms ,您可以使用 System.Windows.Forms.Timer ,但此计时器准确性极差,但我可以想象,在10秒内,你会不会太在意。然后,您可以在计时器甚至处理程序中执行任何操作。对于其他一些计时器,您无法直接调用任何UI方法/属性,因为可以在其他某个线程中调用timer事件。此外,您可以使用线程而不是计时器。



您无法从非UI线程调用与UI相关的任何内容。相反,您需要使用 Invoke System.Windows.Threading的方法。 Dispatcher (对于Forms或WPF)或 System.Windows.Forms.Control (仅限表单)。



您将在我过去的答案中找到有关其工作原理和代码示例的详细说明:

Control.Invoke()与Control.BeginInvoke() [ ^ ],

使用Treeview扫描仪和MD5的问题 [ ^ ]。



另请参阅有关线程的更多参考资料:

如何让keydown事件在不同的操作上运行vb.net中的线程 [ ^ ],

在启用禁用+多线程后控制事件未触发 [ ^ ]。



使用WPF,您可以使用 System.Windows.Threading.DispatcherTimer ,它也可以在一个单独的线程上运行,因此您需要使用上述机制: br />
http:/ /msdn.microsoft.c om / en-us / library / system.windows.threading.dispatchertimer%28v = vs.110%29.aspx [ ^ ]。



-SA
What you describe is not exactly "one by one".

I'm almost sure ( quite sure after the OP's comment in reply to my comment to the question[END EDIT]) this requirement makes no sense, but you should call one method, then set a timer for the delay you need, and in the timer event handler, call the second method. Note that this method will be called asynchronously to you UI, so you have to be prepared to deal with that.

The choice of the timer depends on several factors, including the UI you are using. For example, for System.Windows.Forms, you can use System.Windows.Forms.Timer, but this timer has extremely bad accuracy, but I can imagine that, with 10 sec, you would not care much. Then, you can do whatever you want in the timer even handler. With some other timers, you cannot directly call any UI methods/properties, because the timer event can be called in some other thread. Besides, you can use a thread instead of the timer.

You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

With WPF, you can use System.Windows.Threading.DispatcherTimer which also runs on a separate thread, so you would need to use the mechanism described above:
http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer%28v=vs.110%29.aspx[^].

—SA


试试这个:

Try this :
public void zim()
{
   // your code here
   Thread.Sleep(10*1000);// 10 seconds wait
   zimtim(); // call the other
}



只需在 button_click()中拨打 zim()


这篇关于如何逐个调用c#中的两个方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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