Problem with closing a form after FlashCall

.NET Edition of the F-IN-BOX
Roomie
Posts: 22
Joined: Wed Dec 19, 2007 6:05 am

Problem with closing a form after FlashCall

Postby Roomie » Wed Dec 19, 2007 3:54 pm

Hey there, hopefully someone can help me out here.
I'm having problem closing a form after handling a FlashCall.
After reading some on the forum here I found this topic:
External interface & Form.Close() Problem
In the last post by ovidiu he gives the following solution:

try setting a private variable on your form called closeWhenIdle, which is initially set to false. when u call the method from flash, set this variable to true and do nothing else
next, make sure that in the load event of your form u register the following event:


Application.Idle += new EventHandler(Application_Idle);

this event handler is called continuously when the application has nothing to do in terms of processing (don't worry about performance problems)

the method should look like this;

Code: Select all

private void Application_Idle(object sender, EventArgs e)
{
    if (this.CloseWhenIdle)
    {
        this.Close();

        // useful when the OnClosing method was ovverridden
        // and e.Cancel was set to true inside of it
        this.CloseWhenIdle = false;
    }
}



But for some reason this doesn't work for me. Here's my document, please tell me if you see anything wrong with it.

Code: Select all

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Xml;

namespace Pausit
{
   /// <summary>
   /// Summary description for FormIntro.
   /// </summary>
   public class FormIntro : f_in_box__lib.f_in_box__form
   {

        private bool CloseWhenIdle = false;
        private FormSettings m_parent;

        public FormIntro(FormSettings frm1)
      {
         //
         // TODO: Add constructor logic here
         //
                     
         InitializeComponent();
         m_parent = frm1;
      }
       
      private void InitializeComponent()
      {
            this.SuspendLayout();
            //
            // FormIntro
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(200, 200);
            this.Context = "private";
            this.Name = "FormIntro";
            this.ShowInTaskbar = false;
            this.Text = "Pausit";
            this.Load += new System.EventHandler(this.FormIntro_Load);
            this.OnFlashCall += new f_in_box__lib.f_in_box__form.OnFlashCallEventHandler(this.FormIntro_OnFlashCall);
            this.ResumeLayout(false);

      }

        private void FormIntro_Load(object sender, EventArgs e)
        {
            Application.Idle += new EventHandler(Application_Idle);
        }

       

        private void FormIntro_OnFlashCall(object sender, string request)
        {
            string strCommand;

            XmlDocument FlashXMLDoc = new XmlDocument();
            FlashXMLDoc.LoadXml(request);
           
            strCommand = FlashXMLDoc.GetElementsByTagName("invoke").Item(0).Attributes.GetNamedItem("name").Value;

            if (strCommand == "ExitIntro")
            {
                CloseWhenIdle = true;
            }
            else if (strCommand == "StartExercise")
            {
                //MessageBox.Show("StartExercise");
                m_parent.ShowExercises();
            }

        }

        private void Application_Idle(object sender, EventArgs e)
        {
            if (this.CloseWhenIdle)
            {
                this.Close();
                this.CloseWhenIdle = false;
            }
        }



   }
}


Appreciate any help I can get.

Softanics
Site Admin
Posts: 1402
Joined: Sat Sep 18, 2004 3:03 am
Location: Russia, St. Petersburg
Contact:

Postby Softanics » Wed Dec 19, 2007 4:10 pm

Thank you for your question.

So this handler is not called at all?

I like the solution with a timer...
Best regards, Artem A. Razin,
F-IN-BOX support
Ask your question here: http://www.f-in-box.com/support.html

Roomie
Posts: 22
Joined: Wed Dec 19, 2007 6:05 am

Postby Roomie » Fri Dec 21, 2007 7:08 am

I tried the timer solution but gets the same error as Silvia Tosolini, "Cross-thread operation not valid" exception, and I'm not sure how to use delegates but I guess I could look into that.

If you have any more help it would be appriciated, like an example.
Thanks for your answer though.

Softanics
Site Admin
Posts: 1402
Joined: Sat Sep 18, 2004 3:03 am
Location: Russia, St. Petersburg
Contact:

Postby Softanics » Fri Dec 21, 2007 9:45 am

Roomie wrote:I tried the timer solution but gets the same error as Silvia Tosolini, "Cross-thread operation not valid" exception, and I'm not sure how to use delegates but I guess I could look into that.

If you have any more help it would be appriciated, like an example.
Thanks for your answer though.


If you are familiar with winapi, you can use PostMessage(Handle, WM_CLOSE). It should work well.
Best regards, Artem A. Razin,

F-IN-BOX support

Ask your question here: http://www.f-in-box.com/support.html

Roomie
Posts: 22
Joined: Wed Dec 19, 2007 6:05 am

Postby Roomie » Thu Jan 17, 2008 1:12 am

Hi again, I know it's been a while but would you mind helping me out with the WinAPI. If you could just write an example code on how to use the PostMessage(Handle, WM_CLOSE) I would appriciate it a lot.

Thanks.

Softanics
Site Admin
Posts: 1402
Joined: Sat Sep 18, 2004 3:03 am
Location: Russia, St. Petersburg
Contact:

Postby Softanics » Thu Jan 17, 2008 8:08 am

Roomie wrote:Hi again, I know it's been a while but would you mind helping me out with the WinAPI. If you could just write an example code on how to use the PostMessage(Handle, WM_CLOSE) I would appriciate it a lot.

Thanks.


Yes, of course:

Code: Select all


[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
public static extern bool PostMessage(
IntPtr hWnd,// handle to destination window
UInt32 Msg, // message
IntPtr wParam, // first message parameter
IntPtr lParam // second message parameter
);

public const int WM_CLOSE = 0x0010;

...

PostMessage(Handle, WM_CLOSE, 0, 0);

Best regards, Artem A. Razin,

F-IN-BOX support

Ask your question here: http://www.f-in-box.com/support.html

Roomie
Posts: 22
Joined: Wed Dec 19, 2007 6:05 am

Postby Roomie » Thu Jan 17, 2008 2:33 pm

Thank you, it works like a charm now :D

BTW, in the new release you state that "AxCode implements IDisposable" does that have anything to do with my problem of closing the form?

Softanics
Site Admin
Posts: 1402
Joined: Sat Sep 18, 2004 3:03 am
Location: Russia, St. Petersburg
Contact:

Postby Softanics » Thu Jan 17, 2008 6:23 pm

Roomie wrote:Thank you, it works like a charm now :D


Good!

Roomie wrote:BTW, in the new release you state that "AxCode implements IDisposable" does that have anything to do with my problem of closing the form?


No... It's useful when you write the code like that:

Code: Select all

using (AxCode ax_code = new AxCode(...))
    using (f_in_box__form form = new f_in_box__form(ax_code))
    {
        ...
    }


After executing this block, be sure that ax_code is deallocated. In the past, code is freed by Garbage Collector when .NET Runtime decides.
Best regards, Artem A. Razin,

F-IN-BOX support

Ask your question here: http://www.f-in-box.com/support.html


Return to “.NET Edition”

Who is online

Users browsing this forum: No registered users and 13 guests

cron