Advertisement
MarcelloGrechi

Fixed CrossThreading Issue

Jun 15th, 2012
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.44 KB | None | 0 0
  1. // START BUTTON Event
  2. private void StartBtn_OnClick(object sender, EventArgs e)
  3.          {            
  4.              string dllName = cboxDlls.Text;
  5.  
  6.              try
  7.              {
  8.                  InputModel input = new InputModel (_coursesSource, dllName);
  9.                  
  10.                  // Starting Background Crawler
  11.                  _bgCrawler = new AbortableBackgroundWorker(); // Crawler in Background
  12.  
  13.          // Invokes BGWorker using invoke. Dunno why, but it worked
  14.                  CoursesGrid.Invoke((Action)  delegate {_bgCrawler.RunWorkerAsync(input);});
  15.  
  16.                  // Starting Background Grid Refresher
  17.                  RefreshGridWorker.RunWorkerAsync ();
  18.  
  19.              }
  20.              catch (Exception ex)
  21.              {
  22.                  // TODO: Log and Add Exceptions Logs @ UI
  23.              }
  24.          }
  25.  
  26. // BG Worker 2 : Gridview refresher
  27. private void RefreshGridWorker_DoWork(object sender, DoWorkEventArgs e)
  28.          {
  29.              while (1 == 1)
  30.              {
  31.                  CoursesGrid.BeginInvoke // Inline method declaration
  32.                      (
  33.                         (Action)delegate
  34.                         {
  35.                             if (_coursesSource.Count > 0)
  36.                             {
  37.                                 _coursesSourceClone = new BindingSource();
  38.  
  39.                     // Not good but everysecond i copy the whole source list to a temp one
  40.                                 foreach (OutputModel m in _coursesSource)
  41.                                 {
  42.                                     _coursesSourceClone.Add(m);
  43.                                 }
  44.  
  45.                                 CoursesGrid.DataSource = _coursesSourceClone;
  46.                                 CoursesGrid.EndEdit();
  47.                             }
  48.                         }
  49.  
  50.                      ); // end of inline method declaration
  51.                  Thread.Sleep(1000); // Checks every second for a new source
  52.              }
  53.              }
  54.          }
  55.  
  56.  // Method of interest in the .dll - This is the method that parses each course
  57.  public static void ParseCourse(ref Course parsedCourse, ref BindingSource coursesSource)
  58.         {
  59.             List<Discipline> courseDisciplines;
  60.  
  61.             // Auxiliar Methods Calling
  62.             string name       = ParseCourseName();
  63.             int segments      = CountCourseSegments();
  64.             courseDisciplines = ParseCourseDisciplines();
  65.  
  66.             // Adding parsed course to list
  67.             Course tmpCourse = new Course (name, segments, courseDisciplines);
  68.  
  69.             // Updating source of courses
  70.            
  71.             coursesSource.Add(new OutputModel(tmpCourse.Name,tmpCourse.DisciplinesCount,tmpCourse.Segments)); // Here is where i populate the source
  72.             coursesSource.EndEdit();
  73.            
  74.             parsedCourse     = tmpCourse;
  75.             m_parsedCourses.Add(tmpCourse);
  76.  
  77.             // Adding Unique Disciplines to List of Disciplines of the university
  78.             foreach (Discipline disc in courseDisciplines)
  79.             {
  80.                  if (!m_parsedDisciplines.ContainsKey(disc.Code))
  81.                  {
  82.                      m_parsedDisciplines.Add(disc.Code, disc);
  83.                  }
  84.             }
  85.  
  86.             // Logging Step and Writing course to file
  87.             Logs.LogWriter.LogInfo("Finished Parsing : " + tmpCourse.Name + " Total Disciplines : " + tmpCourse.DisciplinesCount);
  88.                        
  89.          }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement