Reply
Solved! Go to solution

why does console application freeze during event handling?

[ Edited ]

Hello,

 

i wrote a console application that prints the application name to the console when solid edge fires BeforeCommandRun event.

 

But my event handler method and solid edge begin to freeze when the application variable is accessed within the event handler method.
After i press enter, freezing will end: "Waiting... 2" and the application name are written to the console. From now on event handler method and solid edge won't freeze anymore when the event is fired again. Why?

 

I already solved the problem by assigning the application variable within the event handler method (line 58). Now freezing doesn't occur at all. But i don't understand why.

 

// for a better clarity catch and finally methods and OLEMessageFilter are omitted

using OleMessageFilterNS;
using StaThreadSyncronizer;
using System;
using SolidEdgeFramework;
using System.Runtime.InteropServices;
using System.Threading;

namespace SeEvents
{
class Program
{
private static SolidEdgeFramework.Application application = null;
private static SolidEdgeFramework.DISEApplicationEvents_Event appEvents = null;

[STAThread]
static void Main(string[] args)
{

application = (SolidEdgeFramework.Application)Marshal.GetActiveObject("SolidEdge.Application");
appEvents = (SolidEdgeFramework.DISEApplicationEvents_Event)application.ApplicationEvents;
appEvents.BeforeCommandRun += new SolidEdgeFramework.DISEApplicationEvents_BeforeCommandRunEventHandler(appEvents_BeforeCommandRun);

Console.WriteLine("Waiting...1");
Console.ReadLine();
Console.WriteLine("Waiting...2");
Console.ReadLine();
}


//event handler method
static void appEvents_BeforeCommandRun(int theCommandID)
{
// application = (SolidEdgeFramework.Application)Marshal.GetActiveObject("SolidEdge.Application");

Console.WriteLine("Application Name: {0}", application.Name);
Console.WriteLine("Application Name was printed.");
}
}
}

 

3 REPLIES
Solution
Solution
Accepted by topic author Garfield
‎08-26-2015 04:32 AM

Re: why does console application freeze during event handling?

Console.ReadLine() is a blocking call. i.e. It will pause the current thread until input is entered.

 

While not impossible, it's difficult to receive events in a console application because it does not have a Windows Message Pump like its WinForm counterpart. Unless you have a really good reason, I would suggest doing this in a WinForm application. There are plenty of examples on Samples for Solid Edge on GitHub.

Jason Newell
Applications Architect
www.jasonnewell.net

Re: why does console application freeze during event handling?

Thank you a lot for your quick reply!!

Re: why does console application freeze during event handling?

[ Edited ]

You're welcome. To speed up your search to a sample, I would recommend starting with the EventHandling Sample.

Jason Newell
Applications Architect
www.jasonnewell.net