当我们将Guid对象作为用户状态传递时,如何将其他数据传递给回调?

时间:2013-06-29 05:42:06

标签: c# web-services asynchronous webservice-client

我想将Property类解析为callback方法,因此我可以指定在callback完成时应执行的操作。

    // I have a webservice proxy created , in that webservice there is a method called Test which returns an integer value. 
       Myserver.servic service = new  Myserver.servic() ; 

        // In here i am calling Test webmethod asyncronously 
           public void test(ccc sk , int id )
            {
                service.TestCompleted -= testCompleted;
                service.TestCompleted += testCompleted;
               //When I am caling the TestAsyn method I am parsing Guid.NewGuid() 
               //Because I call the same method many times simultaneously.
               service.TestAsync(Guid.NewGuid ());
               //But this is not how i  want to call it 
               //I meant i want to parse some additional data to Callback method. 
               //In socket we can do it using AsyncState , but in WebService we cann't use UserState to parse custom data 
               //Because we have to parse a Guid object to it, otherwise it will generate the following error message 
               //Error: **There was an error during asynchronous processing. Unique state object is required for multiple asynchronous simultaneous.**   

               // Inorder to prevent getting this error, I have to parse new guid object as UserState 
               // But I also need to parse some data to Callback method  
               // Smething like this 
               //I.e 
               MyClass m = new MyClass () ;
               m.s = "Hello" ; 
               m.i = 292; 
               m.enable = true ; 
               service.TestAsyn(m) ; // I want to parse data this way
               // But if I parse data to call back method this way I will get that error again 
               // Can someone please help me ? 
          }
 // When Call back completed I want to use something like following
            public void TestCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Error != null)
                MessageBox.Show(e.Error.ToString());


                if (e.Error == null)
                {
                    MyClass  m = (MyClass )e.UserState;
                    MessageBox.Show(m.s);
                }

        } 
  } 

1 个答案:

答案 0 :(得分:-1)

创建方法重载[未测试]

void by_server_AddFriendCompleted(int user,int friend, Guid NGuid, object sender, AsyncCompletedEventArgs e)
        {
            //I want soemthing like e.Parameter ? , like we use AsyncState in sockets
            // ex :  Myperopetyclass c = e.Parameter , this is what i want  

            if (e.Error != null)
               //error
        }