Don't want to wait for the method to return

Is there a way to "tell" C # not to wait for an object method to return?

Using COM (VbScript) I can run a method without waiting for it to return, so I would like to use C # in the same way.

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

      

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 referenced by ( QuickTestApplication

    ) is a COM object, has methods for Run and Pause (as mentioned in the code above).
  • There's QuickTestApplication

    a black box for me
  • I don't want to use Thread

    or ASync

    (which is mostly streaming), but QuickTestApplication

    build in methods

I'm really looking for a simple answer if this can be done with C #

+1


source to share


3 answers


QTP automation allows you to run an asynchronous test, see the documentation:

Documentation for Run method



So, instead of running plain Run

(in a Test

Property, not in a QTP application like in your example), you should provide default result parameters and then specify False

forWaitOnReturn

Set resOpt = CreateObject("QuickTest.RunResultsOptions")
qtApp.Test.Run resOpt, False

      

+1


source


Execute it asynchronously.

http://msdn.microsoft.com/en-us/library/2e08f6yc.aspx
http://msdn.microsoft.com/en-us/magazine/cc301332.aspx



The .NET Framework allows 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 determines the BeginInvoke and EndInvoke for this delegate, with the appropriate signature.

The BeginInvoke method initiates an asynchronous call. The EndInvoke method retrieves the results of the asynchronous call.

+3


source


You might be able to use Task and run a method in it. I haven't used them yet, but I believe this is their purpose in C #.

From MSDN (http://msdn.microsoft.com/en-us/library/dd537609.aspx):

"A task is an asynchronous operation and is somewhat similar to creating a new thread or ThreadPool work item, but at a higher level of abstraction. Tasks provide two main benefits:

More efficient and scalable use of system resources.

Behind the scenes, tasks are put into a ThreadPool queue, which have been augmented with algorithms (like rock climbing) that determine and adjust the number of threads that maximize throughput. This makes the tasks relatively easy, and you can create many to enable fine-grained parallelism. In addition to this, widely used work processing algorithms are used to provide load balancing.

More programmatic control than is possible with a thread or work item.

Tasks and the infrastructure built around them provide a rich set of APIs supporting pending, canceling, continuing, robust exception handling, verbose status, custom scheduling, and more.

For both of these reasons, in the .NET Framework 4, tasks are the preferred API for writing 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. 
*/

      

+2


source







All Articles