开发者

Problems with "ISynchronizeInvoke" and "this" in C#. I think some sort of Invoke can fix it, but I can't work out how!

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.

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开发者_StackOverflow中文版 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:

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

public ProcessCaller(ISynchronizeInvoke isi)
    : base(isi)
{
}

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

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();  
    }

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.

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();
        }
   }

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

processCaller = new ProcessCaller(this);

When I try to build, I get two errors:

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

and

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

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!?

Thanks so much in advance!

Richard

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.

AsyncOperation

ProcessCaller


The parameter required by the ProcessCaller constructor must be of type System.ComponentModel.ISynchronizeInvoke. The compiler is telling you that your "this" object (of type App.Backend.Tools.Test) does not implement ISynchronizeInvoke.

Change your Test class definition so that it implements ISynchronizeInvoke, something similar to the following:

namespace App.Backend.Tools
{
    class Test : System.ComponentModel.ISynchronizeInvoke
    {
        // Existing code, as well as required implementations
        // of ISynchronizeInvoke members.
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜