2017-08-05 61 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

相关问题