views:

75

answers:

2

I am working on a DirectX based simulator. On which I have to check for a device whether device has been plugged-in or removed from the PC.

I've managed to make classes for device arrival and removal on another thread, which raises an event from the thread itself on device arrival or removal. The corresponding event method is being called in the main form and there:

Assume Form1 is main window and Form2 is the secondary.

Form2 form2Instance = new Form2();

I want to show another Form (Form2) keeping main Window (Form1) in behind (same as it behaves as form2Instance.ShowDialog(); in general cases.) After a few tries I have done it by

Applicatin.Run(new Form2());, but the Form2 doesn't behave as'form2Instance.ShowDialog(); in any way.

Just giving the code if it can help in answering:

iARMdetectionThreadClass detection;
InProgram_iARMdetection iARMStatusGUI;

 private void Form2_Load(object sender, EventArgs e)
        {
         iARMStatusGUI = new InProgram_iARMdetection();
         detection = new iARMdetectionThreadClass();
         detection.IniARM_device_Arrive += new iARMdetectionThreadClass.iARM_device_ArrivedEventHandler(detection_IniARM_device_Arrive);
         detection.IniARM_device_Remove += new iARMdetectionThreadClass.iARM_device_RemovedEventHandler(detection_IniARM_device_Remove);
         detection.startThread();
        }


 void detection_IniARM_device_Remove(iARM_deviceInfo senderInfo)
        {

            detection.StopCheckBeingRemoved();
            MethodInvoker act = delegate
            {
                this.label_iARMStatus.Text = detection.iARM_deviceInf.iARMStatus;
            };
            this.label_iARMStatus.BeginInvoke(act);

            Application.Run(new InProgram_iARMdetection()); //Blocking code

            detection.StartCheckBeingRemoved();

        }

 void detection_IniARM_device_Arrive(iARM_deviceInfo senderInfo)
        {
            MethodInvoker act = delegate
            {
                this.label_iARMStatus.Text = detection.iARM_deviceInf.iARMStatus;
            };
            this.label_iARMStatus.BeginInvoke(act);
            //detection.StopCheckArriving();
            //detection.StartCheckArriving();
        }

I need the code to be Blocking Code. In here:

Application.Run(new InProgram_iARMdetection()); //Blocking code
A: 

You should handle the remove event on form2 rather than form1 and use the ShowDialog() method.

So when on form1 the arrival event will fire it will open up form2 like a dialog. Now when the device will be unplugged, on form2 removal event will fire where you can close the form.

Aseem Gautam
+1  A: 

Perhaps mainform.AddOwnedForm(form2) will do what you want. It will make form2 display in front of mainform and when either one is minimized, the other is also.

Virtlink
Yes, or just `form2.Show(this)`
Henk Holterman
Not working as needed. :( If you need any other reference plz mention.
Rahul2047
When I try AddOwnedForm() then when I call .show() it shows:System.InvalidOperationException: Cross-thread operation not valid: Control 'OnProgram_iARMdetection' accessed from a thread other than the thread it was created on. at System.Windows.Forms.Control.get_Handle() at System.Windows.Forms.Form.UpdateHandleWithOwner() at System.Windows.Forms.Form.set_Owner(Form value) at System.Windows.Forms.Form.AddOwnedForm(Form ownedForm)OnProgram_iARMdetection has been instantiated in the main form itself and both addOwnedForm() and Show() fns are being called from main form itself.
Rahul2047