Alex
Alex

Reputation: 2144

C#/Winforms App freezing/lagging

I have an Application in C#/Winforms that is basically used to run test for customers accounts in a specific order/setup. They are all tests run in browsers and so I built it so it would automate the process since there are about 10-12 tests that need to be ran everytime a customer account is pulled up.

Basically what happens is you input the account number then it runs the tests.

Here's a sample code.

public void startTestProcess()
    { 
        if (!cancelTests)
        {
            testRunning = true;
            var tabPage = createTabPage(banToRun, Convert.ToInt32(banToRun));
            loadingBox.Visible = true;
            mainTabControl.TabPages.Insert(0, tabPage);
            mainTabControl.SelectedTab = mainTabControl.TabPages[0];
            runTest(tabPage, banToRun);
            loadingBox.Visible = false;
        }
    }

private void runTest(TabPage t, string ban)
    {
        if (!cancelTests && !cancelCurrentOnly)
        {
            var tC = createInitialTabControl();
            t.Controls.Add(tC);
            int[] theTests = profileInfo.getSetList;
            for (int i = 0; i < theTests.Length; i++)
            {
                if (!cancelTests && !cancelCurrentOnly)
                {
                    var newTab = createTabPage(urlStrings.getName(theTests[i]), theTests[i]);
                    tC.TabPages.Add(newTab);
                    var webBrowser = createBrowser(urlStrings.getUrl(theTests[i], ban));
                    newTab.Controls.Add(webBrowser);
                    if (theTests[i] != 0 && theTests[i] != 1 && theTests[i] != 6
                        && theTests[i] != 11 && theTests[i] != 12)
                    {
                        if (!webBrowser.IsDisposed)
                        {
                            try
                            {
                                while (webBrowser.ReadyState != WebBrowserReadyState.Complete)
                                {
                                    Application.DoEvents();
                                }
                            }
                            catch
                            {
                                //Do Nothing
                            }
                        }
                    }
                    IntPtr pHandle = GetCurrentProcess();
                    SetProcessWorkingSetSize(pHandle, -1, -1);
                }
            }
        }
        if (cancelCurrentOnly)
        {
            cancelCurrentOnly = false;
        }
        banToRun = string.Empty;
        testRunning = false;

    }

So basically my question is, how can I optimize what I have in order to

A. Reduce lag/freezing - Note: Already implemented a way of forcing garbage collection after each test is run. B. Improve performance of the WebBrowser controls possibly? - Already tried some webbrowser alternatives like WebKit for C# Wrapper (does not work on all tests due to some ajax based coding i believe) C. Maybe implement multi-threaded operations. Not sure how i'd go about this without having cross-threaded exceptions thrown.

Thanks for your assistance. If you have any other questions feel free to ask.

Upvotes: 3

Views: 4318

Answers (2)

Mark Avenius
Mark Avenius

Reputation: 13947

The lag / freezing issue is caused by you not using multiple threads; therefore all of the test runs are using the UI thread, so the UI cannot respond while the tests are being run.

If the reason you are not using a backround worker(s) is that your are worried about having cross-threaded exceptions thrown, then you just need to make sure you are properly passing information between your threads (as opposed to avoiding multithreading altogether).

Update

This question addresses the issue of updating the UI based on workers' progress.

As a side note, you should not have to force garbage collection; most of the time, this will actually decrease performance. The garbage collector is specifically designed to collect when it has availability to do so (when the CPU is available). Forcing it to run takes cycles away from the real work your app is trying to do.

Upvotes: 6

KeithS
KeithS

Reputation: 71591

I see a lot of heavy lifting being performed in methods that also handle GUI, and therefore I assume that all of this is being done in the application's main thread (which will block while performing non-graphic operations related to that WebBrowser and other areas).

Try to refactor this application to run in multiple threads. The main thread should be available as much as possible to respond to user events. Background threads should do the heavy lifting of creating expensive objects, performing long-running read/write operations, etc.

Upvotes: 2

Related Questions