Just Do It
Just Do It

Reputation: 486

Slowing down a function

I have a winforms app where I can select between 2 companies(databases) and access their articles/items to either block or unblock them. I do it through the SAP B1 SDK. The function works, it does everything as expected. The "problem" is that users don't like how fast it is(users, am I right?) and they asked me to make it slower and add some visual features(progressbar, lock/unlock pictures, additional labels) so they'll know which article is being blocked/unblocked.

This is the function where I lock/unlock the articles:

private void items()
        {
           string[] lines = textBox1.Lines;

           for (int i = 0; i <= lines.GetUpperBound(0); i++)
           {
               oItem = ((SAPbobsCOM.Items)(oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oItems)));
               oItem.GetByKey(lines[i]);
               if (comboBox1.Text == "Lock")
               {
                   oItem.Frozen = BoYesNoEnum.tYES;
               }
               else if (comboBox1.Text == "Unlock")
               {
                   oItem.Frozen = BoYesNoEnum.tNO;
               }
               lRetCode = oItem.Update();
               if (lRetCode != 0)
               {
                   oCompany.GetLastError(out lErrCode, out sErrMsg);
                   MessageBox.Show(lErrCode + " " + sErrMsg + " item: " + lines[i]);
               }
           }
        }

What could possibly be the best and correct way to achieve this? I tried using Thread.Sleep but after several failed attempts I couldn't find where to insert the line of code, I tried before, middle and after operations and I had no success. Any ideas/suggestions will be greatly appreciated.

Upvotes: 0

Views: 151

Answers (1)

Marshal
Marshal

Reputation: 6671

Locking and Unclocking is taking place at these statements oItem.Frozen = BoYesNoEnum.tYES; and oItem.Frozen = BoYesNoEnum.tNO;. So you can't literally "go between" execution of that statements. All you can do is enable WaitCursor before Thread.Sleep and then let freezing/unfreezing happen. This won't prevent locking and unlocking from going fast, but users will feel that the process is taking some time because of WaitCursor effect.

Summing up: Waitcursor is enabled for 1 second- for visual effect, and we delude the users that something is going behind the scenes, but actually nothing is happening except changing of cursors!

You can make use of Thread.Sleep with WaitCursor like:

           if (comboBox1.Text == "Lock")
           {
               Cursor.Current = Cursors.WaitCursor;
               Thread.Sleep(1000);
               oItem.Frozen = BoYesNoEnum.tYES;
               Cursor.Current = Cursors.Default;
           }
           else if (comboBox1.Text == "Unlock")
           {
               Cursor.Current = Cursors.WaitCursor;
               Thread.Sleep(1000);
               oItem.Frozen = BoYesNoEnum.tNO;
               Cursor.Current = Cursors.Default;
           }

Upvotes: 1

Related Questions