Use Parallel.Invoke() To Run Two DB Calls At Once

  • Thread starter Thread starter IndigoMontoya
  • Start date Start date
I

IndigoMontoya

Guest
I have this set-up and functioning to run one Db call then the next, but I want to modify it to run them together using Parallel.Invoke()

I have this syntax

static void Main(string[] args)
{
double AN;

var keepHostOptions = false;

while (true)
{
Console.WriteLine("Please Input The Number:");
string an = Console.ReadLine();

if (double.TryParse(an, out AN) == true)
{
var values = GetDBData(an);
string x = GetDBData1(an);
}
else
{
Console.WriteLine("Please Input A Valid Value!");
}
}
struct Data
{
public string r;
public string s;
public string t;
public string u;
}
public static string GetDBData(string an)
{
string a = null;
string b = null;
string c = null;
string d = null;

using (var ctx = new ClientContext(url))
{
var web = ctx.Web;

List list = web.Lists.GetByTitle(site);
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = string.Format("");
var listItemCollection = list.GetItems(camlQuery);
ctx.Load(listItemCollection, eachItem => eachItem.Include(item => item, item => item["a"], item => item["b"], item => item["c"], item => item["d"]));

ctx.ExecuteQuery();

foreach(ListItem listItem in listItemCollection)
{
a = listItem["a"].ToString();
b = listItem["b"].ToString();
c = listItem["c"].ToString();
d = listItem["d"].ToString();
}
var values = new Data
{
r = a,
s = b,
t = c,
u = d
};
return values;
}
}
public static string GetDBData1(string an)
{
string a = null;


using (var ctx = new ClientContext(url))
{
var web = ctx.Web;

List list = web.Lists.GetByTitle(site);
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = string.Format("");
var listItemCollection = list.GetItems(camlQuery);
ctx.Load(listItemCollection, eachItem => eachItem.Include(item => item, item => item["a"]));

ctx.ExecuteQuery();

foreach(ListItem listItem in listItemCollection)
{
a = listItem["a"].ToString();
}

return als;
}
}



Now my issue is that when I attempt to add the Paralle.Invoke() like this:

static void Main(string[] args)
{
string b;
Parallel.Invoke(
new Action(spvalues = GetDBData(an)),
new Action(() => b = GetDBData1(an))
);
}



I get these compile errors:

the name 'var' does not exist in the current context
can not use local variable 'spvalues' before it is declared


Using C# and console app what would be the proper way to achieve what I am after? (parallel calls to the Database and only continue when BOTH methods have returned the data)

Continue reading...
 
Back
Top