Rashim's Blog

Archive for the ‘Win Form’ Category

We, the developers, always try to look into the performance when we develop an application. Is there any developer who wants his application to freeze or smash? But what are the steps a developer should yield to keep it all smooth going. When I used to develop this application, I saw whenever I attempt to copy files from one directory to other it includes CPU as well as hard drive. The major fallback of it is to limit a user to execute one task at a time. So how do we deal with application freezing and smashing?

By using threads through which we can handle all the jobs simultaneously under one main application thread. You can perform smaller operations with the help of threading class; instead I prefer using Background Worker class to implement to perform all my heavy duty operations. Background Worker is used to execute long running operations on a separate thread, support cancellation, progress reporting and marshalling results back to the UI thread. As I have offered the smooth UI which shows the operation details and at the same time the operation will continue in the background, I choose this one to use. If you want to know in details about the Background Worker, you can read this article.

The main mechanism is that when the user clicks the start button, it issues the event OnCopyStarted and the event handler is responsible for Copying data while at the same time OnUIChanged was getting issued and do some enable/disable UI Controls, when we click stop button, it issued the OnCopyStoppedEvent and at the same time it checks the Background worker’s working status does Background worker finish/Cancel its task or not. If not, it pops up the dialog window for a moment and waits for Background worker to be free, once the Backgroundworker gets free, the Dialogue will automatically dispose. Here I have used one way binding. So any changes in property value will reflect to the UI but changing the UI will not reflect on the properties.

You will find the full article here[^]


Say, you have two Forms namely FrmMain and FrmDialog and you want to show up FrmDialog from FrmMain. When you click OK/Yes button then it might return DialogResult and Fill Up some variables data that you need in your FrmMain for further processing.  So what should we do in this case has been given below,

  1. Design Your FrmMain.cs
  2. Design your FrmDialog.cs as a Dialog Window
    1. from the property window set controlBox=False
    2. Add Two Buttons Ok and Cancel

In the below code,GetData() method opens up the Dialog window. When we Click Okay/Cancel button, it set up DialogResult for current window also fill-up the variable data as well.

private static List<Test> currentList = null;

        public static bool GetData(ref List<Test> referenceList)
            FrmDialog frm = new FrmDialog();
            if (frm.ShowDialog() == DialogResult.OK)
                referenceList = currentList;

        private void BtnOk_Click(object sender, EventArgs e)
            currentList = this.GetData();
            this.DialogResult = DialogResult.OK;

        private void BtnCancel_Click(object sender, EventArgs e)
            this.DialogResult = DialogResult.Cancel;

Call the GetData() method from FrmMain.cs like the way,

List<Test> currentList=null;

        if (FrmDialog.GetData (ref currentList)== DialogResult.OK)
            ////Do Whatever you want

%d bloggers like this: