Click here to Skip to main content
15,919,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Sir I am Uday

In my c sharp winform,

I have two Long Running method uploadUsingText() and uploadUsingExcel()


One process is selected at time depends upon combobox selection.after selection i have to show picturebox1 visible true. and after process is finished picturebox1 must visible false.

my code is
Like this

C#
if (isFiletypeSelect())
            {
                if (cmbSelectFileType.Text == "Text")
                {
                    if (validation())
                    {
                        panel2.Visible = true;
                        
                         pictureBox1.Visible = true;
                        uploadUsingText();
                        // panel1.Enabled = true;

                        panel2.Visible = false;

                    }
                }
                if (cmbSelectFileType.Text == "Excel")
                {
                    if (validation())
                    {
                        uploadUsingExcel();
                    }

                }

            }



How to check method is finished using backgroundWorker depends upon selection?
How i can use backgroundWorker for 2 long methods depends upon selection?

I have tried for above code in following

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e){}

please give me solution.
Thanks
Posted
Updated 29-Aug-11 19:32pm
v2

1 solution

First of all you need to declare a form level BackGroundWorker, and in your form constructor add a handler for it's DoWork event. Then you can simply run your above code in the DoWork event, something like this:

C#
BackgroundWorker bgw = new BackgroundWorker();

        public Form1()
        {
            InitializeComponent();
            bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
            button1.Click += new EventHandler(button1_Click);
        }

        void button1_Click(object sender, EventArgs e)
        {
            bgw.RunWorkerAsync();
        }

        void bgw_DoWork(object sender, DoWorkEventArgs e)
        {
            if(cmbSelectFileType.Text == "Text")
            {
                if (Validation())
                {
                    panel1.Enabled = false;
                    pictureBox1.Visible = true;
                    UpLoadUsingText();
                }
            }
            else if (cmbSelectFileType.Text == "Excel")
            {
                if (Validation())
                {
                    panel1.Enabled = false;
                    pictureBox1.Visible = true;
                    UpLoadUsingExcel();
                }
            }
        }


Hope this helps
 
Share this answer
 
Comments
udusat13 31-Aug-11 6:59am    
Thanks sir.

But
if(cmbSelectFileType.Text == "Text") //this line throws following exception.
Cross-thread operation not valid: Control 'cmbSelectFileType' accessed from a thread other than the thread it was created on.

Please help me
thanks

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