I have a C# progranm that is using a Flash control to play a movie. The problem I’m having is when I try to send a message to Actionscript before the movie has completely loaded I receive an error. To resolve the problem I set a timer to wait 1 second before continuing. That in turn created a different problem because I’m now running on a different thread. To resolve that problem I’ve tried using Invoke in the timer event handler but it doesn’t seem to actually invoke the method for some reason. I can see that 50% of the cpu is being used by the process but it never comes back from the Invoke statement. I have looked at a number of questions and I have tried 4 or 5 of different ways to use the Invoke and BegainInvoke methods. The design of my applications is that I have a main class that instantiates the form. The constructor of the form loads and starts to play the movie. The main class sets the timer and the tries to show the form, which is where the problem occurrs.
public MyClass()
{
demoForm = new DemoForm();
timer.Elapsed += new ElapsedEventHandler(engine);
timer.Interval = 1000;
timer.Enabled = true;
for (int i = 0; ; i++)
counter++;
}
private void engine(Object source, ElapsedEventArgs e)
{
timer.Enabled = false;
if (demoForm.InvokeRequired)
{
demoForm.BeginInvoke(new MethodInvoker(sendNextMessage));
}
else
{
sendNextMessage();
}
}
private void sendNextMessage()
{
// It never gets here!!!
}
This is an infinite busy loop:
The construction of your object will never complete, and you are probably blocking the GUI thread which will prevent any messages from being handled, including the
Invoke.You are probably seeing 50% CPU usage because you have a dual core processor. One of the cores is sitting in the busy loop repeatedly incrementing the counter. The other core is idle.
You say that you were using the loop to prevent the application from terminating, but this is not the correct way to do it. You should call
Application.Runfrom the main method. Visual Studio adds this code automatically for you when you create a new Windows Forms project.Suggestion
Create two forms. Put the Flash control in its own form and show that form first. When that form is closed, you can then open the second form to show the rest of your application.