Hi, i am trouble to find out this problem. I have develop an windows application in that one form contains the datagridview(see attachments). When i click on Download it will displayed lable on datagridview displaying message Downloading Please wait... . When i am clicking on download a am enable datagridview to false for the not allowing any click on it but after some (4 to 5 clicks) it will hang or close the application abnormally.
How i can figure out this problem.

Thanks in advance.

Recommended Answers

All 4 Replies

Debugging?
Or if you don't want the user to click your DGV, hide the window and show it again after the download.

Sounds like you might be doing the download in the UI thread.
If this is the case then the whole UI is probably hung waiting for the download to finish.
The easiest way to manage a big task in a windows forms app is to use a BackgroundWorker.

The code sample below shows how to use one and get feedback when it finishes.

private void button1_Click(object sender, EventArgs e)
{
	var bgw = new System.ComponentModel.BackgroundWorker();
	bgw.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
	bgw.DoWork += new System.ComponentModel.DoWorkEventHandler(bgw_DoWork);
	bgw.RunWorkerAsync();
}

void bgw_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
	// execute the big task
	BigTask();
}

void bgw_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
	// Report big task done
	MessageBox.Show("Task Done.");
}

private void BigTask()
{
	for (int i = 0; i < 50; i++)
	{
		System.Threading.Thread.Sleep(100);
	}
}

Here is what you can do:
I would use a backgroundworker, and do the download work in it:

private delegate void DownloadDelegate(string msg);
        private BackgroundWorker bgv;

        public Form1()
        {
            InitializeComponent();
            //create bgv:
            bgv = new BackgroundWorker();
            bgv.ProgressChanged += new ProgressChangedEventHandler(bgv_ProgressChanged);
            bgv.DoWork += new DoWorkEventHandler(bgv_DoWork);
            bgv.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgv_RunWorkerCompleted);

            //label will be used as a note of a progress
            label1.Text = "";

            //creating columns:
            DataGridViewButtonColumn btnColumn = new DataGridViewButtonColumn();
            btnColumn.HeaderText = "Dowload";
            btnColumn.Name = "column2";
            btnColumn.Text = "download";
            btnColumn.UseColumnTextForButtonValue = true;

            dataGridView1.Columns.Add("column1", "Text Name");
            dataGridView1.Columns.Add(btnColumn);

            //adding some example rows:
            for (int i = 1; i < 6; i++)
                dataGridView1.Rows.Add("Test number " + i);

            //adding event:
            dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == dataGridView1.Columns["column2"].Index)
            {
                //disable dgv until download runs our:
                //dataGridView1.Enabled = false;
                bgv.RunWorkerAsync();
                //disable dgv:
                dataGridView1.Enabled = false;
            }
        }

        void bgv_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (!(e.Error == null))
                label1.Text = "Error: " + e.Error.Message;
            else
            {
                label1.Text = "Done!";
                //enable dgv:
                dataGridView1.Enabled = true;
            }
        }

        void bgv_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            worker.WorkerReportsProgress = true;
            worker.WorkerSupportsCancellation = true;

            //
            //THIS IS WHERE YOU PUT YOUR DOWNLOAD CODE!!!
            //
            //this is only my example code of time consuming:
            int total = 50; //some variable
            for (int i = 0; i < total; i++)
            {
                if (worker.CancellationPending)
                {
                    e.Cancel = true;
                    break;
                }
                else
                    worker.ReportProgress((100 * i) / total);
                //stop a bit:
                System.Threading.Thread.Sleep(50);
            }
        }

        void bgv_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            label1.Text = " Download progress: " + e.ProgressPercentage.ToString() + " %";
        }

Thanks to all for reply it works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.