2013-08-01 38 views
1

我有一個主頁它獲取新聞和n背景負載從網絡上的其他信息:BeginGetResponse在背景

BackgroundWorker bw = new BackgroundWorker(); 
     bw.WorkerSupportsCancellation = true; 
     bw.DoWork += new DoWorkEventHandler(GetContacts); 
     bw.RunWorkerAsync(); 

private void GetContacts(object sender, DoWorkEventArgs e) 
    { 
     BackgroundWorker bw = new BackgroundWorker(); 
     bw.WorkerSupportsCancellation = true; 
     bw.DoWork += new DoWorkEventHandler(getServiceContactData); 
     BackgroundWorker worker = sender as BackgroundWorker; 
     if ((worker.CancellationPending == true)) 
     { 
      e.Cancel = true; 
     } 
     long cont = 0; 

     //This made to pause a thread for a long time 

     do 
     { 
      cont++; 
     } 
     while (cont != 999999999); 
     getServiceContactData(); 
} 

private void getServiceContactData() 
    { 
     HttpWebRequest request = HttpWebRequest.CreateHttp(GS.BACKEND_HOST + "sfbsfb"); 
     request.BeginGetResponse(new AsyncCallback(HandleResponseContacts), request); 
    } 

private void HandleResponseContacts(IAsyncResult result) 
    { 
     HttpWebRequest request = result.AsyncState as HttpWebRequest; 
     if (request != null) 
     { 
      using (WebResponse response = request.EndGetResponse(result)) 
      { 
       using (StreamReader reader = new StreamReader(response.GetResponseStream())) 
       { 
        string JSON = reader.ReadToEnd(); 
        if (JSON == "") 
        { 
         NoItems = true; 
        } 
        IsolatedStorage IS = new IsolatedStorage(); 
        IS.Save("ContactsStorage.txt", JSON); 
        IS.SaveContactsRetrieveDate(DateTime.Now.ToString("dd.MM.yyyy")); 
        Deployment.Current.Dispatcher.BeginInvoke(() => 
        { 
         //MessageBox.Show("got"); 
        }); 
       } 
      } 
     } 

    } 

在通訊錄頁面有:

string contactsRetriveDate = ""; 
     DateTime a; 
     string now = DateTime.Now.ToString("MM/dd/yyyy"); 
     string then = ""; 
     do 
     { 
      contactsRetriveDate = IS.ReadContactsRetriveDate(); 
      if (contactsRetriveDate != "") 
      { 
       a = DateTime.ParseExact(contactsRetriveDate, "dd.MM.yyyy", System.Globalization.CultureInfo.InvariantCulture); 
       a.ToString("MM/dd/yyyy"); 
      } 
     } 
     while(then!=now); 
     MessageBox.Show("Estj"); 

此代碼檢查上次更新聯繫人的日期,當現在=時,它顯示消息框。

目的:

目的是downlad聯繫人數據的背景,所以,有沒有那麼多時間去等待聯繫人頁面...換句話說,開始接觸之前獲取聯繫人數據頁面打開。

但是代碼沒有HandleResponseContacts,它只是當我瀏覽到其他頁面沒有輸入功能。

+0

你不需要使用後臺工作進行定期下載,你可以使用定時器來觸發下載。 'BeginGetResponse'已經在另一個線程上,現在你基本上已經浪費了整個線程。這會浪費CPU時間並浪費電池多一點。 –

回答

0

發現我錯了。

錯誤的方法是讓聯繫人頁面忙於主線:

我應該這樣做:

string contactsRetriveDate = ""; 
    DateTime a; 
    string now = DateTime.Now.ToString("MM/dd/yyyy"); 
    string then = ""; 
    do 
    { 
     contactsRetriveDate = IS.ReadContactsRetriveDate(); 
     if (contactsRetriveDate != "") 
     { 
      a = DateTime.ParseExact(contactsRetriveDate, "dd.MM.yyyy", System.Globalization.CultureInfo.InvariantCulture); 
      a.ToString("MM/dd/yyyy"); 
     } 
    } 
    while(then!=now); 
    MessageBox.Show("Estj"); 

在後臺工作。

由於主線程忙,代碼無法工作。