Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从主UI传递字段作为来自不同线程的方法的参数_C#_Wpf_Multithreading - Fatal编程技术网

C# 如何从主UI传递字段作为来自不同线程的方法的参数

C# 如何从主UI传递字段作为来自不同线程的方法的参数,c#,wpf,multithreading,C#,Wpf,Multithreading,我正在用c#和Wpf编写一个程序。我调用在不同线程中执行计算的方法以保持应用程序的响应性,但是当我尝试从主线程传递一个字段作为来自不同线程的这些方法的参数时,我得到一个异常。我尝试过使用Dispatcher,但没有解决我的问题。以下是在不同线程中调用的方法: private void Compare() { //gets a hashcode using getpixel string instance and checks if image is cro

我正在用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();
        }
public List GetHashCode(位图图像位图)
{//获取位图并将其转换为哈希代码列表
List hashCode=新列表();
int stride=bitmap.PixelWidth*(bitmap.Format.BitsPerPixel/8);
for(int i=0;i

如您所见,我尝试使用Dispatcher方法,但它不起作用。您必须使用后台线程进行与UI相关的活动。请尝试像这样更改代码

    private void CompareButton_Click(object sender, RoutedEventArgs e)
    {
        BackgroundWorker workerThread = new BackgroundWorker();
        workerThread.DoWork+=workerThread_DoWork;
        workerThread.RunWorkerCompleted +=workerThread_RunWorkerCompleted;
        workerThread.RunWorkerAsync();
    }

    private void workerThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        Console.WriteLine("Process completed");
    }

    private void workerThread_DoWork(object sender, DoWorkEventArgs e)
    {
        Compare();
    }

    private void Compare()
    {
        //your method
    }

我尝试按照Kosala的建议使用后台工作程序,但它仍然没有解决我的问题,在谷歌搜索了一段时间后,我发现可以将参数传递给RunWorkerAsync()然后在您的Do_工作类中使用它,所以我所做的是将两个包含位图图像的ADResponse的字符串值传递给RUnWorkerAsync,然后使用它们创建位图图像的新实例,然后将这两个新实例作为参数传递给我的函数。解决方案对我来说有点脏,但我找不到更好的解决方案r一,这是我的工作方法:

 void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        List<string> strings = (List<string>)e.Argument;
        string string1 = strings[0];
        string string2 = strings[1];
        BitmapImage image1 = new BitmapImage();
        image1.BeginInit();
        image1.UriSource = new Uri(string1);
        image1.EndInit();

        BitmapImage image2 = new BitmapImage();
        image2.BeginInit();
        image2.UriSource = new Uri(string2);
        image2.EndInit();
        Compare(image1, image2);
    }
void worker\u DoWork(对象发送方,DoWorkEventArgs e)
{
列表字符串=(列表)e.参数;
字符串string1=字符串[0];
字符串string2=字符串[1];
BitmapImage image1=新的BitmapImage();
图1.BeginInit();
image1.UriSource=新Uri(string1);
image1.EndInit();
BitmapImage image2=新的BitmapImage();
图2.BeginInit();
image2.UriSource=新Uri(string2);
image2.EndInit();
比较(图1、图2);
}

什么是异常,它在哪一行?我个人建议创建一份正在使用的位图图像副本(可能是
位图
对象),并在后台线程中使用它们。