【发布时间】:2015-11-19 07:04:32
【问题描述】:
我正在使用 c# 和 Wpf 编写程序。我调用在不同线程中执行计算的方法以保持应用程序响应,但是当我尝试从主线程传递一个字段作为来自不同线程的这些方法的参数时,我得到一个异常。我试过使用 Dispatcher 它没有解决我的问题。这是在不同线程中调用的方法:
private void Compare()
{
//gets a hashcode using getpixel string instance and checks if image is cropped using CompareStrings method of check instance
BitmapImage Image1 = new BitmapImage();
BitmapImage Image2 = new BitmapImage();
Action a = () =>
{
Image1.UriSource = image1.UriSource;
Image2.UriSource = image2.UriSource;
};
Dispatcher.Invoke(a);
hashcode1 = getPixel.GetHashCode(Image1);
hashcode2 = getPixel.GetHashCode(Image2);
bool match = check.CompareStrings(hashcode2, hashcode1);
if (match)
{
MessageBox.Show("The image is Cropped");
}
else
{
MessageBox.Show("These are two different images");
}
}
下面是调用线程的代码:
private void CompareButton_Click(object sender, RoutedEventArgs e)
{
Thread workerThread = new Thread(Compare);
workerThread.Start();
}
这是一个 GetHashCode 方法的代码,这就是我得到异常的地方:
public List<string> GetHashCode(BitmapImage bitmap)
{//takes a bitmap and translates it into the hashcode list
List<string> hashCode= new List<string>();
int stride = bitmap.PixelWidth * (bitmap.Format.BitsPerPixel / 8);
for (int i = 0; i < bitmap.PixelHeight; i++)//divides an image into rows
{
string row="";
for (int x = 0; x < bitmap.PixelWidth; x++)//iterates through each pixel in the row
{
byte[] pixel = new byte[bitmap.PixelHeight];//holds color values of a single pixel
bitmap.CopyPixels(new Int32Rect(x, i, 1, 1), pixel, stride, 0);//assigns color values of a single pixel to the pixel array
Color singlePixel = new Color();//creates new color objects and assigns the color values found in pixel array to it
singlePixel.B = pixel[0];
singlePixel.G = pixel[1];
singlePixel.R = pixel[2];
singlePixel.A = pixel[3];
row += singlePixel.GetHashCode().ToString();//converst the color value into the hashcode and converts it to the string
}
hashCode.Add(row);
UpdatePRogress();
}
如你所见,我尝试使用 Dispatcher 方法,但没有成功
【问题讨论】:
-
什么是异常,它在哪一行?
-
就个人而言,我建议您创建一个您正在使用的 BitmapImages 的副本(可能是
Bitmap对象)并在后台线程中使用它们。
标签: c# wpf multithreading