0

我正在尝试使用objectsand接收。我不断收到,每个人都被派往一项新任务。我选择不使用,因为它太贵了。问题是,如果我一次只收到 1 一切都很好,但如果我试图收到超过 1 ,几秒钟后我得到: TCPC#serializationobjectsobjectthreadsobjectobject"the input stream is not a valid binary format. the starting contents (in bytes) are: ..."

这是我的听力功能:

public void Listen()
        {
            try
            {
                TcpObject tcpObject = new TcpObject();
                IFormatter formatter = new BinaryFormatter();

                bool offline = true;
                Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                    new Action(() => offline = Offline));

                while (!offline)
                {
                    tcpObject = (TcpObject)formatter.Deserialize(serverStream);

                    if (tcpObject.Command == Command.Transfer)
                    {
                        #region Task
                        Task.Factory.StartNew(() =>
                        {
                            SentAntenna sentAntenna = (SentAntenna)tcpObject.Object;

                            string antennaName = sentAntenna.Name;

                            if (MainWindow.SpectrumList.ContainsKey(antennaName))
                            {
                                PointCollection pointCollection = new PointCollection();
                                float minChan = sentAntenna.Min;
                                float maxChan = sentAntenna.Max;

                                if (MainWindow.SpectrumList[antennaName].spectrumViewModel.AbsoluteMinimum == -1)
                                {
                                    Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                                        new Action(delegate
                                        {
                                            MainWindow.SpectrumList[antennaName].spectrumViewModel.AbsoluteMinimum = minChan;
                                            MainWindow.SpectrumList[antennaName].spectrumViewModel.AbsoluteMaximum = maxChan;
                                            MainWindow.SpectrumList[antennaName].spectrumViewModel.TBMinRange = minChan.ToString();
                                            MainWindow.SpectrumList[antennaName].spectrumViewModel.TBMaxRange = maxChan.ToString();
                                            MainWindow.SpectrumList[antennaName].spectrumViewModel.MinRange = minChan;
                                            MainWindow.SpectrumList[antennaName].spectrumViewModel.MaxRange = maxChan;
                                            MainWindow.SpectrumList[antennaName].spectrumViewModel.UpdateRange();
                                        }));
                                }

                                float gap = maxChan - minChan;
                                foreach (Frequency f in sentAntenna.Frequencies)
                                {
                                    float chan = ((f.Channel - minChan) / gap) * 310;
                                    float inten = ((f.Intensity - 1) / 599) * 100;
                                    pointCollection.Add(new Point(chan, inten));
                                }

                                pointCollection.Freeze();
                                Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                                    new Action(() => MainWindow.SpectrumList[antennaName].spectrumViewModel.AllAntennaPoints = pointCollection.Clone()));
                            }
                            Thread.Sleep(50);
                        });
                        #endregion
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message); // raise an event
            }
        }

我究竟做错了什么?

4

2 回答 2

0

尝试移动

SentAntenna sentAntenna = (SentAntenna)tcpObject.Object;

到 StartNew() 之前的行。我相信这将解决您的问题。

我认为您不希望同时访问 tcpObject,因为它对所有任务都是全局的。

或者,您可以在 while 循环内实例化 TcpObject,然后将其保持在每个任务的本地。

于 2013-01-29T18:05:55.827 回答
0

这个错误有几个原因:

  1. 当两个对象同时写入一个连接时

  2. 当 serverStream 出现问题时:流已接收到部分数据或接收到 0 长度数据

  3. 您同时访问 tcpObject。这是个坏主意。

于 2013-01-29T18:28:06.793 回答