Click here to Skip to main content
15,892,839 members
Home / Discussions / C#
   

C#

 
GeneralRe: Stopping threads (semi-safely).... Pin
Jacob Dixon12-Apr-10 11:27
Jacob Dixon12-Apr-10 11:27 
GeneralRe: Stopping threads (semi-safely).... Pin
Luc Pattyn12-Apr-10 11:40
sitebuilderLuc Pattyn12-Apr-10 11:40 
GeneralRe: Stopping threads (semi-safely).... Pin
Jacob Dixon12-Apr-10 11:23
Jacob Dixon12-Apr-10 11:23 
GeneralRe: Stopping threads (semi-safely).... Pin
William Winner12-Apr-10 11:01
William Winner12-Apr-10 11:01 
GeneralRe: Stopping threads (semi-safely).... Pin
Jacob Dixon12-Apr-10 11:07
Jacob Dixon12-Apr-10 11:07 
GeneralRe: Stopping threads (semi-safely).... Pin
William Winner12-Apr-10 12:49
William Winner12-Apr-10 12:49 
GeneralRe: Stopping threads (semi-safely).... Pin
Jacob Dixon12-Apr-10 13:40
Jacob Dixon12-Apr-10 13:40 
AnswerRe: Stopping threads (semi-safely).... Pin
Jacob Dixon12-Apr-10 14:38
Jacob Dixon12-Apr-10 14:38 
Solution from all of your guys help:

I have tested this a couple times and it has yet to throw any errors on closing the form early. Still have plenty more testing to do though.

I create my event for when the applications have been loaded and start my threads
private delegate void ApplicationsLoadedDelegate(List<Product> products);
private event ApplicationsLoadedDelegate ApplicationsLoaded;

        private void frmCompInfo_Load(object sender, EventArgs e)
        {
            // Create our methods for handling data once its finished
            ApplicationsLoaded += new ApplicationsLoadedDelegate(frmCompInfo_ApplicationsLoaded);
            ServicesLoaded += new ServicesLoadedDelegate(frmCompInfo_ServicesLoaded);
            InfoLoaded += new InfoLoadedDelegate(frmCompInfo_InfoLoaded);


            // Start our threads for getting information
            appThread = new Thread(new ThreadStart(GetApplications));
            appThread.Start();

            driveThread = new Thread(new ThreadStart(GetInfo));
            driveThread.Start();

            serviceThread = new Thread(new ThreadStart(GetServices));
            serviceThread.Start();
        }


This is what I call which makes the call to the WMI DLL file I created for querying the data of a remote computer. It will return a List of my custom structure <product>. Before I do anything else with this I check and make sure that my ApplicationsLoaded is not null. If it is null that means the form has been closed. I do the invoke required blah blah blah because of another thread. I know you suggested not doing it, but I might call this from the main thread also (for the future). Plus the article you sent me the author was really fond of sticking to this technique.
        private void GetApplications()
        {
            WMI_Commands wmi = new WMI_Commands(Username, Password,
                System.Management.ImpersonationLevel.Impersonate, System.Management.AuthenticationLevel.Default);

            List<Product> products = wmi.InstalledApplications(NetBios);
            if (ApplicationsLoaded != null)
                ApplicationsLoaded(products);

            products = null;
        }
private void frmCompInfo_ApplicationsLoaded(List<Product> products)
        {
            if (lstApplications.InvokeRequired)
                lstApplications.Invoke(new ApplicationsLoadedDelegate(ApplicationsLoaded), new object[] { products });
            else
            {
                lstApplications.BeginUpdate();

                foreach (Product p in products)
                {
                    ListViewItem item = new ListViewItem();
                    item.Text = p.Name;
                    item.SubItems.Add(p.Version);
                    item.SubItems.Add(p.Vendor);
                    item.SubItems.Add(p.InstallLocation);

                    lstApplications.Items.Add(item);
                }

                lstApplications.EndUpdate();
                SetPBVisible(pbApplications, false);
            }
        }


On form closing I unload all of the events.
private void frmCompInfo_FormClosing(object sender, FormClosingEventArgs e)
{
    ApplicationsLoaded -= frmCompInfo_ApplicationsLoaded;
    ServicesLoaded -= frmCompInfo_ServicesLoaded;
    InfoLoaded -= frmCompInfo_InfoLoaded;
}




I've taken a couple of your suggestions and kind of compiled them together I think. Like I said I have tested this only a couple times by loading the form and watching it start the thread and closing the form before the thread was completed. I never saw any kind of error messages at all. (which is what I wanted)
GeneralRe: Stopping threads (semi-safely).... Pin
DaveyM6912-Apr-10 14:46
professionalDaveyM6912-Apr-10 14:46 
AnswerRe: Stopping threads (semi-safely).... Pin
DaveyM6912-Apr-10 14:44
professionalDaveyM6912-Apr-10 14:44 
QuestionMultithreaded drawing Pin
Groulien12-Apr-10 9:07
Groulien12-Apr-10 9:07 
AnswerRe: Multithreaded drawing Pin
Tarakeshwar Reddy12-Apr-10 9:37
professionalTarakeshwar Reddy12-Apr-10 9:37 
AnswerRe: Multithreaded drawing Pin
ntrceptr12-Apr-10 9:46
ntrceptr12-Apr-10 9:46 
AnswerRe: Multithreaded drawing Pin
William Winner12-Apr-10 10:16
William Winner12-Apr-10 10:16 
QuestionUser and Password Prompt Pin
Jassim Rahma12-Apr-10 5:59
Jassim Rahma12-Apr-10 5:59 
AnswerRe: User and Password Prompt Pin
Dave Kreskowiak12-Apr-10 6:14
mveDave Kreskowiak12-Apr-10 6:14 
GeneralRe: User and Password Prompt Pin
Jassim Rahma12-Apr-10 6:28
Jassim Rahma12-Apr-10 6:28 
GeneralRe: User and Password Prompt Pin
J4amieC12-Apr-10 6:35
J4amieC12-Apr-10 6:35 
AnswerRe: User and Password Prompt Pin
Ravi Bhavnani12-Apr-10 19:17
professionalRavi Bhavnani12-Apr-10 19:17 
QuestionWebBrowser Control - .NET 3.5 - C# Pin
Jon Braunsma12-Apr-10 4:58
Jon Braunsma12-Apr-10 4:58 
AnswerRe: WebBrowser Control - .NET 3.5 - C# Pin
Not Active12-Apr-10 5:26
mentorNot Active12-Apr-10 5:26 
AnswerRe: WebBrowser Control - .NET 3.5 - C# Pin
itsravie12-Apr-10 5:46
itsravie12-Apr-10 5:46 
AnswerRe: WebBrowser Control - .NET 3.5 - C# Pin
Jon Braunsma12-Apr-10 9:40
Jon Braunsma12-Apr-10 9:40 
GeneralRe: WebBrowser Control - .NET 3.5 - C# Pin
Dave Kreskowiak12-Apr-10 9:55
mveDave Kreskowiak12-Apr-10 9:55 
Questionhow to get textbox value from one class to another Pin
Sr...Frank12-Apr-10 4:44
Sr...Frank12-Apr-10 4:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.