Threading & Event Handlers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cmwb2000
    New Member
    • Jul 2009
    • 9

    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

    MouseClickDurat ion mcd = new MouseClickDurat ion();
    HookManager.Mou sePressed += mcd.Pressed;
    HookManager.Mou seReleased += mcd.Released;

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

    Many Thanks

    Chris
  • IanWright
    New Member
    • Jan 2008
    • 179

    #2
    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.

    Comment

    • cmwb2000
      New Member
      • Jul 2009
      • 9

      #3
      More Detail

      The issue I was having was I may have multiple timers working at the same time, for instance;

      MouseButtonPres sed - MouseDurationTi mer Started
      MouseButtonRele ased - MouseDurationTi mer Stopped / MouseLatencyTim er Started
      MouseButtonPres sed - MouseLatencyTim er Stopped / MouseDurationTi merStarted.

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

      Thanks

      Chris

      Comment

      • IanWright
        New Member
        • Jan 2008
        • 179

        #4
        So something like this running just on the main thread? If so I'd expect that to work fine...

        Code:
        StopWatch durationTimer = new StopWatch();
        StopWatch latency = new StopWatch();
        
        public void OnMousePress(object sender, MouseEventArgs e)
        {
           latency.Stop();
           durationTimer.Start();
           Trace.WriteLine(latency.ToString());
        }
        
        public void OnMouseRelease(object sender, MouseEventArgs e)
        {
           durationTimer.Stop();
           latency.Start();
           Trace.WriteLine(durationTimer.ToString());
        }

        Comment

        • cmwb2000
          New Member
          • Jul 2009
          • 9

          #5
          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

          Comment

          • IanWright
            New Member
            • Jan 2008
            • 179

            #6
            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.

            Comment

            • cmwb2000
              New Member
              • Jul 2009
              • 9

              #7
              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

              Comment

              • IanWright
                New Member
                • Jan 2008
                • 179

                #8
                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 BaseEventChecke r
                {
                public virtual OnMouseDown() {}
                public virtual OnMouseRelease( ) {}
                }

                public class Duration : BaseEventChecke r
                {
                public override OnMouseDown() {}
                public override OnMouseRelease( ) {}
                }

                public class Latency : BaseEventChecke r
                {
                public override OnMouseDown() {}
                public override OnMouseRelease( ) {}
                }


                Then initalize lots of BaseEventChecke r's at the start. For each event you are interested in, go through each BaseEventChecke r 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.

                Comment

                • cmwb2000
                  New Member
                  • Jul 2009
                  • 9

                  #9
                  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(objec t sender, MouseEventArgs e)
                  {
                  mouseLatencyTim er.Stop();
                  int LatencyTime = Convert.ToInt32 (mouseLatencyTi mer.ElapsedMill iseconds);
                  ReadOutBox.Appe ndText("Mouse Latency = " + LatencyTime+ "\n");
                  WriteDataToDB.w riteMouseLatenc y(LatencyTime);
                  mouseLatencyTim er.Reset();
                  mouseDurationTi mer.Start();
                  }

                  private void mouseReleased(o bject sender, MouseEventArgs e)
                  {
                  mouseDurationTi mer.Stop();
                  int DurationTime = Convert.ToInt32 (mouseDurationT imer.ElapsedMil liseconds);
                  ReadOutBox.Appe ndText("Mouse Duration = " + DurationTime + "\n");
                  WriteDataToDB.w riteMouseDurati on(DurationTime );
                  mouseDurationTi mer.Reset();
                  mouseLatencyTim er.Start();
                  }

                  Comment

                  • IanWright
                    New Member
                    • Jan 2008
                    • 179

                    #10
                    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.

                    Comment

                    Working...