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

In my c sharp winform application,I am inserting multiple rows.It takes 1 min.In

That time i have to show picture box enabled and other controls gets blocked indicating process is running.

In my save button click,i am calling that long running process.
like

C#
private void button2_Click(object sender, EventArgs e)
      {
          if (isFiletypeSelect())
          {
              if (cmbSelectFileType.Text == "Text")
              {
                  if (validation())
                  {
                      pictureBox1.Visible = true;
                      uploadUsingText();  
                      pictureBox1.Visible = false;
                  }
              }

}
}

In above example uploadUsingText() is long running process.

before this method I am showing picture and after this hiding it.

but it not working.
If i remove pictureBox1.Visible = false;

pictureBox visible after method finished.

please give me solution or any other change is acceptable.
Posted

1 solution

Your best bet would be use a separate thread to do the long process. If you are using .Net 4.0 then you could use Tasks like this:

private void button2_Click(object sender, EventArgs e)
       {
           pictureBox1.Visible = true;
           button2.Enabled = false;//disable button so user cannot press until first task is finished
           TaskScheduler UISyncContext = TaskScheduler.FromCurrentSynchronizationContext();
           Task task1 = Task.Factory.StartNew(() => uploadUsingText()).ContinueWith((t) =>
               {
                   pictureBox1.Visible = false;
                   button2.Enabled = true;
               }, UISyncContext);
       }


This assumes that you initially set your picturebox to Visible = false in your designer. I would also suggest you give your control meaningful names. pictureBox1 and button2 are not really a good idea.

According to your comment you are using .Net 3.5. In that case I would simply use a BackGroundWorker to do the long process. Create a BackGroundWorker in your designer and then you could do something like this:

C#
public Form1()
        {
            InitializeComponent();
            button2.Click += new EventHandler(button2_Click);
            this.backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
            this.backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
        }

        void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            uploadUsingText();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.panel1.Enabled = false;
            this.pictureBox1.Visible = true;
            this.backgroundWorker1.RunWorkerAsync();
        }

        void uploadUsingText()
        {
            Thread.Sleep(60000);//simulate long process that takes 1 minute
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.panel1.Enabled = true;
            this.pictureBox1.Visible = false;
        }




Hope this helps
 
Share this answer
 
v3
Comments
Timberbird 25-Aug-11 3:14am    
Though I agree parallel threads for long-running tasks are better, there's an option that other controls can mess with task as well or user can attempt to close form while the task is running (we know nothing about the application, in the end). As far as I understand OP wants to disable all the controls on his form except for pictureBox... maybe placing a panel over the form filling it, putting all the controls except pictureBox on that panel and disabling panel+bringing pictureBox up would help
udusat13 25-Aug-11 3:23am    
Actually I am using panel.All the control are in panel1 and picturebox is in panel2.

and my changed code is

if (validation())
{
// panel2.Visible = true;
panel1.Enabled = false;

// pictureBox1.Visible = true;
uploadUsingText();
panel1.Enabled = true;
panel2.Visible = false;

}
but
it not working
properly

and i am using net3.5 version
udusat13 25-Aug-11 3:44am    
Thanks suggestion for naming convention

allPanelControl enabled true false working properly for me.

but picturePanelControl must visible before the code stated. but it not working properly.
It get visible just before method finished.Even though setting visible true before allPanelControl enabled false.
Wayne Gaylard 25-Aug-11 4:01am    
Please see my updated answer.
Timberbird 25-Aug-11 4:29am    
Have you tried Invalidate() method?

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