如何使用线程同时调用两行代码 [英] How to call two two lines of code at a same time with threading

查看:80
本文介绍了如何使用线程同时调用两行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,



我有要求,

我正在做一些服务验证。所以我需要在同一时间同时调用两个服务。

我可以使用线程吗?如果有,怎么样?

什么是实现我的要求的最佳方法



我的代码如下

Hi Friends,

I have a requirement,
I am doing some service validations. so i need to invoke two services at a same time both service call has to hit on the same time.
Can i use threading? If yes how?
Whats the best method to achieve my requirement

My code is as follows

//First Part
string SOAPAction = "http://tempuri.org/Holdings/GetTopPositions";
                string ReqXML = (new WebServicesAutomationFramework.XMLMapper.DataToRequestXMLMapper(BaseTestClassProp.RequestMap[TestContext.DataRow["ReqMapName"].ToString()],
                    XDocument.Parse(BaseTestClassProp.DataInsertMap[TestContext.DataRow["ReqMapName"].ToString()].ToString()), TestContext)).MapToXML();                
                _iTestFactory.SourceServiceCall = new WebServicesAutomationFramework.Entities.ServiceCallEntity(ReqXML, SOAPAction, WebServicesAutomationFramework.Common.ConfigurationSettings.SourceUrl
                                 , WebServicesAutomationFramework.Common.ConfigurationSettings.RedirectUrl, true, "POST");               
                string GetTopPositionXML = _iTestFactory.SourceServiceCall.RespData.Response;

//Second part
                string SOAPAction2 = "http://tempuri.org/Holdings/GetRequestedData";
                string ReqXML2 = string.Format(doc.ToString(), TestContext.DataRow["SignOnRACFID"].ToString(), TestContext.DataRow["Office"].ToString(), TestContext.DataRow["Account"].ToString());
                _iTestFactory.SourceServiceCall = new WebServicesAutomationFramework.Entities.ServiceCallEntity(ReqXML2, SOAPAction2, WebServicesAutomationFramework.Common.ConfigurationSettings.SourceUrl
                                 , WebServicesAutomationFramework.Common.ConfigurationSettings.RedirectUrl, true, "POST");
                string GetRequestedDataXML = _iTestFactory.SourceServiceCall.RespData.Response;

//Both Service response i need to pass to another method
                DefectFix.ServiceValidation(ReqXML, GetTopPositionXML, GetRequestedDataXML, TestContext.DataRow["TestCaseName"].ToString(), "GetTopPositions", sb);

推荐答案

您无法保证抢先式多任务系统中的执行顺序,甚至类似的执行时间,因为系统可以随意暂停和激活线程,以及可用性免费核心控制多少同时发生的事情。



除此之外,互联网本身是不确定的:消息可以采取不同的路线通过并因此采取不同的时间。当他们到达另一端时,绝对不能保证他们什么时候得到维修 - 再次取决于备用处理能力的可用性和另一端设备的负载。



所以基本上,答案是不。您无法保证同时执行两项服务请求。
You can't guarantee execution order, or even similar execution times in a pre-emptive multitasking system, since the system is at liberty to suspend and activate threads at will, and the availability of free cores controls how many "things happen at the same time".

And to add to that, the internet itself is non-deterministic: messages can take different routes through and thus take different times. And when they get to the other end, there is absolutely no guarantee when they will be serviced - again it will depend on the availability of spare processing power and the load on the equipment at the other end.

So basically, the answer is "no". You cannot guarantee simultaneous execution of your two service requests.


这篇关于如何使用线程同时调用两行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆