2014-02-16 34 views
1

我有一个问题,我无法自行解决。访问线程内列表中的对象属性

我有一个具有“价格”属性的“产品”的ObservableCollection。每一秒产品的价格都会发生变化。这是我的服务器部分。 我有一个窗口,带有一些文本框,它绑定在price属性上。

另一方面,我有一个客户。客户需要获得所有产品的价格。

所以,首先,我的客户端连接到我的服务器(这里没有问题)。它发送一条消息到服务器,服务器接收它。

我的问题是在这里:价格属性改变每秒的价值,但在我的线程,我不能得到新的价值...

这里我的代码: - 产品:

private volatile float price; 
public float Price 
{ 
    get { return price; } 
    set 
    { 
     price = value; 
     notifyPropertyChanged("Price"); 
    } 
} 

public Product(int time) 
{ 
    timer = new Timer(time); 
    timer.Elapsed += timer_Elapsed; 
    timer.Start(); 
} 

void timer_Elapsed(object sender, ElapsedEventArgs e) 
{ 
    this.Price++; 
} 
  • 我的服务器:

    private ObservableCollection<product> listGroupProduct; 
    public MainWindow() 
    { 
        Thread thread; 
        InitializeComponent(); 
        this.DataContext = this; 
    
        // Create the server 
        thread = new Thread(() => createServer(ref listGroupProduct)); 
        thread.Start(); 
    } 
    public static void createServer(ref ObservableCollection<Product> list) 
    { 
        string client = ""; 
        try 
        { 
         IPAddress ipAdress = IPAddress.Parse("192.168.1.50"); 
         TcpListener listener = new TcpListener(ipAdress, 1220); 
    
         listener.Start(); 
         socket = listener.AcceptSocket(); 
    
         // Receive client name 
         client = ReceiveMessage(100); 
         MessageBox.Show("New client connected : " + client); 
    
         // Send number of products 
         SendMessage(list.Count.ToString()); 
    
         // Get articles request from a client 
         ReceiveMessage(8); 
    
         // Send all articles 
         while (true) 
         { 
          for (int i = 0; i < 3; i++) 
          { 
           if (articlesString != "") 
            articlesString += "|"; 
           articlesString += list[i].Price + ";"; 
          } 
          byte[] bytes = new byte[list.Count * 50]; 
          bytes = System.Text.Encoding.ASCII.GetBytes(articlesString.ToCharArray()); 
          socket.Send(bytes); 
         } 
        } 
        catch (Exception e) 
        { 
         MessageBox.Show(e.Message); 
        } 
    } 
    
    private static void SendMessage(string p) 
    { 
        byte[] bytes = new byte[p.Length]; 
        bytes = System.Text.Encoding.ASCII.GetBytes(p.ToCharArray()); 
    
        socket.Send(bytes); 
    } 
    
    private static string ReceiveMessage(int p) 
    { 
        string tmp = ""; 
    
        byte[] b = new byte[p]; 
        int k = socket.Receive(b); 
        for (int i = 0; i < k; i++) 
         tmp += Convert.ToChar(b[i]); 
    
        return tmp; 
    } 
    

我的客户:

private StreamSocket streamSocket; 
public string Server = "192.168.1.89"; 
public int Port = 1220; 

IInputStream inputStream; 
IOutputStream outputStream; 

public MainPage() 
{ 
    this.InitializeComponent(); 
    CreateSocket(); 
} 

void timer_Tick(object sender, object e) 
{ 
    SendMessage("articles"); 
    toto.Text = "Message send : articles"; 

    GetAllArticles(); 
} 

private async void GetAllArticles() 
{ 
    toto.Text = await GetMessage(50); 
    toto.Text = "Waiting articles..."; 
    toto.Text = await GetMessage(articlesNumber * 50)); 
} 

private async Task CreateConnection() 
{ 
    SendMessage("tablet"); 
    toto.Text = "message send : tablet"; 

    articlesNumber = int.Parse(await GetMessage(1)); 
    toto.Text = "Number articles : " + articlesNumber.ToString(); 
} 

private async void CreateSocket() 
{ 
    DispatcherTimer timer = new DispatcherTimer(); 

    streamSocket = new StreamSocket(); 
    await streamSocket.ConnectAsync(new HostName(Server), Port.ToString()); 

    inputStream = streamSocket.InputStream; 
    outputStream = streamSocket.OutputStream; 

    timer.Interval = new TimeSpan(0, 0, 1); 
    timer.Tick += timer_Tick; 

    // Envoi du nom et réception du nombre d'articles 
    await CreateConnection(); 

    // Réception de tous les articles chaque secondes 
    SendMessage("tablet"); 
    timer.Start(); 
} 

private async void SendMessage(string message) 
{ 
    IBuffer buffer = CryptographicBuffer.ConvertStringToBinary(message, BinaryStringEncoding.Utf8); 
    await outputStream.WriteAsync(buffer); 
} 

private async Task<string> GetMessage(int size) 
{ 
    byte[] tmp = new byte[size]; 
    IBuffer buffer1 = CryptographicBuffer.CreateFromByteArray(tmp); 

    toto.Text = "Waiting message... (size : " + size.ToString() + ")"; 
    await inputStream.ReadAsync(buffer1, (uint)size, InputStreamOptions.None); 
    toto.Text = "Message received !"; 

    return CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, buffer1); 
} 

(顺便说一句:“TOTO”是一个文本框,我使用的调试:))

你有一个想法,为什么我的客户收到很好的第一个值,但是当,在我的服务器端,价值发生了变化,我的客户继续获得相同的价值而不是新的价值?

回答

0

它看起来像服务器有无限循环,并不断重复发送相同的数据。在“发送所有文章”评论之后,CreateServer立即有无限循环。

+0

是的,我知道,但我不知道如何改变它。我希望我的服务器发送新闻数据,而不是所有的时间都一样。 – carndacier

+0

您的服务器需要一个接收线程,并且在接收线程收到一个完整的消息后,它需要发送适当的响应。 – RamblinRose

+0

我明白了。但问题是一样的:我怎样才能得到这个线程内的列表的价值? – carndacier