So im checking if its null inside the StartWork function and if its null im doing trackBar2.Enable = false;
Now the backGroundWorking is starting to work and after few seconds file_info_trackBar2 isnt null.
So in the event: backGroundWorker1_DoWork im checking if file_info_trackBar2 isnt null != and its not so im doing trackBar2.Enable = true;
But i used a breakpoint on it and as its out after the trackBar2.Enable = true; im getting exception say:
Cross-thread operation not valid: Control 'trackBar2' accessed from a thread other than the thread it was created on
Whats that ? How can i solve it ? It happens only when im trying to make the trackBar2.Enable = true;
Second problem is inside the event: backGroundWorker1_DoWork again. I want that pb11 = new Bitmap(@"e:\radar003626.jpg"); instead this file it will make new Bitmap to the current file is on trackBar1. And in the function LoadPictureAt im getting the current file name on pictureBox1.Load(file_info_mouse_wheel[nIndex].FullName);
But how do i pass or use this file name on the pb11 = new Bitmap ?
Expand|Select|Wrap|Line Numbers
- 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
- {
- int nIndex1;
- BackgroundWorker backGroundWorker1;
- Bitmap[] myBitmaps;
- Bitmap pb11;
- FileInfo[] file_info_mouse_wheel;
- string radar_images_download_directory;
- private Bitmap[] file_info_trackBar2;
- private bool _isDirty = false;
- public Picturebox1_Fullscreen()
- {
- InitializeComponent();
- backGroundWorker1 = new BackgroundWorker();
- backGroundWorker1.WorkerSupportsCancellation = true;
- backGroundWorker1.DoWork += new DoWorkEventHandler(backGroundWorker1_DoWork);
- backGroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backGroundWorker1_RunWorkerCompleted);
- 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;
- // To check the pb11 new.....and to check how to check the file_info_trackBar2 if it null what to do \\
- // if (file_info_trackBar2 == null)
- // {
- // trackBar2.Enabled = false;
- // }
- StartWork();
- this.FormClosing += new FormClosingEventHandler(Picturebox1_Fullscreen_FormClosing);
- }
- 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);
- nIndex1 = nIndex;
- bRet = true;
- }
- if (file_info_trackBar2 == null)
- {
- }
- else
- {
- 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)
- {
- StartWork();
- LoadPictureAt(trackBar2.Value, sender);
- pictureBox1.Refresh();
- }
- private void trackBar1_Scroll(object sender, EventArgs e)
- {
- LoadPictureAt(trackBar1.Value, sender);
- }
- 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)
- {
- int f = (int)e.Argument;
- pb11 = new Bitmap(@"e:\radar003626.jpg");
- myBitmaps = ImagesComparison.get_images_with_clouds(pb11);
- if (this.backGroundWorker1.CancellationPending)
- {
- e.Result = f;
- return;
- }
- file_info_trackBar2 = myBitmaps;
- e.Result = f;
- pb11.Dispose();
- if (file_info_trackBar2 != null)
- {
- trackBar2.Enabled = true;
- }
- }
- private void Picturebox1_Fullscreen_FormClosing(object sender, FormClosingEventArgs e)
- {
- //put in code here to show a message, or take some action
- //if the bgw isBusy
- if (backGroundWorker1 != null)
- backGroundWorker1.Dispose();
- }
- private void backGroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
- {
- //display result
- //if previous bgw-operation cancelled
- //start bgw again, so, that the current
- //result is the current trackbar-value
- if (this._isDirty)
- {
- this._isDirty = false;
- StartWork();
- }
- }
- private void StartWork()
- {
- //run bgw
- if (this.backGroundWorker1.IsBusy == false)
- {
- this.backGroundWorker1.RunWorkerAsync(this.trackBar2.Value);
- if (file_info_trackBar2 == null)
- {
- trackBar2.Enabled = false;
- }
- }
- else
- {
- //force re-run of bgw
- this._isDirty = true;
- //and cancel current action of bgw
- this.backGroundWorker1.CancelAsync();
- }
- }
- }
- }
Thanks for helping.