views:

30

answers:

1

I've written a library which handles all the TCP/IP comms with our custom embedded hardware. It is used with most of our in house software and in the future could possibly be sold separately.

The most annoying thing this is that every time I handle events from the library, I have to have a seperate function to invoke through. I can only imagine that there is an easier way to do this that I just dont know...

Any ideas?

    public Setup(DiscoveryReader reader)
    {
        download = new DownloadFilesIndividual(Reader.ip, DateTime.Today);
        download.OnDownloadStatus += new DownloadStatusHandler(download_OnDownloadStatus);
    }

    void download_OnDownloadStatus(DownloadStatus status)
    {
        Invoke(new DownloadStatusHandler(this.safe_download_OnDownloadStatus), new object[] { status });
    }

    void safe_download_OnDownloadStatus(DownloadStatus status)
    {
        // Do UI stuff here
    }
+1  A: 

syntactical sugar

public Setup(DiscoveryReader reader)
{
    download = new DownloadFilesIndividual(Reader.ip, DateTime.Today);
    download.OnDownloadStatus += new DownloadStatusHandler(download_OnDownloadStatus);
}

void download_OnDownloadStatus(DownloadStatus status)
{
   if(InvokeRequired)
   {
    Invoke(new Action<DownloadStatus>(download_OnDownloadStatus),status);
   } else {
   // Do UI stuff here
   }
}
Andrew Keith
I like this! I didnt know about Action<T>. It would be good if I could somehow include the invoking into the library but im not sure thats possible!
Tim
why is "Invoke(new Action<DownloadStatus>(download_OnDownloadStatus, status));" giving me "Error - Method name expected"
Tim
Worked it out, it should be: Invoke(new Action<DownloadStatus>(download_OnDownloadStatus), status);
Tim