C-Sharp Windows窗体中的参数化查询 [英] paramaterized query in C-Sharp windows forms

查看:119
本文介绍了C-Sharp Windows窗体中的参数化查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在Visual Studio 2010中创建一个小的Windows应用程序,它将检测窗口名称,如果窗口名称与所需的窗口名称匹配,它将捕获鼠标滚动事件并相应地按一些功能键.为了实现这一点,以便即使该过程不是活动表单,该过程也能正常工作,我需要创建将继续监视并相应地触发事件的线程.为此,我创建了函数


1)获取当前活动窗口的GetActiveWindowTitle()
2)Form1_MouseWheel(对象发送方,MouseEventArgs e),它检测鼠标活动并根据e.Delta的值设置参数mouseScrolldetector的值;
3)ThreadRunner类,它是可用于运行线程的类.它包含一个功能DetectMouseScroll(int mouseScrollDetectorBIt),该功能检测MouseScrolldetector的值并相应地按功能键.
4)现在触发事件,我使用以下代码:-

Hi,
I am creating a small Windows application in Visual Studio 2010 that will detect the window name and if the window name matches the required window name, it will capture the mouse scroll events and accordingly press some function keys. To implement this, so that the process works even when it is not the active form, I need to create threads that will keep on monitoring and keep on triggering the events accordingly. For this, I have created functions


1) GetActiveWindowTitle() that fetches the current active window
2) Form1_MouseWheel(object sender, MouseEventArgs e) that detects the mouse activity and sets the value of a parameter mouseScrolldetector based on the value of e.Delta;
3) ThreadRunner class that is a class that can be used to run the thread. It contains a function DetectMouseScroll(int mouseScrollDetectorBIt)that detects the value of MouseScrolldetector and accordingly presses the function keys.
4) Now to trigger the event, I am using the following code :-

int mouseScrolldetector;


Thread thr;


ThreadRunner tr = new ThreadRunner();







thr = new Thread(new ThreadStart(tr.DetectMouseScroll(mouseScrolldetector)));




OR

thr = new Thread(new ParameterizedThreadStart(tr.DetectMouseScroll(mouseScrolldetector)));



但是它们两个都显示以下错误:方法名称为预期

如果我将其更改为



But both of them are showing the following error : method name expected

If I change it to

thr = new Thread(new ThreadStart(tr.DetectMouseScroll));




OR

thr = new Thread(new ParameterizedThreadStart(tr.DetectMouseScroll));



然后显示对于"DetectMouseScroll"没有重载,与代理"System.Threading.ParameterizedThreadStart"相匹配



任何人都知道为什么会显示预期的错误方法名称"以及如何运行参数化线程而不会出现错误

在此先感谢
Jashobanta Chakraborty



then it is showing ‘no overload for the "DetectMouseScroll" matches the delegate ‘System.Threading.ParameterizedThreadStart’



Anyone has any idea of why the error Method Name expected is being shown and how to run the parameterized thread, without error 

Thanks in Advance
Jashobanta Chakraborty

推荐答案

检查此链接

在C#中将多个参数发送到线程 [ ^ ]

http://msdn.microsoft.com/en-us/library/1h2f2459.aspx [ ^ ]

http://social.msdn.microsoft.com/Forums/是/csharpgeneral/thread/22d5792c-aa0b-4626-bc64-f04daa0883e5 [
Check this links

Send multiple parameters to a thread in C#[^]

http://msdn.microsoft.com/en-us/library/1h2f2459.aspx[^]

http://social.msdn.microsoft.com/Forums/is/csharpgeneral/thread/22d5792c-aa0b-4626-bc64-f04daa0883e5[^]

Hope this will guide you


您只需指定方法名称,不带方括号,例如ex
You need to specify only the method name without brackets like below ex
Thread mythread= new Thread(tr.DetectMouseScroll);
object obj=//obj to be passed to Detect MouseScroll method
mythread.Start(obj);//equivalent to ParameterizedThreadStart 


请注意,您只能将对象作为参数传递给ParameterizedThreadStart委托封装的方法.

同样也不需要显式实例化ParameterizedThreadStart或Thread,只需将方法名称分配给Delegate变量即可,因为C#可以推断委托类型,并创建包装该方法的委托的新对象并将其分配给委托变量.
当编译器看到该对象传递给threadstart时,它将在内部创建ParameterizedThreadStart的实例


Please note you can only pass object as parameter to the method encapsulated by ParameterizedThreadStart delegate.

Also there is no need for explicit instantiation of ParameterizedThreadStart or Thread ,you can simply assign the method name to Delegate variable as C# infers the delegate type and creates the new object of delegate wrapping the method and assigns it to delegate variable.
When compiler sees that object is passed to threadstart it creates instance of ParameterizedThreadStart internally


这篇关于C-Sharp Windows窗体中的参数化查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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