0

I have a simple application which performs a search in the user's computer and then display's the results. I have a function search which searches for the files in the computer and a function DisplayForm which then display's the form containing the results. The results are displayed inside a ListBox.
Search function is called inside a BackgroundWorker and the DisplayFunction is called when the BackgroundWorker completes. The problem that I am facing is thate the first time the search function is called just after computer reboot , although the search function takes very less time (1 second or so), the time taken to display the form takes too long.(10 seconds or so).
Correct me if I am wrong,
based on what I have googled and read, I think the problem is that as soon as the BackgroundWorker completes, Assemblies required for DisplayForm start loading, which takes considerable time, and after that the form containing the search results is displayed.
I think the solution for the problem is that I can preload all the assemblies, so that when the DisplayForm function is called, there is not time wasted in loading the assemblies. I am completely new to this stuff, and don't have any clue on how to do it. I know about the System.Reflection.Assembly.Load() function, but I am unable to find "How to use it". So, basically I need help with using the Assembly.Load() function.
Below I am posting the code for DisplayForm function. Please have a look at it.

    private void DisplayForm()
    {
        Label LabelMessage = new Label();
        LabelMessage.Text = "There are a total of " + ReaderClass.SearchList.Count.ToString() + " files and folders found with the given name. ";
        LabelMessage.Parent = SearchForm;
        LabelMessage.Left = 300;
        LabelMessage.Top = 0;
        LabelMessage.Size = new Size(800, 75);
        LabelMessage.Font = new Font("Impact", 20);
        LabelMessage.Show();
        SearchForm.Controls.Add(LabelMessage);
        SearchForm.AutoScroll = true;
        ResultListBox.Parent = SearchForm;
        ResultListBox.DataSource = null;
        ResultListBox.DataSource = ReaderClass.SearchList;
        ResultListBox.Width = 1340;
        ResultListBox.Height = 1300;
        ResultListBox.BorderStyle = BorderStyle.Fixed3D;
        ResultListBox.ScrollAlwaysVisible = true;
        ResultListBox.Left = 0;
        ResultListBox.Top = 100;
        ResultListBox.DoubleClick += OpenFile;
        ResultListBox.KeyDown += OpenFileKey;
        ResultListBox.Font = new Font("Arial", 14, FontStyle.Regular);
        notifyIcon1.BalloonTipText = "Search Completed. It took " + (ReaderClass.SearchTime.ElapsedMilliseconds / 1000.0).ToString() + " to complete the search. ";
        notifyIcon1.ShowBalloonTip(2000);
        if (ReaderClass.SearchList.Count != 0)
        {
            ResultListBox.DataSource = ReaderClass.SearchList;
            SearchForm.ShowDialog();
        }
        else
        {
            MessageBox.Show("Sorry, we couldn't find anything with this name.", "No Results", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
        ReaderClass.SearchList.Clear();
        LabelMessage.Dispose();
    }
Pratik Singhal
  • 6,283
  • 10
  • 55
  • 97
  • So the second time a user search, with different query, the result is displayed much more quickly? – Martheen Nov 27 '13 at 04:17
  • Yes, very very quickly. In fact, the search query doesn't matter at all here.! – Pratik Singhal Nov 27 '13 at 04:18
  • What do you mean it doesn't matter? – Martheen Nov 27 '13 at 04:20
  • What I mean is that the time taken to display the form overrules the time taken by search function. for ex if query A took 1 second to search and query B took 3 seconds to search ,the form will be displayed in 10 seconds (approx) when running first time after reeboot, – Pratik Singhal Nov 27 '13 at 04:22
  • So as long as the user doesn't reboot, any query except the first will run fast? And the results are correct? Why not simply trigger a dummy search at the first launch and hardcode a special query that won't be displayed? – Martheen Nov 27 '13 at 04:25
  • Yes, any query other than the first will run fast, I used this idea, but the problem is "displaying", if I don't display the results of dummy search , the problem remains so , I have to display the results if I do dummy search which is not possible. If I don't display the results, the problem remains as it is. `Search` functions takes same time whether you run it after startup or subsequent times. – Pratik Singhal Nov 27 '13 at 04:28
  • Even if you set the Visible property to false? The way I see it you overload the DisplayForm constructor to accept a boolean parameter, and when it's set to true, the first thing it does is setting the Visible parameter to false. THEN, the Form_Load event would check if it's visible, and would close itself if it's not visible – Martheen Nov 27 '13 at 04:33
  • @Martheen Thanks, I will try it. It might solve the problem. – Pratik Singhal Nov 27 '13 at 04:34
  • Possible duplicate of [how to load all assemblies from within your /bin directory](http://stackoverflow.com/questions/1288288/how-to-load-all-assemblies-from-within-your-bin-directory) – Paul Sweatte Apr 15 '17 at 02:04

0 Answers0