Problem using Invoke to update UI from secondary thread


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Problem using Invoke to update UI from secondary thread

  1. #1
    Join Date
    Oct 2005
    Posts
    4

    SOLVED: Problem using Invoke to update UI from secondary thread

    Hi all,

    I have a small application that performs a time consuming process (resizing multiple images in a directory) so i am executing in a separate thread but i want to update the UI thread with the status of the process. I'm trying to do this in the standard way of defining a delagate and a method in the UI thread but i'm getting the following error messages:

    Error 1: An object reference is required for the nonstatic field, method, or property 'ImageResizer.Form1.listbox_delegate' C:\...\Projects\ImageResizer\ImageResizer\Form1.cs

    Error 2: An object reference is required for the nonstatic field, method, or property 'System.Windows.Forms.Control.Invoke(System.Delegate, params object[])' C:\...\Projects\ImageResizer\ImageResizer\Form1.cs
    The bold line in the relevant segment of code below is the line causing the error.

    Code:
    namespace ImageResizer
    {
        public delegate void DelegateAddToListbox(String s);
        
        public partial class Form1 : Form
        {
            public DelegateAddToListbox listbox_delegate;
    
            public Form1()
            {
                InitializeComponent();
    			...
                listbox_delegate = new DelegateAddToListbox(this.addToListbox);
    			...
            }
    
            public void addToListbox(String s)
            {
                listbox.Items.Add(s);
            }
    
            private void button_Click(object sender, EventArgs e)
            {
                ThreadClassWithState tcws = new ThreadClassWithState(textbox_source_folder.Text,
                                                                     textbox_folder.Text,
                                                                     textbox_percent.Text);
                Thread t = new Thread(new ThreadStart(tcws.Run));
                t.Start();
            }
       }
    
        public class ThreadClassWithState
        {
            private String source_folder, dest_folder, percent;
    
            public ThreadClassWithState(String textbox_source_folder, String textbox_folder, String Percent)
            {
                this.source_folder = textbox_source_folder;
                this.dest_folder = textbox_folder;
                this.percent = Percent;
            }
    
            public void Run()
            {
                DirectoryInfo di = new DirectoryInfo(this.source_folder);
                foreach (FileInfo fi in di.GetFiles("*.jpg"))
                {
                    Image original_image = Image.FromFile(this.source_folder + "\\" + fi.Name);
                    Image resized_image = null;
    
                    resized_image = scaleByPercent(original_image, Convert.ToInt16(this.percent));
                    resized_image.Save(this.dest_folder + "\\" + fi.Name);
                    resized_image.Dispose();
    
                    Form1.Invoke(Form1.listbox_delegate, new Object[] {fi.Name});
                }
            }
    
            static Image scaleByPercent(Image original_image, int percent)
            {
    		...
    		...
            }
        }
    }
    Last edited by mat106; 10-07-2006 at 10:22 AM.

  2. #2
    Join Date
    Oct 2005
    Posts
    4

    SOLVED: Problem using Invoke to update UI from secondary thread

    Solution can be found here

Similar Threads

  1. Problem to update using datagrid
    By shobapond in forum ASP.NET
    Replies: 4
    Last Post: 05-02-2005, 11:22 PM
  2. Replies: 3
    Last Post: 03-11-2003, 06:00 PM
  3. TcpListener thread problem
    By David Chu in forum .NET
    Replies: 2
    Last Post: 01-23-2003, 02:27 PM
  4. Replies: 0
    Last Post: 12-13-2001, 01:06 PM
  5. Java Thread
    By Kevin Chien in forum Java
    Replies: 3
    Last Post: 09-12-2000, 06:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center