无效的交叉线程

时间:2014-06-15 05:39:19

标签: c# windows-phone-8 windows-phone

当我尝试使用await运行其他函数时,我在构造函数(UnauthorisedAccessException)上遇到特定错误。我用await来使我的进度条可见,否则看不到。你能指出我的错误吗?我必须提交我的代码。我在这里添加了全班。如果我删除等待,进度条仍然不可见。如果我按照我的方式使用它,它会抛出异常。请帮忙。

public partial class FeatureSelection1 : PhoneApplicationPage
{
   static int Dataset = 0;
   static int maxRej = 0;
   static int file_type = 0;
   static String path_Healthy = "";
   static String path_faulty = "";
   static float[,] faultyData;
   static float[,] healthyData;

   public FeatureSelection1()
    {
        InitializeComponent();


    }
private void OptionSelection_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
    {

        var selectedItem = (sender as ListPicker).SelectedItem;
        Dataset = (int)Math.Ceiling(float.Parse(selectedItem.ToString()));

    }

    private void OptionSelection6_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
    {

        var selectedItem = (sender as ListPicker).SelectedItem;
        maxRej = (int)Math.Ceiling(float.Parse(selectedItem.ToString()));
    }
    protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
    {
        base.OnBackKeyPress(e);
     MessageBoxResult m =    MessageBox.Show("You are aborting operation." , "Disclaimer" , MessageBoxButton.OKCancel);
     if (m == MessageBoxResult.OK)
         NavigationService.Navigate(new Uri("/Procedure Selection.xaml" , UriKind.Relative));


    }

    private async void SelectFeature_Click_1(object sender, RoutedEventArgs e)
    {
        String test1 = (App.Current as App).Folder_Name;
        String test2 = (App.Current as App).Folder_Name2;
        if (test1 == "" || test2 == "")
        {
            MessageBox.Show("Please select the files before proceeding");
        }
        else
        {
            ReadFaultyFile(test1);
            ReadHealthyFile(test2);

                progressBar1.Visibility = System.Windows.Visibility.Visible;
                TextBox3.Visibility = System.Windows.Visibility.Visible;

       await Task.Run( ()=>  FileExplorer.FeatureSelection2.mainFunc(healthyData, faultyData, Dataset, maxRej));
            progressBar1.Visibility = System.Windows.Visibility.Collapsed;
            TextBox3.Visibility = System.Windows.Visibility.Collapsed;
        }

    }

    private void Faulty_Click_1(object sender, RoutedEventArgs e)
    {
        file_type = 1;
        (App.Current as App).FeatSelect = 1;
      //  MessageBoxResult m = MessageBox.Show("Please select a file and then follow the instructions up ahead to use it to process", "GENERAL INSTRUCTIONS", MessageBoxButton.OK);
       // if (m == MessageBoxResult.OK)
            NavigationService.Navigate(new Uri("/IsoAccess3.xaml", UriKind.Relative));

    }

    private void Healthy_Click_1(object sender, RoutedEventArgs e)
    {
        file_type = 2;
        (App.Current as App).FeatSelect =2;
      //  MessageBoxResult m = MessageBox.Show("Please select a file and then follow the instructions up ahead to use it to process", "GENERAL INSTRUCTIONS", MessageBoxButton.OK);
      //  if (m == MessageBoxResult.OK)
            NavigationService.Navigate(new Uri("/IsoAccess3.xaml", UriKind.Relative));



    }
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {


                OptionSelection.ItemsSource = new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8" };
                OptionSelection6.ItemsSource = new String[] { "0", "1", "2", "3" };

            progressBar1.IsIndeterminate = true;

            progressBar1.IsEnabled = true;
            progressBar1.Visibility = System.Windows.Visibility.Collapsed;
            TextBox3.Visibility = System.Windows.Visibility.Collapsed;


        base.OnNavigatedTo(e);

        if (file_type == 1)
        {
            TextBox2.DataContext = (App.Current as App).Folder_Name;
            path_faulty = (App.Current as App).Folder_Name;
        }
        else if(file_type == 2)
        {

            TextBox1.DataContext = (App.Current as App).Folder_Name2;
            path_Healthy = (App.Current as App).Folder_Name2; 
        }
        file_type = 0;
        (App.Current as App).s = "";
    }
    public  void ReadFaultyFile(String path)
    {
        IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
        String contents = "";
        // String temp = "";
        int i = 0, j = 0,length=0;
        StringBuilder sb = new StringBuilder();
        if (isf.FileExists(path))
        {
            using (StreamReader reader =
                                    new StreamReader(isf.OpenFile(path,
                                        FileMode.Open, FileAccess.Read)))
            {
                contents = reader.ReadToEnd();
                String[] str = contents.Split('\n');
                String[] str2 = str[0].Split(',');

                for (int k = 0; k < str.Length; k++)
                {
                    if (str[k] == "")
                        continue;
                    length += 1;

                }
                faultyData = new float[length, str2.Length - 1];
                for (i = 0; i <= str.Length - 1; i++)
                {
                    j = 0;
                    str2 = str[i].Split(',');
                    for (j = 0; j < str2.Length - 1; j++)
                    {
                        if (str2[i].Length == 0)
                            continue;
                        faultyData[i, j] = float.Parse(str2[j]);
                    }
                }

            }
        }
    }

         public  void ReadHealthyFile(String path)
    {
        IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
         String contents = "";
       // String temp = "";
        int i=0 , j=0 , length =0;
        StringBuilder sb = new StringBuilder();
        if (isf.FileExists(path))
        {
            using (StreamReader reader =
                                    new StreamReader(isf.OpenFile(path,
                                        FileMode.Open, FileAccess.Read)))
            {
                contents = reader.ReadToEnd();
                String[] str = contents.Split('\n');
                String[] str2 = str[0].Split(',');

                for (int k = 0; k < str.Length; k++)
                {
                    if (str[k] == "")
                        continue;
                    length += 1;

                }
               healthyData = new float[length, str2.Length - 1];
                for (i = 0; i <= str.Length - 1; i++)
                {
                    j = 0;
                    str2 = str[i].Split(',');
                    for (j = 0; j < str2.Length - 1; j++)
                    {
                        if (str2[i].Length == 0)
                            continue;
                        healthyData[i, j] = float.Parse(str2[j]);
                    }
                }

            }
        }
    }


}

}

MainFunc()

  public static void mainFunc(float[,] brej_healthy, float[,] brej_faulty, int dataset, int maxreject) // the main working function
    {
        feature = brej_healthy.GetLength(1);
        datawidth = brej_faulty.GetLength(0) / dataset;

        FeatureSelection2.dataset = dataset;

        score = new int[feature];
        for (int i = 0; i < feature; i++)     // Creates an array of all zeroes
            score[i] = 1;

        int dataset_old = dataset;
        float[,] healthy = brej_healthy;
        float[,] faulty = brej_faulty;
        int[] indices;

        score = asyncFeatureDatasetSelect(healthy, faulty, feature, datawidth, dataset, dataset_old, maxreject, score, 50);
        int count = -1;
        indices = new int[feature];
        for (int i = 0; i < feature; i++)
        {
            if (score[i] != 0)
            {
                count += 1;
                indices[count] = i;
            }
        }

      //  return indices;
        // CAn return Index as output of this function. Chek the matlab code for this. 

    }

0 个答案:

没有答案