Problems with "ISynchronizeInvoke" and "this" in C#. I think some sort of Invoke can fix it, but I can't work out how!
<p>Thank you so much for reading this! I think the fix is relatively easy, but it is beyond me! I can never get my BeginInvokes correct. </p>

<p>I am still learning, and not very good yet, so please don't laugh too hard! As a temporary measure, I am using some public domain code, just to get a feel for what is being done in the code, before I write some similar stuff with only the bits I actually need. However, I do really need the answer if anyone can help. I have uploaded the public domain code to an area of my website, and will link to it later, but I don't think it is necessary, as I have picked out the bits that I think matter below:</p>

<p>My test application works fine, but my main application doesn't. Here is the method I am trying to call:</p>

<pre><code>public ProcessCaller(ISynchronizeInvoke isi)
    : base(isi)
{
}
</code></pre>

<p>Here is the code that calls this method from my test application. This works perfectly. </p>

<pre><code>private void btnOk_Click(object sender, System.EventArgs e)
    {
        processCaller = new ProcessCaller(this);
        processCaller.FileName = @"..\..\test.exe";
        processCaller.Arguments = "";
        processCaller.StdErrReceived += new DataReceivedHandler(writeStreamInfo);
        processCaller.StdOutReceived += new DataReceivedHandler(writeStreamInfo);
        processCaller.Completed += new EventHandler(processCompletedOrCancelled);
        processCaller.Cancelled += new EventHandler(processCompletedOrCancelled);
        // processCaller.Failed += no event handler for this one, yet.

        // the following function starts a process and returns immediately,
        // thus allowing the form to stay responsive.
        processCaller.Start();  
    }
</code></pre>

<p>Here is the code to run the same method from the main app. This RunTest is actually called from a BackGroundWorker. I see that this is slightly pointless, but even when I run it directly, removing the extra BackGroundWorker, I still get exactly the same error, outlined below.</p>

<pre><code>public void RunTest()
    {
        try
        {
        processCaller = new ProcessCaller(this);
        processCaller.FileName = "test.exe";
        processCaller.Arguments = "/test";
        processCaller.StdErrReceived += new DataReceivedHandler(writeStreamInfo);
        processCaller.StdOutReceived += new DataReceivedHandler(writeStreamInfo);
        processCaller.Completed += new EventHandler(processCompletedOrCancelled);
        processCaller.Cancelled += new EventHandler(processCompletedOrCancelled);
        // processCaller.Failed += no event handler for this one, yet.

        // the following function starts a process and returns immediately,
        // thus allowing the form to stay responsive.
        processCaller.Start();
        }
   }
</code></pre>

<p>However, this code does not work. The problem is this line. The bit it does not like is "this". </p>

<pre><code>processCaller = new ProcessCaller(this);
</code></pre>

<p>When I try to build, I get two errors:</p>

<p>Argument 1: cannot convert from 'App.Backend.Tools.Test' to 'System.ComponentModel.ISynchronizeInvoke'</p>

<p>and </p>

<p>The best overloaded method match for 'App.Backend.Tools.ProcessCaller.ProcessCaller(System.ComponentModel.ISynchronizeInvoke)' has some invalid arguments</p>

<p>I kind of understand the issue. I am not calling this from my GUI or Button (object sender stuff) as in my Test App. I kind of know that I need to Invoke it but I haven't got a clue how to do this! If BeginInvoke/Invoke is the correct course of action, could someone please show me how to do it!?</p>

<p>Thanks so much in advance!</p>

<p>Richard</p>

<p>P.S: Just in case anybody does need it, I have uploaded the public domain code I am using here, but I don't think that anybody does need it. </p>

<p><a href="http://www.niemiro.co.uk/FileShare/AsyncOperation.txt" rel="nofollow">AsyncOperation</a></p>

<p><a href="http://www.niemiro.co.uk/FileShare/ProcessCaller.txt" rel="nofollow">ProcessCaller</a></p>
