views:

36

answers:

0

Hello Friends,

I am new to .NET Windows application & unfortunately, to the MultiThreading. My problem is as below:

I have application in which I spawn three threads (3: activity thread, snapshot thread, upload thread) which every minute take snapshot of screen and logs mouse-keyboard activity in database. Now, every time I insert a record in database I check database limit. If it exceeds a particular value, I need to quit my application. My activity thread performs the check and I want to quit my application using the activity check. Now I tried to marshal the form-closing using delegate, but for some reason its not working please take a look at my code. Also, please suggest an improved version. Moreover, I also want to abort all of my thread if database limit has reached.Please suggest a solution.

Thanks

Code goes like this

    delegate void TestClose(System.Windows.Forms.Form frm);

    private static void CloseF(System.Windows.Forms.Form f)
    {

        if (f.InvokeRequired)
        {

            f.Invoke(new TestClose(CloseF), new object[] { f });
        }
        else
        {
            f.Close();
        }
    }

    public static void CloseAllFrm()
    {

        System.Windows.Forms.FormCollection formCol = System.Windows.Forms.Application.OpenForms;
        System.Windows.Forms.Form[] frmArray = new System.Windows.Forms.Form[formCol.Count];
        int i = 0;
        foreach (System.Windows.Forms.Form frm in formCol)
        {
            if (frm.Name != "Form1") // for not closing the main form so that application doesn't exits; only logs out user
            {
                frmArray[i] = frm;
                i++;
            }
        }
        for (i = 0; i < frmArray.Length; i++)
        {
            if(frmArray[i] != null)
                CloseF(frmArray[i]);

        }