Don't want to wait for method to return

安稳与你 提交于 2019-12-11 09:19:32

问题


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:

  1. The object referred (QuickTestApplication) is COM object has methods for Run and Pause ( as mentioned in the code above).
  2. For me QuickTestApplication is a black box
  3. I do not want to use Thread or ASync (which is basically threading), but the QuickTestApplicationbuild 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!