I would like to implement one way call method in C#. I know in Web service we can implement using the ONE Way attribute. I have gone through some Async calls with Delegate. If any one have some sample code which I can use for the same.
Asked
Active
Viewed 1,795 times
1
-
4Can you be a little more specific...I'm not sure anybody is going to know what you're talking about. – Justin Niessner Sep 10 '09 at 15:44
2 Answers
4
Well, perhaps the easiest way to emulate a one-way call is to push it onto the thread-pool?
ThreadPool.QueueUserWorkItem(delegate { DoSomeStuff("abc"); });
Although the above suffers a bit from issues with exceptions, it would be easy enough to wrap:
(edit: oops, borked the exception handling! fixed...)
public static void OneWay(Action action) {
if (action == null) throw new ArgumentNullException("action");
ThreadPool.QueueUserWorkItem(delegate {
try { action(); }
catch (Exception ex) {
Trace.WriteLine(ex);
}
});
}
...
OneWay(() => DoSomeStuff("abc"));

Marc Gravell
- 1,026,079
- 266
- 2,566
- 2,900
-
Following a link from one of the comments turns up a Phil Haack post which is essentially the technique describe in this fine answer, with some background and discussion of fire and forget delegates. The post is titled "Asynchronous Fire and Forget With Lamdas" OneWay messaging is commonly known as FireAndForget, so this answer is right on the money. Phil's sytax is a little more concise if you are using C# 3.0. Either way, I'd read the article. http://haacked.com/archive/2009/01/09/asynchronous-fire-and-forget-with-lambdas.aspx – Nathan Sep 10 '09 at 17:45
0
You can wrap the async delegate in the method call
public void MyOneWay()
{
//you can replace the anonymous function with any function you define, if you want
var d = delegate(int a) { Console.Write(a);};
d.BeginInvoke();
}
Note: This is pseudocode.

Alan
- 45,915
- 17
- 113
- 134
-
1MSDN is very specific that BeginInvoke should be paired with EndInvoke, with the sole exception of Control.BeginInvoke. – Marc Gravell Sep 10 '09 at 15:55
-
@Marc is that because of the way you are _supposed_ to use it, or because behind the scenes it can get upset? I like the fire-and-forget ability delegates provide... – cjk Sep 10 '09 at 15:57
-
I genuinely couldn't say for sure. The fact that it is called out with a warning sign in the documentation was reason enough for me ;-p – Marc Gravell Sep 10 '09 at 15:58
-
I haven't had any problems so far, but that could be becuase I capture exceptions at the top level and make sure nothing gets lost... To do it right, would it be best to use a callback to call EndInvoke, as the main aim for running like this is so that I can return my main thread asap with a Guid that allows the UI to track progress of a long running operation? – cjk Sep 10 '09 at 16:02
-
@ck: that is certainly what MSDN suggests, but see http://stackoverflow.com/questions/1274276/must-every-begininvoke-be-followed-by-an-endinvoke or http://stackoverflow.com/questions/1269983/should-one-always-call-endinvoke-a-delegate-inside-asynccallback – Marc Gravell Sep 10 '09 at 16:09
-
-
Search on EndInvoke sez that you should most definitely call EndInvoke. If you inspect the IAsyncResult result, it has a boolean property called "EndInvokeCalled" – Alan Sep 10 '09 at 16:12