Im trying to use BackgroundWorker first time and it dosent work good

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Chocolade
    New Member
    • Aug 2010
    • 69

    Im trying to use BackgroundWorker first time and it dosent work good

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    
    namespace WindowsFormsApplication1
    {
        public partial class Picturebox1_Fullscreen : Form
        {
            BackgroundWorker backGroundWorker1;
            Bitmap pb_form1;
            Bitmap[] myBitmaps;
            Bitmap pb11;
            FileInfo[] file_info_mouse_wheel;
            string radar_images_download_directory;
            private Bitmap[] file_info_trackBar2;
    
    
            public Picturebox1_Fullscreen()
            {
                InitializeComponent();
                backGroundWorker1 = new BackgroundWorker();
                radar_images_download_directory = Options_DB.Get_Radar_Images_Download_Directory();
                DirectoryInfo dir1 = new DirectoryInfo(radar_images_download_directory);
                file_info_mouse_wheel = dir1.GetFiles("*.jpg");
                trackBar1.Minimum = 0;
                trackBar1.Maximum = file_info_mouse_wheel.Length - 1;
                trackBar1.Value = file_info_mouse_wheel.Length - 1;
    
                pb_form1 = new Bitmap(Form1.pb1);
                myBitmaps = ImagesComparison.get_images_with_clouds(pb_form1);
                file_info_trackBar2 = myBitmaps;
    
            }
    
            private void splitContainer1_Panel1_DoubleClick(object sender, EventArgs e)
            {
                this.Close();
            }
    
            private void splitContainer1_Panel2_DoubleClick(object sender, EventArgs e)
            {
                this.Close();
            }
    
            private void pictureBox1_DoubleClick(object sender, EventArgs e)
            {
                this.Close();
                
            }
    
            private bool LoadPictureAt(int nIndex, object c)
            {
    
                bool bRet = false;
    
                if (nIndex >= 0 && nIndex < file_info_mouse_wheel.Length)
                {
                    if (c.Equals(trackBar1))
                    
                        pictureBox1.Load(file_info_mouse_wheel[nIndex].FullName);
                        bRet = true;
                    
                }
                if (nIndex >= 0 && nIndex < file_info_trackBar2.Length)
                {
                    if (c.Equals(trackBar2))
                        pictureBox1.Image = file_info_trackBar2[nIndex];
                        bRet = true;
                }
    
                return bRet;
    
            }
    
            private void trackBar2_Scroll(object sender, EventArgs e)
            {
    
                LoadPictureAt(trackBar2.Value, sender);
                pictureBox1.Refresh();
    
            }
    
            private void trackBar1_Scroll(object sender, EventArgs e)
            {
    
                LoadPictureAt(trackBar1.Value, sender);
                backGroundWorker1.DoWork +=new DoWorkEventHandler(backGroundWorker1_DoWork);
                pictureBox1.Refresh();
            }
    
    
            public PictureBox picturebox1(PictureBox pb1)
            {
                pictureBox1.Image = pb1.Image;
                return pictureBox1;
            }
    
            public string picturebox1_lastFile(string last_file)
            {
                pictureBox1.Load(last_file);
                return last_file;
           }
    
            private void backGroundWorker1_DoWork(object sender, DoWorkEventArgs e)
            {
                pb11 = new Bitmap(pictureBox1.Image);
                myBitmaps = ImagesComparison.get_images_with_clouds(pb11);
                file_info_trackBar2 = myBitmaps;
                
            }
        }
    }
    Before i had the BackgroundWorke r so the code inside the DoWork event was here in the trackBar1_Scrol l event like this:

    Code:
    private void trackBar1_Scroll(object sender, EventArgs e)
            {
    
                LoadPictureAt(trackBar1.Value, sender);
                pb11 = new Bitmap(pictureBox1.Image);
                myBitmaps = ImagesComparison.get_images_with_clouds(pb11);
                file_info_trackBar2 = myBitmaps;
                pictureBox1.Refresh();
            }
    And it worked good but when i added the BackgroundWorke r and its events it dosent work as before.

    Since in the constructor and also in the trackBar1_Scrol l event im using the get_images_with _clouds event from another class and this get_images_with _clouds using For and make the program to be hold untill the For in the other class will end. I want to use the BackgroundWorke r in the two places where im using the get_images_with _clouds so it wont stuck/hold the program.

    I want that when im doing ImagesCompariso n.get_images_wi th_clouds(pb11) ; so it will do it in the Background.


    This is the ImagesCompariso n.get_images_wi th_clouds code if its important:

    Code:
     public static Bitmap[] get_images_with_clouds(Bitmap radar_image)
            {
                int e = 0;
                int f = 0;
                int image_clock_area_x=0;
                int image_clock_area_y=0;
                int image_clock_area_x1=140;
                int image_clock_area_y1=21;              Bitmap[] images;
                 images = new Bitmap[15];
                Bitmap image;
                image = new Bitmap(Properties.Resources.radar_without_clouds);
                for (e = image_clock_area_x; e < image_clock_area_x + image_clock_area_x1; e++)
                {
                    for (f = image_clock_area_y; f < image_clock_area_y + image_clock_area_y1; f++)
                    {
                        Color clock_color = radar_image.GetPixel(e, f);
                        image.SetPixel(e, f, clock_color);
                        
                    }
                }
    
                int c;
                for (c = 0; c < images.Length; c++)
                {
                    images[c] = new Bitmap(image);
                }
    
                Bitmap new_image = new Bitmap(Properties.Resources.radar_without_clouds);
                Bitmap new_image1 = new Bitmap(Properties.Resources.radar_without_clouds);
                black_and_white(new_image, radar_image);// to change the black and white function name 
                           Image image1 = black_and_white(new_image, radar_image);
                Bitmap clouds = new Bitmap(image1);
               
                int x;
                int y;
                int a;
                int b;
                int d;
                Bitmap redImage;
                redImage = new Bitmap(512, 512);
                for (x = 0; x < redImage.Width; x++)
                {
                    for (y = 0; y < redImage.Height; y++)
                    {   
                        redImage.SetPixel(x, y, Color.Red);
                    }
                }
    
                for (a = 0; a < new_image.Width; a++)
                {
                    for (b = 0; b < new_image.Height; b++)
                    {
                        Color color1 = clouds.GetPixel(a, b);
                        Color color2 = radar_image.GetPixel(a, b);
                        if (color1.R == 0 && color1.G == 0 && color1.B == 0)
                        {
                                               }
                        else
                        {
                            double h, mm;
                            h = color2.GetHue();
    
                            mm = RadarAnalysis.Hue2MMPerHour(h);
                                                    double treshold = 0;
                            for (d = 0; d < images.Length; d++)
                            {
    
                                if (mm >= treshold)
                                {
                                   // new_image.SetPixel(a, b, color2);
                                   images[d].SetPixel(a, b, color2);
                                }
                                treshold = treshold + 4;
                                
                            }
                                               }
                    }
                }
                return images;
    
            }

    Thanks.
  • Subin Ninan
    New Member
    • Sep 2010
    • 91

    #2
    You need to call RunWorkerAsync( ) method of backgroundworke r to fire doWork event.

    Comment

    Working...