Shoutbox

[C#] EventLog and Threads - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Forum: Plug-Ins (/forumdisplay.php?fid=28)
+------ Thread: [C#] EventLog and Threads (/showthread.php?tid=52036)

[C#] EventLog and Threads by Jedimark on 10-21-2005 at 05:01 PM

Quick question: does anyone know how to write to the event log in a multi-threaded environment? MsgPlus just chucks the request away if it doesn't come from the main thread (which isn't very functionable reallly!)


RE: [C#] EventLog and Threads by J-Thread on 10-21-2005 at 06:58 PM

It's a very interesting question.... Jedimark and I searched for the problem a while, and suddenly I remembered you can't write to the Event log from another Thread...

So the question should be (at least that's what I think): how to make another thread execute a function?


RE: [C#] EventLog and Threads by Jedimark on 10-21-2005 at 07:02 PM

Yeah, the problem is that MLP grabs song information from a Listener thread; I can't fundamentally change that... so I'm stuck with figuring out how to pass back a few string values to the main. But this goes beyond my programming knowledge...


RE: [C#] EventLog and Threads by J-Thread on 10-21-2005 at 07:08 PM

An option might be to make an array, and everytime you want to add something to the event log you add it to the array (in the listener thread). In your main function there is a timer that checks the array every ... seconds (10 or so) and writes every line to the event log, and then clears the array...

It may be a strange option, but it might work. Although I'm sure there must be better options!


RE: [C#] EventLog and Threads by Jedimark on 10-21-2005 at 07:47 PM

Yeah I'd though of a similar function, I'll do a bit more thread research before I start messing round with stuff like that though.


RE: [C#] EventLog and Threads by Jedimark on 10-22-2005 at 10:07 AM

The Solution:

Delegates!

I.e:

code:
private delegate void addEventLogDelegate(String thisContact, String thisTitle, String thisArtist);

And then in the other thread (one that isnt the main):
code:
addEventLogDelegate d = new addEventLogDelegate(this.addEventLog);
                        Object[] oArgs = new Object[3];
                        oArgs[0] = myUBX.getContact();
                        oArgs[1] = myUBX.getSong();
                        oArgs[2] = myUBX.getArtist();
                        this.Invoke(d, oArgs);