Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :-

C#
int mouseScrolldetector;

C#
Thread thr;

C#
ThreadRunner tr = new ThreadRunner();



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

OR
C#
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
C#
thr = new Thread(new ThreadStart(tr.DetectMouseScroll));

OR
C#
thr = new Thread(new ParameterizedThreadStart(tr.DetectMouseScroll));


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
Posted
Comments
Shahin Khorshidnia 7-Mar-12 5:48am    
Start with this:

https://www.google.com/search?hl=en&sa=X&ei=njxXT8rIFcbu8QOdpqn3Dg&ved=0CB0QvwUoAQ&q=c%23+how+to+run+parameterized+thread&spell=1

You need to specify only the method name without brackets like below ex
C#
Thread mythread= new Thread(tr.DetectMouseScroll);
object obj=//obj to be passed to Detect MouseScroll method
mythread.Start(obj);//equivalent to 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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900