Hi all,
I am trying to write a software where I want to put some UI in the form that would indicate the loading process(progress bar, loading image etc).
Please advice how to do it since when a form runs some code all the form gets static. Threading is also not helping either

Recommended Answers

All 9 Replies

You have to update components by calling them with pinvoke - theres some great examples that come in your helpfile

I know nothing abt PInvoke. I searched the net and All I get is that it is used to include functionality of different dlls.

how is going to solve my problem.

LizR you sure you are meaning pinvoke?

i do it by invokerequired

this example here, is setting a button enabled

private delegate void UpdateBtnContinueDelegate();
        private void UpdateBtnContinue()
        {
            if (this.InvokeRequired)
            {
                UpdateBtnContinueDelegate updateCount = new UpdateBtnContinueDelegate(this.UpdateBtnContinue);
                this.Invoke(updateCount);
            }
            else
            {
                this.btnContinue.Text = "Continue";
                this.btnContinue.Enabled = true;
            }
        }

from your code you would just call UpdateBtnContinue() and let the method take care of the invoke

I think I have been unable to eleborate the problem.

I want to load a combo box from a data base. Now, when I make a connection to the data base and retrieve the data, it takes time. and in that time the form remains statix and gives a look of hanged application. I want to overcome this. I want the form to display some sort of progress but its not working that way. I am unable to use thread since it does not allow to access form components and load them with value or alter their properties.

the concept is right, i gave you an example with a button, you can do the same with a progress bar


this assumes you have progressbar name pbStatus

private delegate void UpdatePbDelegate();
        private void UpdatePb()
        {
            if (this.pbStatus.InvokeRequired)
            {
                UpdatePbDelegate updateCount = new UpdatePbDelegate(this.UpdatePb);
                this.pbStatus.Invoke(updateCount);
            }
            else
            {
                if(this.pbStatus.Value == 100)
		{
			this.pbStatus.Value = 1;
		}
                this.pbStatus.PerformStep();
                this.pbStatus.Refresh();
            }
        }

sorry forgot the thread part

private void DownloadData()
{
//your code in here that downloads your values
}

private void GetDropDownValues()
{
 Thread t= new Thread(new ThreadStart(DownloadData));
            t.Start();

            while (t.IsAlive)
            {
                UpdatePb();
                System.Threading.Thread.Sleep(50);
            }
}

also, i wouldn't recommend it, but you can call UpdatePb() from anywhere in the code, because it will invoke itself if it is a cross thread call

LizR you sure you are meaning pinvoke?

Darn it, its what comes when Im mid code and doing something else .. (and then go out to do something) sigh - see previous version where I kept saying textbox not textbook.. :(

Yes, I meant invoke. The silly part is, if you try calling the UI from a thread, it would have told you in the helpfile what to do when it crashed..

Lol lizr i saw your textbox comment the other day, and thought you must have been a little preoccupied.

Ans as Lizr said, its in the helpfile along with the example i gave you.

To avoid the problem of Form freezing you can use the asynchrone mode to load data from the data base

using System;
using System.Data;
using System.Threading;
using System.Data.SqlClient;

class MainClass {
    public static void CallbackHandler(IAsyncResult result) {
        using (SqlCommand cmd = result.AsyncState as SqlCommand) {
            using (SqlDataReader reader = cmd.EndExecuteReader(result)) {
                lock (Console.Out) {
                    while (reader.Read()) {
                        Console.WriteLine("  {0} = {1}",
                            reader["ID"],
                            reader["FirstName"]);
                    }
                }
            }
        }
    }

    public static void Main()
    {
        using (SqlConnection con = new SqlConnection())
        {
            con.ConnectionString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;Asynchronous Processing=true;";

            SqlCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT ID, FirstName FROM Employee";

            con.Open();
            cmd.BeginExecuteReader(CallbackHandler, cmd);

            // Continue with other processing.
            for (int count = 0; count < 10; count++)
            {
                lock (Console.Out)
                {
                    Console.WriteLine("{0} : Continue processing...",
                        DateTime.Now.ToString("HH:mm:ss.ffff"));
                }
                Thread.Sleep(500);
            }
        }
    }
}
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.