Proper Programming using Events ?

Experimenter
Experimenter

Hi all,

 

I am slowly getting familiar with SE programming (VS C#.NET)

 

My code was unstable and after some trail and error, I found out that it seems that the code runs a-synchronosly.... sort of.

My solution is by looking at events to know when the code is idle, and then do the next task.

The Question to you all is: Is this the way, or I am missing something here ??

application.IsIdling and application.DoIdle didn't help me here.

 

I am curious to read your comments !

- jb

 

//in Class:
static private SolidEdgeFramework.ISEApplicationEvents_Event appEvents;
static private int  CommandBusy_Count = 0;
static private object locker = new object();
static void Test_Main ()
{
appEvents.BeforeCommandRun += AppEvents_BeforeCommandRun; appEvents.AfterCommandRun += AppEvents_AfterCommandRun;

// Before every task, call the waiter:
WaitingForIdle();
<do a task here>

WaitingForIdle();
<do next task>
}

private static void AppEvents_BeforeCommandRun ( int theCommandID )
{
lock ( locker )
CommandBusy_Count++;

//Console.WriteLine ( $"Before Event command ID: {FindCommand ( theCommandID )}" );
}

private static void AppEvents_AfterCommandRun ( int theCommandID )
{
lock ( locker )
CommandBusy_Count--;

//Console.WriteLine ( $"After Event command ID: {FindCommand ( theCommandID )}" );
}

// Here comes the magic:
// If there are more BEFORE events fired than AFTER events, then wait.
static private void WaitingForIdle ()
{
bool MsgShown = false;

DateTime DT1 = DateTime.Now;

lock ( locker )
{
while ( CommandBusy_Count > 0 )
{
if ( !MsgShown )
{
Console.Write ( "Waiting" );
MsgShown = true;
}
else
Console.Write ( "." );

Thread.Sleep ( 100 );

DateTime DT2 = DateTime.Now;
TimeSpan TS = DT2 - DT1;
if ( TS.TotalMilliseconds > 2000 ) // Timeout
break;
}
if ( MsgShown )
Console.WriteLine ( "" );
}
Thread.Sleep ( 50 );
}


 

 

2 REPLIES

Re: Proper Programming using Events ?

Solution Partner Phenom Solution Partner Phenom
Solution Partner Phenom
Hi,
I never used the idle event.
Is your program an addin or is it running in its own process?
What are you doing in the times solid ends is idelling?
My programs assume that the user start the action, either by starting my program, entering some parameters and waiting to finish or triggered by some event like the uses opens, saves or closes the document.
regards

JB
VS2015, SE ST10

Re: Proper Programming using Events ?

Experimenter
Experimenter

Hi JB,

 

I am working in 2 areas. One as C#, and the other in Excel VBA.

It seems that VBA run s a bit better, and it seems that after each App.Startcommand, a Doidle is needed.

 

What my program is doing during the idle: nothing, just waiting.

 

I do need to see if in c# a doidle would be enough, I just have to test that.

 

It seems that programming for Solid Edge requires a lot of head scratching...