|
|
About WiimoteChanged event
Last post Wed, Oct 15 2008 2:41 AM by Brian Peek. 13 replies.
-
Wed, Aug 6 2008 8:53 AM
|
|
-
Caio



- Joined on Fri, Jun 13 2008
- São Paulo, SP
- Posts 19
|
About WiimoteChanged event
Hi, I'm concerned about some general C# behavior on events/delegates. I'd like to know if when I overload the WiimoteChanged event (public event EventHandler<WiimoteChangedEventArgs> WiimoteChanged) it calls the method in another thread, this is, asynchronously or not. The thing is that I came from C and C++ and I'm afraid that I have to implement mutexes to avoid racing conditions or inconsistent data reading. The situation is that I'm doing something like this (I need to avoid inconsistent data reading in draw() method): wiimote.WiimoteChanged += updateCoordinates; // This function updates the coordinates that will be used to draw on screen.
updateCoordinates(/* goes the parameters */) { /* this is just an example code - think of "..." as the full path and that'll be set only if the IRSensor is found */ this.x = ...IRSensor[0].Position.X; this.y = ...IRSensor[0].Position.Y; }
draw() { /* this function uses the x, y saved on updatedCoordinates to draw on screen */ } Let's suppose that updateCoordinates() is called in an async way, in another thread, this way if draw() is called when updateCoordinates() is, I will draw in the middle of the attribution (again, I want to avoid this). For example, let's suppose that first coordinates we've read from the wiimote are (4, 5) (x and y respectively) then draw() is called. Draw() is supposed to draw on coordinate 4 and 5, but if updateCoordinates() is triggered again asynchronously it can happen that X can be updated but Y not, and draw would draw something like this (16, 5) when it should draw the new captured value, e.g (16, 25). The thing is: 1. Can the situation above happen?
2. If 1 is true, how can I handle (avoid) this situation so draw would draw only when updateCoordinates() is finished? Must I obligatorily call draw in the end of updateCoordinates nowhere else? Thanks in advance and sorry for hitting the key of "avoid the situation" more than once I just wanted to be sure I expressed myself correctly :)
|
|
-
-
Brian Peek



- Joined on Fri, Mar 17 2006
- Niskayuna, NY
- Posts 1,127
|
Re: About WiimoteChanged event
I've tried to write it so that it happens infrequently, but there's no guarantee it won't happen. The event is raised at the completion of an IO read and before the next async read is setup. But there's the possibility you won't be finished processing your event handler before the next read completes and another event fires. If you absolutely need them to be synchronous like you described, your best bet is to either wrap the entire event handler in a lock/mutex, or wrap your drawing code in a lock/mutex.
|
|
-
-
Caio



- Joined on Fri, Jun 13 2008
- São Paulo, SP
- Posts 19
|
Re: About WiimoteChanged event
So what I do basically need to do is to call WaitOne() before entering on that critical area and then call ReleaseMutex() after I finish using it, right? (I've never used it. I'm used to pthread_mutex_lock and stuff from C :P) Regards,
|
|
-
-
-
vanmelle52


- Joined on Thu, May 15 2008
- Posts 12
|
Re: About WiimoteChanged event
Of course, if you're using C#, the "lock" construct is a much simpler way. I'd show you an example, but this stupid forum software will remove all my line breaks :-(.
|
|
-
-
Brian Peek



- Joined on Fri, Mar 17 2006
- Niskayuna, NY
- Posts 1,127
|
Re: About WiimoteChanged event
If you're logged in and using the Standard or Enhanced editor it won't. Click "Edit Profile" at the top right, hit the Site Options tab, and change your editor. Line breaks will be preserved.
|
|
-
-
vanmelle52


- Joined on Thu, May 15 2008
- Posts 12
|
Re: About WiimoteChanged event
Hmm, the remaining choice is "Plain Text". (What does that say about "Enhanced" that "Plain Text" is more faithful to the author?) Okay, my profile now says Plain Text editor (more than 10 minutes ago, so the mysterious message about it not taking effect immediately shouldn't matter).
So then this should be a new line.
And this as well.
However, if the Preview tab is to be believed, this didn't work, either.
|
|
-
-
Brian Peek



- Joined on Fri, Mar 17 2006
- Niskayuna, NY
- Posts 1,127
|
Re: About WiimoteChanged event
I'm a total idiot. I dropped the word "not" in that last reply. It should have read "If you're not logged in and using the Standard or Enhanced editor it won't work". So. You need to be logged in and have your text editor set to Standard or Enhanced. It's a bug with this version of CommunityServer and I haven't had the time to upgrade to the latest since they rewrote the entire skinning system and it's a nightmare to port this skin to it.
|
|
-
|
|
Re: About WiimoteChanged event
Hi,
i am also experiencing the same problem...i am trying to get acceleration data from the wii mote and calculate pitch and transfer data to lego nxt motor, the problem is that the events generated are so much that motor cannot follow the wiimote as before it attains the final position ..next event comes in..so is there a way that i can poll for the current value of the wiimote state..or insert some wait very simply...will lock thing work..any example code?
|
|
-
-
vanmelle52


- Joined on Thu, May 15 2008
- Posts 12
|
Re: About WiimoteChanged event
Brian Peek:You need to be logged in and have your text editor set to Standard or Enhanced
Well, I was logged in (obviously, my user name is there), and previously had one of those editors set. But just to be thorough, I've now switched my editor back to Enhanced. Should the message composer look any different? (It doesn't.)
If it's working, this sentence starts on a new line.
Here's another new line.
And yet my preview tab mashes them altogether.
Perhaps I need to use html tags. Now this really is a new line, but only because I tediously added br tags.
|
|
-
-
Brian Peek



- Joined on Fri, Mar 17 2006
- Niskayuna, NY
- Posts 1,127
|
Re: About WiimoteChanged event
Very strange. I'm using the Enhanced editor here in FireFox and I do get line breaks. Like that one. The enhanced editor should look different...the textbox should have a couple of toolbars and whatnot at the top.
|
|
-
-
vanmelle52


- Joined on Thu, May 15 2008
- Posts 12
|
Re: About WiimoteChanged event
You're right that the enhanced editor looks different in Firefox, which I fired up just now to test this out. And it's also there in IE.
However, I usually use Opera or Chrome these days, and I don't get an enhanced editor in either of those browsers. No biggie, I suppose -- with IE and Firefox you've probably got well over 90% coverage
|
|
-
-
-
Brian Peek



- Joined on Fri, Mar 17 2006
- Niskayuna, NY
- Posts 1,127
|
Re: About WiimoteChanged event
Anonymous:Hi,
i am also experiencing the same problem...i am trying to get acceleration data from the wii mote and calculate pitch and transfer data to lego nxt motor, the problem is that the events generated are so much that motor cannot follow the wiimote as before it attains the final position ..next event comes in..so is there a way that i can poll for the current value of the wiimote state..or insert some wait very simply...will lock thing work..any example code? You can not hook the event and simply poll the WiimoteState property off the Wiimote object at whatever interval you want.
|
|
Page 1 of 1 (14 items)
|
|
|