473,412 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

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

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10.  
  11. namespace WindowsFormsApplication1
  12. {
  13.     public partial class Picturebox1_Fullscreen : Form
  14.     {
  15.         BackgroundWorker backGroundWorker1;
  16.         Bitmap pb_form1;
  17.         Bitmap[] myBitmaps;
  18.         Bitmap pb11;
  19.         FileInfo[] file_info_mouse_wheel;
  20.         string radar_images_download_directory;
  21.         private Bitmap[] file_info_trackBar2;
  22.  
  23.  
  24.         public Picturebox1_Fullscreen()
  25.         {
  26.             InitializeComponent();
  27.             backGroundWorker1 = new BackgroundWorker();
  28.             radar_images_download_directory = Options_DB.Get_Radar_Images_Download_Directory();
  29.             DirectoryInfo dir1 = new DirectoryInfo(radar_images_download_directory);
  30.             file_info_mouse_wheel = dir1.GetFiles("*.jpg");
  31.             trackBar1.Minimum = 0;
  32.             trackBar1.Maximum = file_info_mouse_wheel.Length - 1;
  33.             trackBar1.Value = file_info_mouse_wheel.Length - 1;
  34.  
  35.             pb_form1 = new Bitmap(Form1.pb1);
  36.             myBitmaps = ImagesComparison.get_images_with_clouds(pb_form1);
  37.             file_info_trackBar2 = myBitmaps;
  38.  
  39.         }
  40.  
  41.         private void splitContainer1_Panel1_DoubleClick(object sender, EventArgs e)
  42.         {
  43.             this.Close();
  44.         }
  45.  
  46.         private void splitContainer1_Panel2_DoubleClick(object sender, EventArgs e)
  47.         {
  48.             this.Close();
  49.         }
  50.  
  51.         private void pictureBox1_DoubleClick(object sender, EventArgs e)
  52.         {
  53.             this.Close();
  54.  
  55.         }
  56.  
  57.         private bool LoadPictureAt(int nIndex, object c)
  58.         {
  59.  
  60.             bool bRet = false;
  61.  
  62.             if (nIndex >= 0 && nIndex < file_info_mouse_wheel.Length)
  63.             {
  64.                 if (c.Equals(trackBar1))
  65.  
  66.                     pictureBox1.Load(file_info_mouse_wheel[nIndex].FullName);
  67.                     bRet = true;
  68.  
  69.             }
  70.             if (nIndex >= 0 && nIndex < file_info_trackBar2.Length)
  71.             {
  72.                 if (c.Equals(trackBar2))
  73.                     pictureBox1.Image = file_info_trackBar2[nIndex];
  74.                     bRet = true;
  75.             }
  76.  
  77.             return bRet;
  78.  
  79.         }
  80.  
  81.         private void trackBar2_Scroll(object sender, EventArgs e)
  82.         {
  83.  
  84.             LoadPictureAt(trackBar2.Value, sender);
  85.             pictureBox1.Refresh();
  86.  
  87.         }
  88.  
  89.         private void trackBar1_Scroll(object sender, EventArgs e)
  90.         {
  91.  
  92.             LoadPictureAt(trackBar1.Value, sender);
  93.             backGroundWorker1.DoWork +=new DoWorkEventHandler(backGroundWorker1_DoWork);
  94.             pictureBox1.Refresh();
  95.         }
  96.  
  97.  
  98.         public PictureBox picturebox1(PictureBox pb1)
  99.         {
  100.             pictureBox1.Image = pb1.Image;
  101.             return pictureBox1;
  102.         }
  103.  
  104.         public string picturebox1_lastFile(string last_file)
  105.         {
  106.             pictureBox1.Load(last_file);
  107.             return last_file;
  108.        }
  109.  
  110.         private void backGroundWorker1_DoWork(object sender, DoWorkEventArgs e)
  111.         {
  112.             pb11 = new Bitmap(pictureBox1.Image);
  113.             myBitmaps = ImagesComparison.get_images_with_clouds(pb11);
  114.             file_info_trackBar2 = myBitmaps;
  115.  
  116.         }
  117.     }
  118. }
  119.  
Before i had the BackgroundWorker so the code inside the DoWork event was here in the trackBar1_Scroll event like this:

Expand|Select|Wrap|Line Numbers
  1. private void trackBar1_Scroll(object sender, EventArgs e)
  2.         {
  3.  
  4.             LoadPictureAt(trackBar1.Value, sender);
  5.             pb11 = new Bitmap(pictureBox1.Image);
  6.             myBitmaps = ImagesComparison.get_images_with_clouds(pb11);
  7.             file_info_trackBar2 = myBitmaps;
  8.             pictureBox1.Refresh();
  9.         }
  10.  
And it worked good but when i added the BackgroundWorker and its events it dosent work as before.

Since in the constructor and also in the trackBar1_Scroll 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 BackgroundWorker 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 ImagesComparison.get_images_with_clouds(pb11); so it will do it in the Background.


This is the ImagesComparison.get_images_with_clouds code if its important:

Expand|Select|Wrap|Line Numbers
  1.  public static Bitmap[] get_images_with_clouds(Bitmap radar_image)
  2.         {
  3.             int e = 0;
  4.             int f = 0;
  5.             int image_clock_area_x=0;
  6.             int image_clock_area_y=0;
  7.             int image_clock_area_x1=140;
  8.             int image_clock_area_y1=21;              Bitmap[] images;
  9.              images = new Bitmap[15];
  10.             Bitmap image;
  11.             image = new Bitmap(Properties.Resources.radar_without_clouds);
  12.             for (e = image_clock_area_x; e < image_clock_area_x + image_clock_area_x1; e++)
  13.             {
  14.                 for (f = image_clock_area_y; f < image_clock_area_y + image_clock_area_y1; f++)
  15.                 {
  16.                     Color clock_color = radar_image.GetPixel(e, f);
  17.                     image.SetPixel(e, f, clock_color);
  18.  
  19.                 }
  20.             }
  21.  
  22.             int c;
  23.             for (c = 0; c < images.Length; c++)
  24.             {
  25.                 images[c] = new Bitmap(image);
  26.             }
  27.  
  28.             Bitmap new_image = new Bitmap(Properties.Resources.radar_without_clouds);
  29.             Bitmap new_image1 = new Bitmap(Properties.Resources.radar_without_clouds);
  30.             black_and_white(new_image, radar_image);// to change the black and white function name 
  31.                        Image image1 = black_and_white(new_image, radar_image);
  32.             Bitmap clouds = new Bitmap(image1);
  33.  
  34.             int x;
  35.             int y;
  36.             int a;
  37.             int b;
  38.             int d;
  39.             Bitmap redImage;
  40.             redImage = new Bitmap(512, 512);
  41.             for (x = 0; x < redImage.Width; x++)
  42.             {
  43.                 for (y = 0; y < redImage.Height; y++)
  44.                 {   
  45.                     redImage.SetPixel(x, y, Color.Red);
  46.                 }
  47.             }
  48.  
  49.             for (a = 0; a < new_image.Width; a++)
  50.             {
  51.                 for (b = 0; b < new_image.Height; b++)
  52.                 {
  53.                     Color color1 = clouds.GetPixel(a, b);
  54.                     Color color2 = radar_image.GetPixel(a, b);
  55.                     if (color1.R == 0 && color1.G == 0 && color1.B == 0)
  56.                     {
  57.                                            }
  58.                     else
  59.                     {
  60.                         double h, mm;
  61.                         h = color2.GetHue();
  62.  
  63.                         mm = RadarAnalysis.Hue2MMPerHour(h);
  64.                                                 double treshold = 0;
  65.                         for (d = 0; d < images.Length; d++)
  66.                         {
  67.  
  68.                             if (mm >= treshold)
  69.                             {
  70.                                // new_image.SetPixel(a, b, color2);
  71.                                images[d].SetPixel(a, b, color2);
  72.                             }
  73.                             treshold = treshold + 4;
  74.  
  75.                         }
  76.                                            }
  77.                 }
  78.             }
  79.             return images;
  80.  
  81.         }
  82.  

Thanks.
May 22 '11 #1
1 1355
You need to call RunWorkerAsync() method of backgroundworker to fire doWork event.
May 27 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Slide-O-Mix | last post by:
I am using the Process class to run an external application from my application. The first time I call the .Start() method it takes several seconds for the process to actually start. Subsequent...
1
by: Shourie | last post by:
I've noticed that none of the child controls events are firing for the first time from the dynamic user control. Here is the event cycle. 1) MainPage_load 2) User control1_Load user clicks a...
2
by: Alan Silver | last post by:
Hello, I'm having rather a problem with user control. It is a fairly simple affair (see my other threads for more details) that shows a date and time in five drop down controls. I had private...
16
by: TB | last post by:
Hi all: If you think that the following comments are absolute amateurish, then please bear with me, or simply skip this thread. A couple of months back I made the decision to initiate a...
2
by: bwhite | last post by:
I have an access database application that is used to calculate landed costs for foreign goods imported into vartious countries. I am trying to determine an approach so that a user will be...
13
by: newlearner | last post by:
hi, Im new into Ajax and Javascript, I have a doubt... I have a External javascript. and a couple of html files and I load These through ajax request and response. The html pages contain a...
2
by: majestic12 | last post by:
This is the first time I've tried to use mysql/phpmyadmin and I'm having trouble. I'm using a geocites pro account and I got both installed and it set up a database call mysql and the user yroot. ...
2
by: Martin Leese | last post by:
Hi, I am having trouble with CSS in that I can't get the pseudo-element 'first-line' to work. I have some pages that contain dialog. I use a <dlfor this, with the character's name in a...
4
kcdoell
by: kcdoell | last post by:
Hello: I am trying to create a DAO record set for the first time. I wanted to keep it simple, so I first wrote something to work for my Division field then I would expand on the code later. ...
3
by: Chocolade | last post by:
The file logoArea is 512x512 Now all the int variables are the area of the ogi wich is on the right bottom corner. newimage is the image where i want to put the logo inside so in the end when...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.