我有两种方法,我必须在页面启动时并行运行。
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 :(得分: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));
}