none
Weird behavior of the OneWay attribute in asynchronous invocation in fw3.5 RRS feed

  • Question

  • Hi.

    This's my first time with VS2008, and I was making a test:

    Code Snippet

            delegate void yeah();
            static yeah yeahhh;
            static Stopwatch sw = new Stopwatch();

            static void Main(string[] args)
            {
                sw.Start();
                yeahhh = new yeah(test);
                yeahhh.BeginInvoke(testend, null);
                Console.WriteLine(sw.ElapsedMilliseconds.ToString("00000") + " go!");

                Console.ReadKey(true);
            }

            [System.Runtime.Remoting.Messaging.OneWay()]
            static void test()
            {
                Console.WriteLine(sw.ElapsedMilliseconds.ToString("00000") + " inside");
                System.Threading.Thread.Sleep(5000);
            }

            static void testend(IAsyncResult ia)
            {
                yeahhh.EndInvoke(ia);
                Console.WriteLine(sw.ElapsedMilliseconds.ToString("00000") + " end");
            }




    This code, returns the following ouput:

    00001 end
    00002 go!
    00004 inside

    But, if I comment the OneWay attribute line this happens...

    00003 inside
    00003 go!
    05007 end

    Hummm... This seems that Thread.Sleep inside a asynchronous method doesn't work in a method with OneWay attribute... I read the documentation about OneWay and don't understand why runs on this way ...

    Any thoughs?

    Thans in advance.

    Kind regards.





    Saturday, February 2, 2008 8:49 PM

Answers

  • I think marking an thread method as oneway means that you want to start this method as a fire-and-forget style. You use this attribute to tell the CLR that you don't care about the running result of the funcion. Thus the callback function(testend) is immediately called by CLR(using a thread pool thread). And in the same time the test method started by BeginInvoke is still running.

    Here, the key point is that if you comment that attribute, you tell the CLR that you care about the result, thus CLR will wait for the returning of the test method, and after that it calls
    testend method.
    Monday, February 4, 2008 9:26 AM
  • No, the CLR does not ignore it. It just directly call the testend method without waiting the exit of test method. This means that the test and testend methods start at the same time. You can set a break point at the sleep line to see this. And I can see this behaviour on my machine.
    Monday, February 4, 2008 2:40 PM

All replies

  • I think marking an thread method as oneway means that you want to start this method as a fire-and-forget style. You use this attribute to tell the CLR that you don't care about the running result of the funcion. Thus the callback function(testend) is immediately called by CLR(using a thread pool thread). And in the same time the test method started by BeginInvoke is still running.

    Here, the key point is that if you comment that attribute, you tell the CLR that you care about the result, thus CLR will wait for the returning of the test method, and after that it calls
    testend method.
    Monday, February 4, 2008 9:26 AM
  • But.. altough I don't care about the running result... why CLR ignore the Thread.Sleep() ? (see the timings)

     

    Is quite weird Stick out tongue

     

    Kind regards.

     

     

     

    Monday, February 4, 2008 1:30 PM
  • No, the CLR does not ignore it. It just directly call the testend method without waiting the exit of test method. This means that the test and testend methods start at the same time. You can set a break point at the sleep line to see this. And I can see this behaviour on my machine.
    Monday, February 4, 2008 2:40 PM
  • humm... I understand it.

    Thanks!
    Monday, February 4, 2008 7:27 PM