2017-08-05 58 views
-1

我有兩種方法,當頁面啓動時我必須並行運行。當頁面啓動時並行運行兩種方法

public Page1() 
{ 
    InitializeComponent(); 
    loadtest(); 
    loadtest2(); 
} 

這些方法從我的MySQL數據庫中獲取圖像並將其存儲在一個可以稍後調用的變量中。

public void loadtest() 
{ 
    string query = "select*from question where id='" + 1 + "'"; 
    MySqlConnection conDataBase = new MySqlConnection(constring); 
    MySqlCommand cmdDataBase = new MySqlCommand(query, conDataBase); 
    MySqlDataReader myReader; 

    try 
    { 
     conDataBase.Open(); 
     myReader = cmdDataBase.ExecuteReader(); 

     while (myReader.Read()) 
     { 
      string qid = myReader.GetInt32("id").ToString(); 

      byte[] imgg1q1 = (byte[])(myReader["question"]); 
      byte[] imgg2q1 = (byte[])(myReader["opt1"]); 
      byte[] imgg3q1 = (byte[])(myReader["opt2"]); 
      byte[] imgg4q1 = (byte[])(myReader["opt3"]); 
      byte[] imgg5q1 = (byte[])(myReader["opt4"]); 

      MemoryStream mstreamq1 = new MemoryStream(imgg1q1); 
      MemoryStream mstream1q1 = new MemoryStream(imgg2q1); 
      MemoryStream mstream2q1 = new MemoryStream(imgg3q1); 
      MemoryStream mstream3q1 = new MemoryStream(imgg4q1); 
      MemoryStream mstream4q1 = new MemoryStream(imgg5q1); 

      q1.BeginInit(); 
      q1.StreamSource = mstreamq1; 
      q1.CacheOption = BitmapCacheOption.OnLoad; 
      q1.EndInit(); 

      q1opt1.BeginInit(); 
      q1opt1.StreamSource = mstream1q1; 
      q1opt1.CacheOption = BitmapCacheOption.OnLoad; 
      q1opt1.EndInit(); 

      q1opt2.BeginInit(); 
      q1opt2.StreamSource = mstream2q1; 
      q1opt2.CacheOption = BitmapCacheOption.OnLoad; 
      q1opt2.EndInit(); 

      q1opt3.BeginInit(); 
      q1opt3.StreamSource = mstream3q1; 
      q1opt3.CacheOption = BitmapCacheOption.OnLoad; 
      q1opt3.EndInit(); 

      // Assign the Source property of your image 
      option_3.Source = q1opt3; 

      q1opt4.BeginInit(); 
      q1opt4.StreamSource = mstream4q1; 
      q1opt4.CacheOption = BitmapCacheOption.OnLoad; 
      q1opt4.EndInit(); 
     } 

     conDataBase.Close(); 
    } 
    catch 
    { 
    } 
} 

public void loadtest2() 
{ 
    string query = "select*from question where id='" + 2 + "'"; 
    MySqlConnection conDataBase = new MySqlConnection(constring); 
    MySqlCommand cmdDataBase = new MySqlCommand(query, conDataBase); 
    MySqlDataReader myReader; 

    try 
    { 
     conDataBase.Open(); 
     myReader = cmdDataBase.ExecuteReader(); 

     while (myReader.Read()) 
     { 
      string qid = myReader.GetInt32("id").ToString(); 

      byte[] imgg1 = (byte[])(myReader["question"]); 
      byte[] imgg2 = (byte[])(myReader["opt1"]); 
      byte[] imgg3 = (byte[])(myReader["opt2"]); 
      byte[] imgg4 = (byte[])(myReader["opt3"]); 
      byte[] imgg5 = (byte[])(myReader["opt4"]); 

      MemoryStream mstream = new MemoryStream(imgg1); 
      MemoryStream mstream1 = new MemoryStream(imgg2); 
      MemoryStream mstream2 = new MemoryStream(imgg3); 
      MemoryStream mstream3 = new MemoryStream(imgg4); 
      MemoryStream mstream4 = new MemoryStream(imgg5); 

      q2.BeginInit(); 
      q2.StreamSource = mstream; 
      q2.CacheOption = BitmapCacheOption.OnLoad; 
      q2.EndInit(); 

      q2opt1.BeginInit(); 
      q2opt1.StreamSource = mstream1; 
      q2opt1.CacheOption = BitmapCacheOption.OnLoad; 
      q2opt1.EndInit(); 

      q2opt2.BeginInit(); 
      q2opt2.StreamSource = mstream2; 
      q2opt2.CacheOption = BitmapCacheOption.OnLoad; 
      q2opt2.EndInit(); 

      q2opt3.BeginInit(); 
      q2opt3.StreamSource = mstream3; 
      q2opt3.CacheOption = BitmapCacheOption.OnLoad; 
      q2opt3.EndInit(); 

      q2opt4.BeginInit(); 
      q2opt4.StreamSource = mstream4; 
      q2opt4.CacheOption = BitmapCacheOption.OnLoad; 
      q2opt4.EndInit(); 
     } 

     conDataBase.Close(); 
    } 
    catch 
    { 
    } 
} 

我曾嘗試在互聯網上提供的方法,但是當我在並行運行它們不顯示的問題。我已經使用線程來運行這些方法,但變量變爲空並且不顯示任何內容。 我也用過Parallel.Invoke(() => loadtest(),() =>loadtest2()); 但這也是沒用的;它保存了第一個功能的值,在上述情況下爲loadtest(),並且不保存第二個功能的值,即loadtest2()

回答

0

單程

public Page1() 
    { 
     InitializeComponent(); 
     var test1 = Task.Run(() => loadtest()); 
     var test2 = Task.Run(() => loadtest2()); 
     Task.WhenAll(test1,test2); 
    } 

另一種方式是

public Page1() 
{ 
    InitializeComponent(); 
    Parallel.Invoke(new Action(loadtest),new Action(loadtest2)); 
} 
+0

我嘗試這兩種方法,但該方法的任務不顯示的問題,我不知道的可變因素是空的,當我使他們顯示。當我簡單地寫出它們時,它們工作得很好....比如............. loadtest(); loadtest2(); .............和下​​一個並行調用它保存圖像的第一次寫入的方法在上面的情況下,它的loadtest(),但再次爲loadtest2它變空 – Saurabh

+0

嘗試鎖定可能是一些干擾代碼。但我認爲代碼正在並行運行 – Ramankingdom

+0

是的,我已經測試了這個方法....代碼運行並行,但它只是沒有將值保存在變量.....我怎樣才能鎖定值... – Saurabh

相關問題