If the receivers of the event absolutely must have the information back into thier own thread (like a Windows App sometimes does) then you would handle the invoke for arguments in those receiving threads.
Move your delegate out into the name space so that receivers outside of this class can use it. Really your choice, they can use fully qualified naming to get to it as well.
When the thread is ready to push the data into the event, then check to see if it is assigned, and then call it.
The receiver needs to check to see if invoke is required. Your Something class may itself be called from another thread, so to be safe, let the receivers deal with it.
(see more below this code snip)
using System.Threading;
namespace WindowsFormsApplication1
{
public delegate void onTimelineHandler(object sender, TimelineArgs e);
class SomeClass
{
public SomeClass() { }
public event onTimelineHandler OnTimelineRecieved;
public void TimelineAsyncStart(string url)
{
Thread myAsyncer = new Thread(new ParameterizedThreadStart(doTimelineAsync));
string[] objar = new string[] { url, "username", "password" };
myAsyncer.Start(objar);
}
private void doTimelineAsync(object objar)
{
string[] sa2 = (string[])objar;
string url = sa2[0];
string username = sa2[1];
string password = sa2[2];
object myobject = "411"; // just something to play with
//some stuff happens here
if( OnTimelineRecieved != null )
{
TimelineArgs targs = new TimelineArgs(myobject);
OnTimelineRecieved(null, targs);
}
}
}
public class TimelineArgs
{
public object Something = null;
public TimelineArgs(object something)
{
Something = something;
}
}
}
If the receivers need to invoke it, then use something like this:
void cs_OnTimelineRecieved(object sender, TimelineArgs e)
{
if ( this.InvokeRequired )
{
onTimelineHandler d = new onTimelineHandler(cs_OnTimelineRecieved);
this.Invoke(d, new object[] { sender, e });
}
else
{
string something = e.Something.ToString();
MessageBox.Show(something);
}
}
I work with many threads in an app, and they exchange information all the time without invoke. It is when I need to update a some non-thread safe object that I need to use Invoke.
Hope this helps,
Jerry