问题
Is there a way to "tell" C# not to wait for object's method to return?
Using COM (VbScript) I'm able to fire a method without waiting for it to return, so I would like to use C# the same way.
The VbScript Code:
Dim qtApp
Set qtApp = CreateObject("QuickTest.Application")
qtApp.Open("C:\Temp\Test")
qtApp.Run '// VbScript does not wait for this method to finish
Set qtApp = Nothing
The C# code:
//Refer the object
var qtApp = new QuickTest.Application();
//Open the test
qtApp.Open("C:\\Temp\\Test");
qtApp.Test.Run(); //<<== @ this step my code is waiting for the Run method to return
qtApp.Test.Pause(); //<<== This line is never executed until Run returns
Comments:
- The object referred (
QuickTestApplication
) is COM object has methods for Run and Pause ( as mentioned in the code above). - For me
QuickTestApplication
is a black box - I do not want to use
Thread
orASync
(which is basically threading), but theQuickTestApplication
build in methods
I really look for simple answer if it can be done using C#
回答1:
The QTP automation allows running a test asynchronously, look at the documentation:

So instead of running plain Run
(on the Test
Property not the QTP application as you did in your sample) you have to provide a default result-options and then specify False
for WaitOnReturn
Set resOpt = CreateObject("QuickTest.RunResultsOptions")
qtApp.Test.Run resOpt, False
回答2:
Execute it asynchronously.
http://msdn.microsoft.com/en-us/library/2e08f6yc.aspx
http://msdn.microsoft.com/en-us/magazine/cc301332.aspx
The .NET Framework enables you to call any method asynchronously. To do this you define a delegate with the same signature as the method you want to call; the common language runtime automatically defines BeginInvoke and EndInvoke methods for this delegate, with the appropriate signatures.
The BeginInvoke method initiates the asynchronous call. The EndInvoke method retrieves the results of the asynchronous call.
回答3:
You may be able to use a "Task", and run the method in that. I haven't used these yet, but I believe that is the purpose of them in C#.
From MSDN (http://msdn.microsoft.com/en-us/library/dd537609.aspx):
"A task represents an asynchronous operation, and in some ways it resembles the creation of a new thread or ThreadPool work item, but at a higher level of abstraction. Tasks provide two primary benefits:
More efficient and more scalable use of system resources.
Behind the scenes, tasks are queued to the ThreadPool, which has been enhanced with algorithms (like hill-climbing) that determine and adjust to the number of threads that maximizes throughput. This makes tasks relatively lightweight, and you can create many of them to enable fine-grained parallelism. To complement this, widely-known work-stealing algorithms are employed to provide load-balancing.
More programmatic control than is possible with a thread or work item.
Tasks and the framework built around them provide a rich set of APIs that support waiting, cancellation, continuations, robust exception handling, detailed status, custom scheduling, and more.
For both of these reasons, in the .NET Framework 4, tasks are the preferred API for writing multi-threaded, asynchronous, and parallel code."
// Create a task and supply a user delegate by using a lambda expression.
var taskA = new Task(() => Console.WriteLine("Hello from taskA."));
// Start the task.
taskA.Start();
// Output a message from the joining thread.
Console.WriteLine("Hello from the calling thread.");
/* Output:
* Hello from the joining thread.
* Hello from taskA.
*/
来源:https://stackoverflow.com/questions/10305069/dont-want-to-wait-for-method-to-return