【问题标题】:photoChooserTask cause SocketExceptionphotoChooserTask 导致 SocketException
【发布时间】:2012-12-12 05:30:30
【问题描述】:

我想像这样使用 PhotoChooserTask 返回一张照片:

 private void getimage_Click(object sender, EventArgs e)
     {
         photoChooserTask = new PhotoChooserTask();
         photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);

         try
         {
             photoChooserTask.Show();
         }
         catch (System.InvalidOperationException ex)
         {
             MessageBox.Show("An error occurred.");
         }

     }
     void photoChooserTask_Completed(object sender, PhotoResult ee)
     {
         if (ee.TaskResult == TaskResult.OK)
         {

             BitmapImage bmp = new BitmapImage();
             bmp.SetSource(ee.ChosenPhoto);

             if (ee.TaskResult == TaskResult.OK && ee.Error == null)
             {

                     WriteableBitmap wb = new WriteableBitmap(bmp);

                     notes.Add(new chatinfo() { sendimage = bmp });
                     noteListBox.ItemsSource = null;
                     noteListBox.ItemsSource = notes;
             }
           }

       }

但每次程序到达这里时:“bmp.SetSource(ee.ChosenPhoto);”将调用 SocketException。

private void OnRecieveFrom()
     {
         var receiveArgs = new SocketAsyncEventArgs();
         receiveArgs.RemoteEndPoint = this.IPEndPoint;
         receiveArgs.SetBuffer(new Byte[MAX_BUFFER_SIZE], 0, MAX_BUFFER_SIZE);
         var strBdr = new StringBuilder();
         receiveArgs.Completed += (__, result) =>
         {

             string message = CreateMessage(result);

                Deployment.Current.Dispatcher.BeginInvoke(() =>
                 {
                     this.RaiseReceived(message);

                 });
                 socket.ReceiveFromAsync(receiveArgs);    



         };

             socket.ReceiveFromAsync(receiveArgs);


     }

SocketException 由“socket.ReceiveFromAsync(receiveArgs);”调用 我只是想从手机中获取一张照片,没有发送或接收操作。我不知道为什么调用了接收函数。

应用跳转相册时是否会丢失socket通信(socket的“RemoteEndPoint”值变为null)? p.s. “socket”是“Socket”类的一个对象。 如果是这样,我是否应该在每次应用跳出时重新创建“套接字”?

谢谢!

【问题讨论】:

  • 好吧,你不是写了OnRecieveFrom方法的人吗?您应该能够追溯调用此方法的代码,并从那里了解发生了什么。我们没有足够的代码自己弄清楚,但我猜你正在使用来自OnNavigatedTo 事件的套接字或其他东西。或者由于选择器而取消了正在进行的套接字操作(显示选择器时您的应用程序已停用)。
  • @KooKiz 'OnRecieveFrom()' 在客户端(Windows 手机)连接到服务器时首先被调用。然后,如果有任何消息到达,'receiveArgs.Completed' 中的代码将执行。此外,在发生 SocketException 之前,socket 的值 'RemoteEndPoint' 变为 null。

标签: c# windows-phone-7


【解决方案1】:

一旦调用 PhotoChooserTask,您的应用就会被快速应用切换(甚至可能被墓碑化)。

无论哪种方式,您的套接字都将关闭。当您的应用再次激活时,您必须重新打开套接字。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-29
    • 2011-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多