-1

Currently I have a picture box holding up two items on it one processing gif file and a label. Now I have called the BringToFront() for all three items (picture box,processing gif, label) when ever backgroundworker is being invoked

Below is my code snippet for back ground worker

private void buttonUpload_Click(object sender, EventArgs e)
{
   LoadFile(pdfFullPath);
   bgwLoadFile.RunWorkerAsync(dummyPDFPath);
   pictureBox1.BringToFront();
   picLoading.BringToFront();
   label.BringToFront();
}

private void bgwLoadFile_DoWork(object sender, DoWorkEventArgs e)
{       
    this.Invoke((MethodInvoker)delegate() {
        acrPDFViewer.LoadFile(e.Argument.ToString());
    });
}


private void bgwLoadFile_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    if (e.Cancelled == true)
    {
    }
    else if (e.Error != null)
    {
    }
    else
    {
        pictureBox1.SendToBack();
        picLoading.SendToBack();
        label.SendToBack();
    }
}

While executing it fails to show me up none of the three items.

J. Steen
  • 15,470
  • 15
  • 56
  • 63
Ganeshja
  • 2,675
  • 12
  • 36
  • 57
  • Are you getting some Exception or error message? – Agustin Meriles Apr 15 '13 at 12:29
  • Im not getting any exceptions or errors .. when i debug it , it goes though all steps but no action has been taken place – Ganeshja Apr 15 '13 at 12:30
  • Are you sure that you are reaching the `bgwLoadFile_RunWorkerCompleted` event handler? You might need to repeatedly call [Application.DoEvents()](http://msdn.microsoft.com/en-us/library/system.windows.forms.application.doevents.aspx) to ensure that the `RunWorkerCompleted` event is properly fired. – Anders Gustafsson Apr 15 '13 at 12:38
  • This is the third time you asked the same question: http://stackoverflow.com/questions/15152850/how-to-use-backgroundworker-in-c-sharp and http://stackoverflow.com/questions/15703045/backgroundworker-in-c-sharp#comment22299700_15703045 if you need further guidance please include more details in your questions. – void Apr 15 '13 at 12:43

1 Answers1

3

This works for me:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        UpdateControls(false);

        bgwLoadFile.DoWork += new DoWorkEventHandler(bgwLoadFile_DoWork);
        bgwLoadFile.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgwLoadFile_RunWorkerCompleted);
    }

    private void buttonUpload_Click(object sender, EventArgs e)
    {
        UpdateControls(true);
        bgwLoadFile.RunWorkerAsync();
    }

    void bgwLoadFile_DoWork(object sender, DoWorkEventArgs e)
    {
        //simulate work
        System.Threading.Thread.Sleep(2000);
    }

    void bgwLoadFile_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if(!e.Cancelled && e.Error == null)
            UpdateControls(false);
    }

    private void UpdateControls(bool isVisable)
    {
        if (isVisable)
        {
            pictureBox1.BringToFront();
            picLoading.BringToFront();
            label1.BringToFront();
        }
        else
        {
            pictureBox1.SendToBack();
            picLoading.SendToBack();
            label1.SendToBack();
        }
    }
}

If all you want to do is to make these three controls show only while the backgroundworker is active you could change the UpdateControls()-method to this:

private void UpdateControls(bool isVisable)
{
    pictureBox1.Visible = isVisable;
    picLoading.Visible = isVisable;
    label1.Visible = isVisable;
}
Hjalmar Z
  • 1,591
  • 1
  • 18
  • 36