C# control remote mouse

i have a WCF REST service where i NEED to implement a function where it will be accepting X,Y mouse coordinate.

These coordinate will be send from a client with WebGET or WebInvoke then the service will set this coordinate to the local mouse position.

The way i am doing it right now is by sending mouse coordinate on every mouse move by creating different thread for each request.

The problem i am facing is that it is extremely slow and sometime the mouse look like it warp around the screen(on the service computer).

What would be an effective, fluid way to accomplish this?

P.-S. I have to absolutly use WCF REST.

Thanks in advance!
Who is Participating?

Improve company productivity with a Business Account.Sign Up

AngryBinaryConnect With a Mentor Commented:
Well, I was thinking one possible enhancement might be to run the service in "Singleton" mode (InstanceContextMode = InstanceContextMode.Single). That will slightly speed up the amount of time that a service responds to a request.

Do you think you can post your service code that responds to the mouse events as well?
You're going to have to expect some delay between updates via a REST service. Starting a new thread and web request for every mouse move event is going to storm both your CPU and network.

I would send all the requests from a single thread that polls the mouse position at a set interval, and sends the requests asynchronously (ignoring the callback). Also, send a serial # (timestamp?) with each request so you can throw out any coordinates received "out of order". The mouse is going to be a bit jumpy by virtue of latency, but I'd wager it turns out a bit smoother.
EnillAuthor Commented:
Hi and thanks for the reply.

I changed my code a little bit to try your suggestion.

All call are made from one thread and i poll the mouse position every 50ms. Honestly it didnt really changed anything.

Now im wondering if it would be faster with WebInvoke instead of WebGet but i doubt it.

When i say its slow, its really terrible and unusable.
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

The difference between WebInvoke and WebGet is just the HTTP verb used (GET vs POST), so I wouldn't expect much of a difference either.

It may just be the case that REST (or any other HTTP) isn't going to be suited for your needs. Typically, your application would call for a persistent connection between two machines. The HTTP model is one where a new connection is created, used, and closed for each request - it's a model better suited to a situation where you don't want a limited number of clients tying up the available connections to the server application, and is geared towards serving many clients where scalability is key (like on a web server).

That said, have you profiled the app to find out where the bottleneck is? Can you trace the interval at which requests are actually going out and being received?
EnillAuthor Commented:
you are right about WCF REST not being suited for that kind of operation(by design it is not the best).

The bottleneck is clearly in the speed the connection are created, request sent and executed on the other side.

Now i am thinking about figured a way to send direction data. I mean, on the client side i would have a "stick" for exemple, moving the stick inside a circle by pointing it on a direction. This way i could send the wanted direction of the cursor to the server side and the server would start moving the cursor in that direction until it receive a new direction or a StopMoving command.

I guess i will have to do some math there, probably simply stuff but on top of my head i can't remember the Cos/Sin law.
If you want to calculate rate of change over the x/y axis based on an angle vector, where T is the angle (in radians) and s is speed:

x += s*cos(T);
y += s*sin(T);

Anyway, what is the big picture that you're trying to accomplish?
EnillAuthor Commented:
the big picture is actually to move the mouse but i have to use a WCF Rest service.

I am limited by the WCF Rest speed of connection creation everytime i want to send a new position(on mousemove).

I figured it would be best to just elimate the number of total request done by giving a direction to my remote moouse instead of every new point.
If you send a direction, what you'll find is that the remote mouse doesn't mimic the movements of the local one. I'm not sure how important that is for your application, but it's going seem like the mouse sort of drifts about on its own.

If REST is an absolute must, maybe you can tune up on the service side of the application. How is the service hosted? Can you post your service configuration?
EnillAuthor Commented:
Hi, i am definatly concerned with the problem of "no natural move" but i dont think it would be worst than it is currently.

I am using a Self-Hosted WCF servie in a WPF application actually.

I havent passed much time tuning the configuration(i was too busy with the functionality i guess).

It look something like this:

WebServiceHost host = new WebServiceHost(typeof(My_Class), new Uri("http://localhost:8000/"));
host.AddServiceEndpoint(typeof(My_Class_Interface), new WebHttpBinding(), "");

If you have any tuning suggestions then i am taker.

EnillAuthor Commented:
Wrong button pressed.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.