开发者

FireandForget delegate - c#

I came across this link, link text

In the article, where the author has definition of this method

static void WriteIt(string first, string second, int num)

I changed that in my test app to this

static void WriteIt(CustomerObject Customer)
{
  fileIO.CreateFile(XMLUtil.Serialize(Customer));
}

Where

 public static string Serialize(object o)
    {
        System.Xml.Serialization.XmlSerializerNamespaces ns = new System.Xml.Serialization.XmlSerializerNamespaces();
        ns.Add("", "");

        return Serialize(o, ns);

    }

    public static string Serialize(object o, XmlSerializerNamespaces ns)
    {
        try
        {
            using (System.IO.MemoryStream m = new System.IO.MemoryStream())
            {

                //serialize messagelist to xml
                XmlSerializer serializer = new XmlSerializer(o.GetType(), "");

                if (ns != null)
                    serializer.Serialize(m, o, ns);
                else
                    serializer.Serialize(m, o);

                m.Position = 0;
                byte[] b = new byte[m.Length];
                m.Read(b, 0, b.Length);

                return System.Text.UTF8Encoding.UTF8.GetString(b);
            }
        }
        catch (Exception ex)
        {
            return "Ex = " + ex.ToString();
        }
    }

This method always gives an exception

static void EndWrapperInvoke (IAsyncResult ar)
    {
        wrapperInstance.EndInvoke(ar); 
        ar.AsyncWaitHandle.Close();
    }

Stacktrace:

Server stack trace:

   at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Delegate.DynamicInvokeImpl(Object[] args)
   at System.Delegate.DynamicInvoke(Object[] args)
   at SRC.FileMover.ThreadUtil.InvokeWrappedDelegate(Delegate d, Object[] args)
   at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInContext, Object[]& outArgs)
   at System.Runtime.Remoting.Messaging.StackBuilderSink.PrivateProcessMessage(RuntimeMethodHandle md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInContext, Object[]& outA开发者_StackOverflowrgs)
   at System.Runtime.Remoting.Messaging.StackBuilderSink.AsyncProcessMessage(IMessage msg, IMessageSink replySink)

Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.EndInvokeHelper(Message reqMsg, Boolean bProxyCase)
   at System.Runtime.Remoting.Proxies.RemotingProxy.Invoke(Object NotUsed, MessageData& msgData)
   at SRC.FileMover.ThreadUtil.DelegateWrapper.EndInvoke(IAsyncResult result)
   at SRC.FileMover.ThreadUtil.EndWrapperInvoke(IAsyncResult ar)
   at System.Runtime.Remoting.Messaging.AsyncResult.SyncProcessMessage(IMessage msg)
   at System.Runtime.Remoting.Messaging.StackBuilderSink.AsyncProcessMessage(IMessage msg, IMessageSink replySink)
   at System.Runtime.Remoting.Proxies.AgileAsyncWorkerItem.DoAsyncCall()
   at System.Runtime.Remoting.Proxies.AgileAsyncWorkerItem.ThreadPoolCallBack(Object o)
   at System.Threading._ThreadPoolWaitCallback.WaitCallback_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallbackInternal(_ThreadPoolWaitCallback tpWaitCallBack)
   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(Object state)

UPDATE 1: Trying to run my app and get the full exception. It seems like its happening at different locations. I will repost my ? again shortly. I think it may be wise if I can post my application. Can i upload a .zip file or is it better to just post the .cs code that I am using?


If you want to fire and forget a method, you should use the ThreadPool:

ThreadPool.QueueUserWorkItem(o => SomeMethod(someParameter), null);


If you just want to fire and forget a method, why not just declare an anonymous method with an Action?

Action<CustomerObject> fireAndForget = delegate(CustomerObject obj) 
{
     fileIO.CreateFile(XMLUtil.Serialize(obj));
};

fireAndForget.BeginInvoke(new CustomerObject()); // Don't need to End Invoke
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜