Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. BackgroundWorker and comboBox.Items.Add(f)

BackgroundWorker and comboBox.Items.Add(f)

Scheduled Pinned Locked Moved WPF
csharpwpfdesignsysadminhelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    HahnTech
    wrote on last edited by
    #1

    Hi, I'm new to Threading and WPF and need some help Adding to my Combo Box. I Understand I can't touch the UI from a background thread I just cant seem to figure out the work around. I got This Far. You'll see a Combo1.Items.Add(f); That is throwing. If I can get a good example of how you are suppose to do something like this I'll have a chunk of code I can play with and learn from. Thank you very much For any help

    private void button1_Click(object sender, RoutedEventArgs e)
    {
    string Server;
    Server = cmdServers.SelectedItem.ToString();
    FillApplicationCombo(Server);
    }

    void FillApplicationCombo( string Server)
    {
    int count;
    string Path;
    int Version;
    string AppName;

            Path = @"\\\\" + Server + @"\\" + "DOCFOCUS";
    
            progressBar1.Minimum = 0;
            progressBar1.Maximum = \_DirectoryFiles.Count;
    
            \_Worker = new BackgroundWorker();
            \_Worker.WorkerReportsProgress = true;
            \_Worker.WorkerSupportsCancellation = true;
            \_Worker.DoWork += (s, args) =>
            {
                BackgroundWorker worker = s as BackgroundWorker;
                DirSearch(Path); //Gets All Files in the Path
               
                count = 0;
                foreach (string f in \_DirectoryFiles)
                {
                    count++;
                    worker.ReportProgress(count);
                    
    
                    AppName = FileVersionInfo.GetVersionInfo(f).ProductName;
                    if (AppName == null)
                        continue;
    
                    if (AppName.ToUpper().IndexOf("MYAPPNAME") == -1)
                        continue;
    
                    Version = FileVersionInfo.GetVersionInfo(f).ProductMajorPart;//FileVersion;
                    
                    if (Version == 6)
                    {
    
                        Combo1.Items.Add(f);//What should I do about this?
                        
                    }
                }
            };
    
            \_Worker.RunWorkerCompleted += (s, args) =>
            {
                progressBar1.Value = 0;
            };
    
            \_Worker.ProgressChanged += (s, args) =>
           {
               progressBar1.Value = args.ProgressPercentage;
           };
    
            \_Worker.RunWorkerAsync();
    
        }
    

    Ronald Hahn, CN

    realJSOPR 1 Reply Last reply
    0
    • H HahnTech

      Hi, I'm new to Threading and WPF and need some help Adding to my Combo Box. I Understand I can't touch the UI from a background thread I just cant seem to figure out the work around. I got This Far. You'll see a Combo1.Items.Add(f); That is throwing. If I can get a good example of how you are suppose to do something like this I'll have a chunk of code I can play with and learn from. Thank you very much For any help

      private void button1_Click(object sender, RoutedEventArgs e)
      {
      string Server;
      Server = cmdServers.SelectedItem.ToString();
      FillApplicationCombo(Server);
      }

      void FillApplicationCombo( string Server)
      {
      int count;
      string Path;
      int Version;
      string AppName;

              Path = @"\\\\" + Server + @"\\" + "DOCFOCUS";
      
              progressBar1.Minimum = 0;
              progressBar1.Maximum = \_DirectoryFiles.Count;
      
              \_Worker = new BackgroundWorker();
              \_Worker.WorkerReportsProgress = true;
              \_Worker.WorkerSupportsCancellation = true;
              \_Worker.DoWork += (s, args) =>
              {
                  BackgroundWorker worker = s as BackgroundWorker;
                  DirSearch(Path); //Gets All Files in the Path
                 
                  count = 0;
                  foreach (string f in \_DirectoryFiles)
                  {
                      count++;
                      worker.ReportProgress(count);
                      
      
                      AppName = FileVersionInfo.GetVersionInfo(f).ProductName;
                      if (AppName == null)
                          continue;
      
                      if (AppName.ToUpper().IndexOf("MYAPPNAME") == -1)
                          continue;
      
                      Version = FileVersionInfo.GetVersionInfo(f).ProductMajorPart;//FileVersion;
                      
                      if (Version == 6)
                      {
      
                          Combo1.Items.Add(f);//What should I do about this?
                          
                      }
                  }
              };
      
              \_Worker.RunWorkerCompleted += (s, args) =>
              {
                  progressBar1.Value = 0;
              };
      
              \_Worker.ProgressChanged += (s, args) =>
             {
                 progressBar1.Value = args.ProgressPercentage;
             };
      
              \_Worker.RunWorkerAsync();
      
          }
      

      Ronald Hahn, CN

      realJSOPR Offline
      realJSOPR Offline
      realJSOP
      wrote on last edited by
      #2

      You have to setup a delegate,

      private delegate void DelegateUpdateCombo();

      create a method that matches it,

      private void MyMethod()
      {
      // do something
      }

      and then use this in your thread event handler:

      DelegateUpdateCombo method = new DelegateUpdateCombo(MyMethod);
      comboBox.Dispatcher.Invoke(myMethod);

      .45 ACP - because shooting twice is just silly
      -----
      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
      -----
      "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

      H 1 Reply Last reply
      0
      • realJSOPR realJSOP

        You have to setup a delegate,

        private delegate void DelegateUpdateCombo();

        create a method that matches it,

        private void MyMethod()
        {
        // do something
        }

        and then use this in your thread event handler:

        DelegateUpdateCombo method = new DelegateUpdateCombo(MyMethod);
        comboBox.Dispatcher.Invoke(myMethod);

        .45 ACP - because shooting twice is just silly
        -----
        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
        -----
        "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

        H Offline
        H Offline
        HahnTech
        wrote on last edited by
        #3

        Thanks. That did it :)

        Ronald Hahn, CNT - Computer Engineering Technologist

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups