473,394 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Threading & Event Handlers

I am trying to build a peice of software to survey how a user interacts with a pc. I need to record keystroke latencies and durations etc.

I have got the event handler working and I was wanting to create a seperate class for each parameter such as keystroke latency etc. I got it working on the main thread but when I ran latency and duration together they interfiered with each other. Im guessing I am going to have to run them on sperate threads only problem is that everything I have tried doesnt work. I have tried putting the event handlers in the class and in the main but when running in threads the events dont seem to fire.

Here is what I have got so far

MouseClickDuration mcd = new MouseClickDuration();
HookManager.MousePressed += mcd.Pressed;
HookManager.MouseReleased += mcd.Released;

How do I declare this to run in a seperate thread?

Many Thanks

Chris
Jul 14 '09 #1
9 2840
IanWright
179 100+
I believe events work cross threaded, which is why you get them from both threads.

Can you explain the problem a little more, i.e. what are latency and duration? Trying to get an idea exactly what you're doing.
Jul 14 '09 #2
The issue I was having was I may have multiple timers working at the same time, for instance;

MouseButtonPressed - MouseDurationTimer Started
MouseButtonReleased - MouseDurationTimer Stopped / MouseLatencyTimer Started
MouseButtonPressed - MouseLatencyTimer Stopped / MouseDurationTimerStarted.

When I was running these 2 together in the main thread, the latencytimer would give a duration of 0.

Thanks

Chris
Jul 14 '09 #3
IanWright
179 100+
So something like this running just on the main thread? If so I'd expect that to work fine...

Expand|Select|Wrap|Line Numbers
  1. StopWatch durationTimer = new StopWatch();
  2. StopWatch latency = new StopWatch();
  3.  
  4. public void OnMousePress(object sender, MouseEventArgs e)
  5. {
  6.    latency.Stop();
  7.    durationTimer.Start();
  8.    Trace.WriteLine(latency.ToString());
  9. }
  10.  
  11. public void OnMouseRelease(object sender, MouseEventArgs e)
  12. {
  13.    durationTimer.Stop();
  14.    latency.Start();
  15.    Trace.WriteLine(durationTimer.ToString());
  16. }
  17.  
Jul 14 '09 #4
Yeh my code is very similar to that. The next step unfortinatly gets a bit more complicated with Tridrgraphs, They are the time taken to enter 3 consecutive keystrokes so hello would produce a time for hel ell llo. This will require 3 seperate stop watches.

Thanks

Chris
Jul 14 '09 #5
IanWright
179 100+
Right, Ok. And you're saying the bit of code above on its own doesn't work?

Could you post your actual code (something that compiles and runs) then we might be able to give it a try.
Jul 14 '09 #6
Hi

Your right the code you wrote in the main class works perfectly, must of been something to do with the class and writing the data to the database.

You have given me a idea of how to get around this, I will post back the results.

Many Thanks

Chris
Jul 14 '09 #7
IanWright
179 100+
By the way. Thinking about it, it sounds like you want to create some new classes, with Events that have an associated stopwatch.

e.g.

public class BaseEventChecker
{
public virtual OnMouseDown() {}
public virtual OnMouseRelease() {}
}

public class Duration : BaseEventChecker
{
public override OnMouseDown() {}
public override OnMouseRelease() {}
}

public class Latency : BaseEventChecker
{
public override OnMouseDown() {}
public override OnMouseRelease() {}
}


Then initalize lots of BaseEventChecker's at the start. For each event you are interested in, go through each BaseEventChecker and call the method that relates to that event.

Then each one of the classes can respond in its own way. It can start its stopwatch, stop one. It can increment a count and then decide what to do with the stopwatch.

Then it doesn't matter if you have you're Tridrgraphs. They are irrelavant to the actual main code, and helps you isolate their specific functionality.
Jul 14 '09 #8
Hi

You were right there is no problem with running the timers in the main class, the issue seems to be writing the data to the database. The code below works perfectly if I comment out the writeDataToDB. I am guessing that its going to have to be this that I run in a seperate thread or maybe using the backgound worker? any suggestions?

Thanks

Chris

private void mouseDown(object sender, MouseEventArgs e)
{
mouseLatencyTimer.Stop();
int LatencyTime = Convert.ToInt32(mouseLatencyTimer.ElapsedMilliseco nds);
ReadOutBox.AppendText("Mouse Latency = " + LatencyTime+ "\n");
WriteDataToDB.writeMouseLatency(LatencyTime);
mouseLatencyTimer.Reset();
mouseDurationTimer.Start();
}

private void mouseReleased(object sender, MouseEventArgs e)
{
mouseDurationTimer.Stop();
int DurationTime = Convert.ToInt32(mouseDurationTimer.ElapsedMillisec onds);
ReadOutBox.AppendText("Mouse Duration = " + DurationTime + "\n");
WriteDataToDB.writeMouseDuration(DurationTime);
mouseDurationTimer.Reset();
mouseLatencyTimer.Start();
}
Jul 14 '09 #9
IanWright
179 100+
Start a new thread (e.g. post a new question), and in it ask why your WriteDataToDb class isn't working. Then post some details on what it does.
Jul 14 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Jon Pope | last post by:
I've got a basic question about threading which I'm hoping someone here will be able to answer: Let's say my calling CallingClass has instantiated and started a worker thread. When my worker...
3
by: nobs | last post by:
Hi guys I think I missunderstud something with delegates & events Hopefully someone can bring some light in this issue: Following problem I have a class which provide some events (like...
0
by: R Reyes | last post by:
Hi. I'm trying to make some event handlers for buttons that are nested within datalists, however I keep getting errors trying to access them and a blank page shows w/o any real error message. How...
11
by: Jim H | last post by:
I have work being done in worker threads and I trigger an event, that gui objects have subscribed to, when the work is complete. Will that event handler code be executed in the ui thread or the...
6
by: MPH Computers | last post by:
Hi I am looking for some help on Threading and Critical Sections I have a main thread that controls an event the event handler creates a new thread for carrying out the work because the...
3
by: Islam Elkhayat | last post by:
I have master datagrid & master details in 5 panels each belong to diffrent dbtable. I have a dropdpwn list to switch which panel to be visible.. I also have one navigation bar for add, update,...
5
by: Spam Catcher | last post by:
Hi all When .NET fires and event, does the event handler execute under a new thread, or does it execute under the primary application thread? Basically if I have events firing, do the event...
3
by: Ross Boylan | last post by:
I would like my different threads to log without stepping on each other. Past advice on this list (that I've found) mostly says to send the messages to a Queue. That would work, but bypasses...
5
by: Yehia A.Salam | last post by:
Hello, I am building a network application that make use of .Net Sockets, I created a class that works like a server and fires an event when anything arrives at the server, however I ran into...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.