Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,
I am developing a Windows Application in C#.net. I am using Backgroundworker in my Project.I know how to use Backgroundworker?But i don't How to use Dynamically(Backgroundworker). So Please tell me .
I list out my Controls:
1)FlowlayoutPanel --- Static
2)Group Box -- Dynamic
3)picture Boxes(3)-- Dynamic
4)BackgroundWorker -- Dynamic
Task:
when I click the One of picture Box ,Backgroundworker will run
Controls description:
Group Box DEVICE1
DEVICE2
DEVICE3
Picture Box:
PictureBox1
PictureBox2
PictureBox3
in these picture boxes are inbuilt in the Each GroupBox
When i Click Picture Box1 in the Device1-- Backgroundworker will run.
at the same time i click Picture Box1 in the Device2-- Backgroundworker will run.
and it 's going on .So please tell me How to achieve this?
Thanks in Advance.
Regards,
Lakshmi Narayanan.S
Posted
Comments
Herman<T>.Instance 20-Jul-11 4:53am    
you might set up a queue with Tasks and run in paralellism (threading)
naraayanan 20-Jul-11 6:40am    
Thanks .
Sergey Alexandrovich Kryukov 21-Jul-11 4:06am    
What do you mean static or dynamic control? Not clear what's your problem.
--SA
naraayanan 21-Jul-11 23:28pm    
Static-- Drag and Drop
Dynamic -- To create a Control Dynamically

1 solution

Hi Narayanan

Not Sure about what exactly you want your background processor to do. I have created the one below which updates the progress bar status with the % in the respective textblock. Let me know if you want it to do something else.


XAML Window Code should be-->
XML
<window x:class="BackgroundWorker.Window1" xmlns:x="#unknown">
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="600" Width="800">
  <stackpanel>        
     <groupbox> 
         <groupbox.header>
         <textblock text="Device1" foreground="Black">            
         </textblock>
     </groupbox.header>           
 <stackpanel>         
       <Button x:Name="Button1"  Width="90" Height="30" HorizontalAlignment="Left" Click="Button1_Click"  Margin="0 0 0 0">                   <Image Source="yes.png"></Image>
                </Button>
            </stackpanel>
           
        </groupbox>
        <groupbox>        <groupbox.header>            <textblock text="Device2" foreground="Black">
        </textblock></groupbox.header>
        <stackpanel>            <Button x:Name="Button2" Width="90" Height="30" HorizontalAlignment="Left" Click="Button2_Click"   Margin="10 0 0 0">
                <Image Source="yes.png"></Image>
            </Button>
        </stackpanel>
    </groupbox>
        <groupbox>
            <groupbox.header>
                <textblock text="Device3" foreground="Black" />
            </groupbox.header>
            <stackpanel>
                <Button x:Name="Button3" Width="90" Height="30" HorizontalAlignment="Left" Click="Button3_Click" Margin="10 0 0 0">
                    <Image Source="yes.png"></Image>
                </Button>
            </stackpanel>    
    </groupbox>       
 <progressbar x:name="ProgressBar">
                    Margin="0 10 0 0"
                    Height="23"
                     Minimum="0"
                     Maximum="100"
                     />
        <progressbar x:name="ProgressBar2">
                    Margin="0 10 0 0"
                    Height="23"
                     Minimum="0"
                     Maximum="100"
                     />        <textblock x:name="Message">
                     Margin="0 10 0 0"
                     />       <textblock x:name="MessageUpdbyBtn1">
                 Margin="0 20 0 0"
                 />       <textblock x:name="MessageTemp">
                     Margin="0 10 0 0"
                     />        <textblock x:name="MessageUpdbyBtn2">
                 Margin="0 20 0 0"
                 />
    </textblock></textblock></textblock></textblock></progressbar></progressbar></stackpanel>
<window>
</window></window>

Write the codebehind as below:- (I have taken the background worker in code behind)

C#
using System.Windows;
using System.ComponentModel;
using System.Threading;
using System.Windows.Media;
using System;
namespace TestBackgroundWorker7338
{
    public partial class Window1 : Window
    {
        private BackgroundWorker _worker;
        private BackgroundWorker _worker2;
        int _thread1percentageFinished = 0;
        int _thread2percentageFinished = 0;
        public Window1()
        {
            InitializeComponent();
         
        }
     
        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            _worker2 = new BackgroundWorker();
            _worker2.WorkerReportsProgress = true;
            _worker2.WorkerSupportsCancellation = true;
            _worker2.DoWork += (s, args) =>
            {
                BackgroundWorker worker2 = s as BackgroundWorker;
                int numberOfTasks = 1000;
                for (int i = 1; i <= numberOfTasks; i++)
                {
                    if (worker2.CancellationPending)
                    {
                        args.Cancel = true;
                        return;
                    }
                    Thread.Sleep(5);
                    float percentageDone = (i / (float)numberOfTasks) * 100f;
                    worker2.ReportProgress((int)percentageDone);
                }
            };
            _worker2.ProgressChanged += (s, args) =>
            {
                _thread2percentageFinished = args.ProgressPercentage;
                MessageUpdbyBtn1.Text = String.Format("Second thread: {0}, ({1}% finished)", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), _thread2percentageFinished);
            };

            _worker = new BackgroundWorker();
            _worker.WorkerReportsProgress = true;
            _worker.WorkerSupportsCancellation = true;
            _worker.DoWork += (s, args) =>
            {
                BackgroundWorker worker = s as BackgroundWorker;
                int numberOfTasks = 300;
                for (int i = 1; i <= numberOfTasks; i++)
                {
                    if (worker.CancellationPending)
                    {
                        args.Cancel = true;
                        return;
                    }
                    Thread.Sleep(10);
                    float percentageDone = (i / (float)numberOfTasks) * 100f;
                    worker.ReportProgress((int)percentageDone);
                }
            };
            _worker.ProgressChanged += (s, args) =>
            {
                _thread1percentageFinished = args.ProgressPercentage;
                ProgressBar.Value = _thread1percentageFinished;
                Message.Text = _thread1percentageFinished + "% finished";
             
            };
            _worker.RunWorkerCompleted += (s, args) =>
            {
            
                ProgressBar.Value = 0;
                if (_thread1percentageFinished < 100)
                {
                    Message.Text = "stopped at " + _thread1percentageFinished + "%";
                }
                else
                {
                    Message.Text = "first thread finished";
                }
            };
            _worker2.RunWorkerAsync();
            _worker.RunWorkerAsync();
        }
        private void Button2_Click(object sender, RoutedEventArgs e)
        {
            
            
            _worker2 = new BackgroundWorker();
            _worker2.WorkerReportsProgress = true;
            _worker2.WorkerSupportsCancellation = true;
            _worker2.DoWork += (s, args) =>
            {
                BackgroundWorker worker2 = s as BackgroundWorker;
                int numberOfTasks = 1000;
                for (int i = 1; i <= numberOfTasks; i++)
                {
                    if (worker2.CancellationPending)
                    {
                        args.Cancel = true;
                        return;
                    }
                    Thread.Sleep(5);
                    float percentageDone = (i / (float)numberOfTasks) * 100f;
                    worker2.ReportProgress((int)percentageDone);
                }
            };
            _worker2.ProgressChanged += (s, args) =>
            {
                _thread2percentageFinished = args.ProgressPercentage;
                MessageUpdbyBtn2.Text = String.Format("Second thread: {0}, ({1}% finished)", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), _thread2percentageFinished);
            };

            _worker = new BackgroundWorker();
            _worker.WorkerReportsProgress = true;
            _worker.WorkerSupportsCancellation = true;
            _worker.DoWork += (s, args) =>
            {
                BackgroundWorker worker = s as BackgroundWorker;
                int numberOfTasks = 300;
                for (int i = 1; i <= numberOfTasks; i++)
                {
                    if (worker.CancellationPending)
                    {
                        args.Cancel = true;
                        return;
                    }
                    Thread.Sleep(10);
                    float percentageDone = (i / (float)numberOfTasks) * 100f;
                    worker.ReportProgress((int)percentageDone);
                }
            };
            _worker.ProgressChanged += (s, args) =>
            {
                _thread1percentageFinished = args.ProgressPercentage;
                ProgressBar2.Value = _thread1percentageFinished;
                MessageTemp.Text = _thread1percentageFinished + "% finished";

            };
            _worker.RunWorkerCompleted += (s, args) =>
            {
              
                ProgressBar2.Value = 0;
                if (_thread1percentageFinished < 100)
                {
                    MessageTemp.Text = "stopped at " + _thread1percentageFinished + "%";
                }
                else
                {
                    MessageTemp.Text = "second thread finished";
                }
            };
            _worker2.RunWorkerAsync();
            _worker.RunWorkerAsync();
        }
        private void Button3_Click(object sender, RoutedEventArgs e)
        {
        }
    }
}



Note:- I have left Button3 click event blank you can imitate the similar approach


Hope this helps :)
Happy Coding!!
Prachi
 
Share this answer
 
v3
Comments
prachi sahai 20-Jul-11 6:40am    
Let me know in case you want it to behave differently
Tarun.K.S 20-Jul-11 6:42am    
Prachi, use <pre> tags to wrap your code.
prachi sahai 20-Jul-11 6:44am    
Hey m sorry :( sure I would
naraayanan 20-Jul-11 8:36am    
Hi,
Thanks friends.I don't want to Web Application.I want Windows Application
prachi sahai 21-Jul-11 0:45am    
Yeah.. this is WPF(Windows only)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900