3

I am creating a custom statemachine and in order to be determinist, I have to "synchronise" my transitions. I'm not sure about the word "synchronize" but what I want is that when I call a function (through EventHandler), the system is like frozen before I can call another function (through EventHandler too).

It's kinda hard to explain it precisely in english but I think you know what I mean...

I was thinking about Threading but I'd REALLY like to avoid this...

5
  • 1
    Please post your code so we can see what you are talking about. Commented Nov 8, 2011 at 13:34
  • 2
    If you don't use multithreading it already works in a way you want. Untill the execution of event handler is not finished the other function can not be riched. Commented Nov 8, 2011 at 13:37
  • the fact is that my transitions from a state to another is triggered by "Events" (like listeners in java) and I don't know how C# works about this. I mean, does there is a sort of event-stack and event are triggered 1 by 1 when C# realise that every function calls are over for the previous event ? Or does C# triggers listeners in mutiple threads ? Commented Nov 8, 2011 at 13:44
  • 1
    @GuillaumeCogranne: Events in C# are proceed in sequence. So, if you do not call inside any multithreaded code, they will block execution of your program till the end of its execution. Commented Nov 8, 2011 at 19:20
  • I did a try with a Thread.sleep in my "ActionListener" and noticed what you're saying ! thx for the info Commented Nov 9, 2011 at 9:46

3 Answers 3

1

If you are looking to emulate the effect of the "synchronized" keyword from java, the best way is probably to wrap the entire method code inside

lock(this)
{
    // code
}
Sign up to request clarification or add additional context in comments.

6 Comments

It is not recommended to lock(this), btw
I know, but this would be the best equivalent to the java "synchronized" methods.
but the only person mentioning java is yourself... synchronization goes far beyond something that happens to be a keyword in a specific language
Look at the post title, as well as this: "the system is like frozen before I can call another function". This strongly suggests that he wants to have mutually exclusive method calls in my opinion. Anyway, we should probably wait for a clarification from the OP.
@Tudor: IMO, the equivalent to Java's synchronized is to decorate the method with [MethodImpl(MethodImplOptions.Synchronized)] (this in turn is equivalent to placing the entire method body inside a lock (this) { ... } block.
|
1

Not sure if that's what you are looking for, but C# iterator blocks are essentially state machines.

1 Comment

I found information about it but he doesn't fit with what I wanna do :/
0

Synchronization is when you're in a multi-threaded environment and you need to make access to resources by the threads synchronized (1 at a time). This ensures unpredictable results are not achieved when threads are changing resources while other threads are trying to access them. There are many constructs available to you in C# to handle synchronization. It all depends on what your threads are trying to accomplish.

Here is a link from MSDN that shows some simple examples: http://msdn.microsoft.com/en-us/library/ms173179.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.