xsharp.eu • ILSpy translation gives compiler error
Page 1 of 1

ILSpy translation gives compiler error

Posted: Thu Feb 23, 2023 8:59 pm
by ic2
This compiles in C# (to set a timer calling MyMethod every 5 minutes)

Code: Select all

var timer = new System.Threading.Timer(
    e => MyMethod(),  
    null, 
    TimeSpan.Zero, 
    TimeSpan.FromMinutes(5));

ILSpy translates that to:

Code: Select all

	Timer{dueTime: TimeSpan.Zero, period: TimeSpan.FromMinutes(5.0), Callback: { =>Self:MyMethod()	}, state: null}
	Close()

This gives the following compiler eror:

Error XS9002 Parser: unexpected input '{'

1 What is wrong?
2 I I assume it needs to be fixed in the ILSpy plugin?

Dick

ILSpy translation gives compiler error

Posted: Fri Feb 24, 2023 12:35 am
by Chris
Hi Dick,

Indeed, that's not the correct translation. In X# syntax, the equivalent is:

Code: Select all

VAR timer := System.Threading.Timer{;
    MyMethod,;
    NULL,;
    TimeSpan.Zero,;
    TimeSpan.FromMinutes(5)}
and of course you also need to have a method (or function) defined that will be called when the timer ticks:

Code: Select all

METHOD MyMethod(state AS OBJECT) AS VOID
.