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