Accessing Variables by multiple threads in web application [duplicate]











up vote
0
down vote

favorite













This question is an exact duplicate of:




  • Asp.net losing values of variables

    1 answer




I am running a web Form where I call a new thread on a button press to execute an Rdotnet snippet of code ( I needed the thread because the current thread kept sending stack overload errors ).



In the Rdotnet function , I have it access a global class variable and put data into it. Once the function completes and the button submit function is over I try to access that global class on another button click ( separate button ), however it is empty. I assume that the thread ends so all data in it ends to, so I tried returning the class and putting it into the global variable in the buttonclick function itself,same result.



I need help.



public partial class BTForm : Page
{


List<DirectorDetail> Details = new List<DirectorDetail>();
BindingList<string> Test = new BindingList<string>();
List<string> Line = new List<string>();
List<string> output = new List<string>();
WebDetails BTlibrary;




OpenFileDialog ofd = new OpenFileDialog();
List<string> Months = new List<string>();
List<string> Year = new List<string>();
BTBill Billfile = new BTBill();


protected void Page_Load(object sender, EventArgs e)
{


if(!IsPostBack)
{
addMonth();
addyear();
foreach (var x in Months)
{
DropDownList1.Items.Add(x);
}

foreach (var y in Year)
{
DropDownList2.Items.Add(y);
}
}

try
{
ListBox4.DataSource = Test;
ListBox4.DataBind();

}
catch(Exception x)
{
Console.Write(x);
}



}

protected void Submit_Click(object sender, EventArgs e)
{

Thread current = Thread.CurrentThread;

WebDetails BTlibrary2 =BTlibrary;

Thread t = new Thread(() => { BTlibrary2 = processes(BTlibrary2); }, 2500000);
t.Start();
t.Join();


BTlibrary = BTlibrary2;
// Thread.Sleep(10000);



}



public WebDetails processes(WebDetails library)
{

if (FileUpLoad1.HasFile)
{
string location = @"C:/BTBill/" + FileUpLoad1.FileName;
Billfile.Tablename = FileUpLoad1.FileName;

try

{
Billfile.Month = DropDownList1.SelectedItem.ToString();
Billfile.Year = DropDownList2.SelectedItem.ToString();
Billfile.Filename = Cleaning.PathCleaning(location);

Billfile.Limit = TextBox3.Text.ToString();
string fpath = Billfile.Month + " " + Billfile.Year + " Query " + "Limit -£ " + Billfile.Limit + "\";
string filename = Billfile.Month + " " + Billfile.Year + " Query " + "Limit -£ " + Billfile.Limit;

Billfile.Savelocation = "//lonvmfs01/commonwealth_Data/BT BILL Alert/" + filename;
Billfile.Sfilename = "//lonvmfs01/commonwealth_Data/BT BILL Alert/" + filename;

}
catch (Exception x)
{
Error1.Text = "Please Select Month and yearnERROR : " + x;
}


FileUpLoad1.SaveAs(@"C:BTBill" + FileUpLoad1.FileName);



library = Executable.executable(Billfile);


// FileUpLoad1.
// = "File Uploaded: " + FileUpLoad1.FileName;
}
else
{
Label1.Text = "No File Uploaded.";
}


DataFrame Director = library.Director;
DataFrame bill = library.Query;
DataFrame limit = library.Btlim;


int colcount = Director.RowCount;

for (int x = 0; x < colcount; x++)
{
string cost_centre = Director[x, 0].ToString();
string director = Director[x, 1].ToString();
string email_address = Director[x, 2].ToString();
double total = SendMail.calculatetotal(bill, limit, cost_centre);

if (total > 0)
{
Line.Add(x + " )t" + cost_centre + "t" + director + "tt" + email_address + "t" + total);
Test.Add(x + " )t" + cost_centre + "t" + director + "tt" + email_address + "t" + total);
}


}

ListBox4.DataSource = Test;
ListBox4.DataBind();


//foreach (var x in getline().ToArray())
// {
// ListBox4.Items.Add(x);
// }

// ListBox4.DataBind();


return library;
}

protected void Sendmail(object sender, EventArgs e)
{


DataFrame Director = BTlibrary.Director;
DataFrame bill = BTlibrary.Query;
DataFrame limit = BTlibrary.Btlim;


string test;
foreach (Object selecteditem in ListBox4.GetSelectedIndices() )
{

test = ListBox4.Items[(int)selecteditem].Text;
System.Diagnostics.Debug.WriteLine(test);

test = test.Substring(0, 1);
output.Add(test);
}

List<int> index = new List<int>();

for (int y = 0; y < output.Count; y++)
{
index.Add(Convert.ToInt32(output[y]));
}


for (int y = 0; y < index.Count; y++)
{
DirectorDetail temp = new DirectorDetail();
temp.cost_centre = Director[index[y], 0].ToString();
temp.director = Director[index[y], 1].ToString();
temp.email_address = Director[index[y], 2].ToString();

for (int count = 0; count < limit.RowCount; count++)
{
if (limit[count, 0].ToString() == temp.cost_centre)
{
temp.limit = limit[count, 1].ToString();
}
}

Details.Add(temp);

}


SendMail.Mailing(BTlibrary.Query, BTlibrary.Deplist, BTlibrary.Btlim, BTlibrary.Bill, Details);

//Session["Details"] = Details;

// this.Close();

}









private void addyear()
{

DateTime time = System.DateTime.UtcNow;

int baseyear = time.Year - 3;
for (int x = baseyear; x < (baseyear + 10); x++)
{
Year.Add(x.ToString());
}

}



// returns the list of years
public List<string> getYear()
{
return Year;
}

// returns the list of months
public List<String> getMonth()
{
return Months;
}

//adds months to a list
private void addMonth()
{


Months.Add("January");
Months.Add("February");
Months.Add("March");
Months.Add("April");
Months.Add("May");
Months.Add("June");
Months.Add("July");
Months.Add("August");
Months.Add("September");
Months.Add("October");
Months.Add("November");
Months.Add("December");


}



public List<string> getline()
{
return Line;
}

}





public static WebDetails executable(BTBill bill)
{



StartupParameter rinit = new StartupParameter();
rinit.Quiet = true;
rinit.RHome = @"C:Program FilesRR-3.4.4";
rinit.Home = @"C:R";


REngine.SetEnvironmentVariables();

REngine engine = REngine.GetInstance(null,true,rinit);
// engine.Initialize();

//install and make connection to Postgres
// engine.Evaluate("install.packages('RPostgreSQL') n install.packages('gridExtra')");

engine.Evaluate("require('RPostgreSQL')" + "n" +
"pw <- {'admin'}" + "n" +
"drv <- RPostgreSQL::PostgreSQL()" + "n" +
"drv <- dbDriver('PostgreSQL')" +"n"+
"con <- dbConnect(drv, dbname = 'postgres'," +"n"+
"host = 'localhost', port = 5432," +"n"+
"user = 'postgres', password = pw)");

engine.Evaluate("postgresmfile<- dbGetQuery(con,'select * from masterfile')" + "n" +
"Contact <- dbGetQuery(con, 'select * from contact')"+"n"+
"btlim<- dbGetQuery(con, ' select * from bt_departmentlimit')"+"n"+
"dbDisconnect(con)");

engine.Evaluate("BTBill = read.csv(file<-'"+bill.Filename+"', header=TRUE, sep=',',skip=1)");




// building dataframes and queries
DataFrame BTBill = engine.Evaluate("BTBill").AsDataFrame();
DataFrame MasterFile = engine.Evaluate("postgresmfile").AsDataFrame();
DataFrame BTLim = engine.Evaluate("btlim").AsDataFrame();
DataFrame Contact= engine.Evaluate("Contact ").AsDataFrame();
DataFrame Query = engine.Evaluate("Merged <- merge(BTBill,postgresmfile,by.x='SERVICE.NO',by.y = 'service_number')" + "n"+ "Merged_2 <- merge(Merged,Contact,by.x='cost_centre',by.y='cost_centre') " + "n"+
"query <- Merged_2[c('SERVICE.NO','username','cost_centre','job_post','USAGE.CHARGES','TOTAL.COST','USAGE.START.DATE','USAGE.END.DATE','director','email_address')]").AsDataFrame();
DataFrame Merge2 = engine.Evaluate("Merged_2").AsDataFrame();

DataFrame maillist = engine.Evaluate("data.frame(query)" +"n"+
"test <-subset(query, TOTAL.COST >= "+bill.Limit+ ", select = c(SERVICE.NO,username,cost_centre,job_post, TOTAL.COST, USAGE.START.DATE, USAGE.END.DATE,director,email_address,USAGE.CHARGES))").AsDataFrame();
DataFrame DepList = engine.Evaluate("x<-test[c('cost_centre','director','email_address')]" + "n" +
"ux<-unique(x) ").AsDataFrame();

DataFrame DepList2=engine.Evaluate("y <-query[c('cost_centre', 'director', 'email_address')]" + "n" +
"uy<-unique(y) ").AsDataFrame();

engine.Evaluate("dir.create(file.path('" + bill.Savelocation + "'))");

//creating pdf files for each department head



engine.Evaluate("write.csv(Merged_2, file = '" + bill.Savelocation + "/MasterFile.csv');");
for (int count = 0; count < DepList.RowCount; count++)
{
DataFrame temp = engine.Evaluate("data.frame(query);" +
"test2 <-subset(query, USAGE.CHARGES >= " + bill.Limit + " & cost_centre=='"+DepList[count,0]+"', select = c(SERVICE.NO,username,cost_centre,job_post, USAGE.CHARGES, USAGE.START.DATE, USAGE.END.DATE,director,email_address))").AsDataFrame();
engine.Evaluate("library(gridExtra);");



engine.Evaluate("pdf('" + bill.Sfilename +"/"+ DepList[count, 0] + ".pdf', height=20, width=20);" );

try
{
engine.Evaluate("grid.table(test2); dev.off() ;");
}
catch (Exception e)
{

}

}


SendMailForm form = new SendMailForm();


WebDetails web = new WebDetails(DepList2, Query, BTLim);
web.Deplist = DepList;
web.Bill = bill;

engine.Evaluate("write.csv(test, file = '" + bill.Savelocation + "/Users over threshold.csv')");

engine.Dispose();

return web;



// form.Director=DepList2;
//form.bill = Query;
//form.limit = BTLim;

List<DirectorDetail> output = form.Details;









SendMail.Mailing(Query, DepList,BTLim, bill,output);
// to filter by department
// DataFrame maillist = engine.Evaluate("data.frame(query)" + "n" +
// "test <-subset(query, TOTAL.COST >= " + bill.Limit + "& Cost.Centre=='"+bill.Dep+"', select = c(SERVICE.NO, User.Name, Cost.Centre, ROLE.JOB.POST, TOTAL.COST, USAGE.START.DATE, USAGE.END.DATE,DIRECTOR,EMAIL.ADDRESS))").AsDataFrame();


//engine.Evaluate("install.package('dplyr')");








}
}


}










share|improve this question















marked as duplicate by mjwills, Community Nov 8 at 12:50


This question was marked as an exact duplicate of an existing question.















  • The variable i am referring to is WebDetails BTlibrary; which should be visible by all functions in the form behindcode (which it is). My problem is, after the Submit_Click function accesses it and changes it and ends, the Sendmail function tries to access it, but it sees it as null.
    – Kale Williams
    Nov 8 at 12:08










  • The fundamental issue here is that web requests are stateless. Over multiple requests you aren't dealing with a single instance of BTForm. Each button click = new instance of BTForm. As such, you likely want to assign a value of BTLibrary in Page_Load so you know it is always set.
    – mjwills
    Nov 8 at 12:24












  • @mjwills that worked! thanks alot
    – Kale Williams
    Nov 8 at 12:46















up vote
0
down vote

favorite













This question is an exact duplicate of:




  • Asp.net losing values of variables

    1 answer




I am running a web Form where I call a new thread on a button press to execute an Rdotnet snippet of code ( I needed the thread because the current thread kept sending stack overload errors ).



In the Rdotnet function , I have it access a global class variable and put data into it. Once the function completes and the button submit function is over I try to access that global class on another button click ( separate button ), however it is empty. I assume that the thread ends so all data in it ends to, so I tried returning the class and putting it into the global variable in the buttonclick function itself,same result.



I need help.



public partial class BTForm : Page
{


List<DirectorDetail> Details = new List<DirectorDetail>();
BindingList<string> Test = new BindingList<string>();
List<string> Line = new List<string>();
List<string> output = new List<string>();
WebDetails BTlibrary;




OpenFileDialog ofd = new OpenFileDialog();
List<string> Months = new List<string>();
List<string> Year = new List<string>();
BTBill Billfile = new BTBill();


protected void Page_Load(object sender, EventArgs e)
{


if(!IsPostBack)
{
addMonth();
addyear();
foreach (var x in Months)
{
DropDownList1.Items.Add(x);
}

foreach (var y in Year)
{
DropDownList2.Items.Add(y);
}
}

try
{
ListBox4.DataSource = Test;
ListBox4.DataBind();

}
catch(Exception x)
{
Console.Write(x);
}



}

protected void Submit_Click(object sender, EventArgs e)
{

Thread current = Thread.CurrentThread;

WebDetails BTlibrary2 =BTlibrary;

Thread t = new Thread(() => { BTlibrary2 = processes(BTlibrary2); }, 2500000);
t.Start();
t.Join();


BTlibrary = BTlibrary2;
// Thread.Sleep(10000);



}



public WebDetails processes(WebDetails library)
{

if (FileUpLoad1.HasFile)
{
string location = @"C:/BTBill/" + FileUpLoad1.FileName;
Billfile.Tablename = FileUpLoad1.FileName;

try

{
Billfile.Month = DropDownList1.SelectedItem.ToString();
Billfile.Year = DropDownList2.SelectedItem.ToString();
Billfile.Filename = Cleaning.PathCleaning(location);

Billfile.Limit = TextBox3.Text.ToString();
string fpath = Billfile.Month + " " + Billfile.Year + " Query " + "Limit -£ " + Billfile.Limit + "\";
string filename = Billfile.Month + " " + Billfile.Year + " Query " + "Limit -£ " + Billfile.Limit;

Billfile.Savelocation = "//lonvmfs01/commonwealth_Data/BT BILL Alert/" + filename;
Billfile.Sfilename = "//lonvmfs01/commonwealth_Data/BT BILL Alert/" + filename;

}
catch (Exception x)
{
Error1.Text = "Please Select Month and yearnERROR : " + x;
}


FileUpLoad1.SaveAs(@"C:BTBill" + FileUpLoad1.FileName);



library = Executable.executable(Billfile);


// FileUpLoad1.
// = "File Uploaded: " + FileUpLoad1.FileName;
}
else
{
Label1.Text = "No File Uploaded.";
}


DataFrame Director = library.Director;
DataFrame bill = library.Query;
DataFrame limit = library.Btlim;


int colcount = Director.RowCount;

for (int x = 0; x < colcount; x++)
{
string cost_centre = Director[x, 0].ToString();
string director = Director[x, 1].ToString();
string email_address = Director[x, 2].ToString();
double total = SendMail.calculatetotal(bill, limit, cost_centre);

if (total > 0)
{
Line.Add(x + " )t" + cost_centre + "t" + director + "tt" + email_address + "t" + total);
Test.Add(x + " )t" + cost_centre + "t" + director + "tt" + email_address + "t" + total);
}


}

ListBox4.DataSource = Test;
ListBox4.DataBind();


//foreach (var x in getline().ToArray())
// {
// ListBox4.Items.Add(x);
// }

// ListBox4.DataBind();


return library;
}

protected void Sendmail(object sender, EventArgs e)
{


DataFrame Director = BTlibrary.Director;
DataFrame bill = BTlibrary.Query;
DataFrame limit = BTlibrary.Btlim;


string test;
foreach (Object selecteditem in ListBox4.GetSelectedIndices() )
{

test = ListBox4.Items[(int)selecteditem].Text;
System.Diagnostics.Debug.WriteLine(test);

test = test.Substring(0, 1);
output.Add(test);
}

List<int> index = new List<int>();

for (int y = 0; y < output.Count; y++)
{
index.Add(Convert.ToInt32(output[y]));
}


for (int y = 0; y < index.Count; y++)
{
DirectorDetail temp = new DirectorDetail();
temp.cost_centre = Director[index[y], 0].ToString();
temp.director = Director[index[y], 1].ToString();
temp.email_address = Director[index[y], 2].ToString();

for (int count = 0; count < limit.RowCount; count++)
{
if (limit[count, 0].ToString() == temp.cost_centre)
{
temp.limit = limit[count, 1].ToString();
}
}

Details.Add(temp);

}


SendMail.Mailing(BTlibrary.Query, BTlibrary.Deplist, BTlibrary.Btlim, BTlibrary.Bill, Details);

//Session["Details"] = Details;

// this.Close();

}









private void addyear()
{

DateTime time = System.DateTime.UtcNow;

int baseyear = time.Year - 3;
for (int x = baseyear; x < (baseyear + 10); x++)
{
Year.Add(x.ToString());
}

}



// returns the list of years
public List<string> getYear()
{
return Year;
}

// returns the list of months
public List<String> getMonth()
{
return Months;
}

//adds months to a list
private void addMonth()
{


Months.Add("January");
Months.Add("February");
Months.Add("March");
Months.Add("April");
Months.Add("May");
Months.Add("June");
Months.Add("July");
Months.Add("August");
Months.Add("September");
Months.Add("October");
Months.Add("November");
Months.Add("December");


}



public List<string> getline()
{
return Line;
}

}





public static WebDetails executable(BTBill bill)
{



StartupParameter rinit = new StartupParameter();
rinit.Quiet = true;
rinit.RHome = @"C:Program FilesRR-3.4.4";
rinit.Home = @"C:R";


REngine.SetEnvironmentVariables();

REngine engine = REngine.GetInstance(null,true,rinit);
// engine.Initialize();

//install and make connection to Postgres
// engine.Evaluate("install.packages('RPostgreSQL') n install.packages('gridExtra')");

engine.Evaluate("require('RPostgreSQL')" + "n" +
"pw <- {'admin'}" + "n" +
"drv <- RPostgreSQL::PostgreSQL()" + "n" +
"drv <- dbDriver('PostgreSQL')" +"n"+
"con <- dbConnect(drv, dbname = 'postgres'," +"n"+
"host = 'localhost', port = 5432," +"n"+
"user = 'postgres', password = pw)");

engine.Evaluate("postgresmfile<- dbGetQuery(con,'select * from masterfile')" + "n" +
"Contact <- dbGetQuery(con, 'select * from contact')"+"n"+
"btlim<- dbGetQuery(con, ' select * from bt_departmentlimit')"+"n"+
"dbDisconnect(con)");

engine.Evaluate("BTBill = read.csv(file<-'"+bill.Filename+"', header=TRUE, sep=',',skip=1)");




// building dataframes and queries
DataFrame BTBill = engine.Evaluate("BTBill").AsDataFrame();
DataFrame MasterFile = engine.Evaluate("postgresmfile").AsDataFrame();
DataFrame BTLim = engine.Evaluate("btlim").AsDataFrame();
DataFrame Contact= engine.Evaluate("Contact ").AsDataFrame();
DataFrame Query = engine.Evaluate("Merged <- merge(BTBill,postgresmfile,by.x='SERVICE.NO',by.y = 'service_number')" + "n"+ "Merged_2 <- merge(Merged,Contact,by.x='cost_centre',by.y='cost_centre') " + "n"+
"query <- Merged_2[c('SERVICE.NO','username','cost_centre','job_post','USAGE.CHARGES','TOTAL.COST','USAGE.START.DATE','USAGE.END.DATE','director','email_address')]").AsDataFrame();
DataFrame Merge2 = engine.Evaluate("Merged_2").AsDataFrame();

DataFrame maillist = engine.Evaluate("data.frame(query)" +"n"+
"test <-subset(query, TOTAL.COST >= "+bill.Limit+ ", select = c(SERVICE.NO,username,cost_centre,job_post, TOTAL.COST, USAGE.START.DATE, USAGE.END.DATE,director,email_address,USAGE.CHARGES))").AsDataFrame();
DataFrame DepList = engine.Evaluate("x<-test[c('cost_centre','director','email_address')]" + "n" +
"ux<-unique(x) ").AsDataFrame();

DataFrame DepList2=engine.Evaluate("y <-query[c('cost_centre', 'director', 'email_address')]" + "n" +
"uy<-unique(y) ").AsDataFrame();

engine.Evaluate("dir.create(file.path('" + bill.Savelocation + "'))");

//creating pdf files for each department head



engine.Evaluate("write.csv(Merged_2, file = '" + bill.Savelocation + "/MasterFile.csv');");
for (int count = 0; count < DepList.RowCount; count++)
{
DataFrame temp = engine.Evaluate("data.frame(query);" +
"test2 <-subset(query, USAGE.CHARGES >= " + bill.Limit + " & cost_centre=='"+DepList[count,0]+"', select = c(SERVICE.NO,username,cost_centre,job_post, USAGE.CHARGES, USAGE.START.DATE, USAGE.END.DATE,director,email_address))").AsDataFrame();
engine.Evaluate("library(gridExtra);");



engine.Evaluate("pdf('" + bill.Sfilename +"/"+ DepList[count, 0] + ".pdf', height=20, width=20);" );

try
{
engine.Evaluate("grid.table(test2); dev.off() ;");
}
catch (Exception e)
{

}

}


SendMailForm form = new SendMailForm();


WebDetails web = new WebDetails(DepList2, Query, BTLim);
web.Deplist = DepList;
web.Bill = bill;

engine.Evaluate("write.csv(test, file = '" + bill.Savelocation + "/Users over threshold.csv')");

engine.Dispose();

return web;



// form.Director=DepList2;
//form.bill = Query;
//form.limit = BTLim;

List<DirectorDetail> output = form.Details;









SendMail.Mailing(Query, DepList,BTLim, bill,output);
// to filter by department
// DataFrame maillist = engine.Evaluate("data.frame(query)" + "n" +
// "test <-subset(query, TOTAL.COST >= " + bill.Limit + "& Cost.Centre=='"+bill.Dep+"', select = c(SERVICE.NO, User.Name, Cost.Centre, ROLE.JOB.POST, TOTAL.COST, USAGE.START.DATE, USAGE.END.DATE,DIRECTOR,EMAIL.ADDRESS))").AsDataFrame();


//engine.Evaluate("install.package('dplyr')");








}
}


}










share|improve this question















marked as duplicate by mjwills, Community Nov 8 at 12:50


This question was marked as an exact duplicate of an existing question.















  • The variable i am referring to is WebDetails BTlibrary; which should be visible by all functions in the form behindcode (which it is). My problem is, after the Submit_Click function accesses it and changes it and ends, the Sendmail function tries to access it, but it sees it as null.
    – Kale Williams
    Nov 8 at 12:08










  • The fundamental issue here is that web requests are stateless. Over multiple requests you aren't dealing with a single instance of BTForm. Each button click = new instance of BTForm. As such, you likely want to assign a value of BTLibrary in Page_Load so you know it is always set.
    – mjwills
    Nov 8 at 12:24












  • @mjwills that worked! thanks alot
    – Kale Williams
    Nov 8 at 12:46













up vote
0
down vote

favorite









up vote
0
down vote

favorite












This question is an exact duplicate of:




  • Asp.net losing values of variables

    1 answer




I am running a web Form where I call a new thread on a button press to execute an Rdotnet snippet of code ( I needed the thread because the current thread kept sending stack overload errors ).



In the Rdotnet function , I have it access a global class variable and put data into it. Once the function completes and the button submit function is over I try to access that global class on another button click ( separate button ), however it is empty. I assume that the thread ends so all data in it ends to, so I tried returning the class and putting it into the global variable in the buttonclick function itself,same result.



I need help.



public partial class BTForm : Page
{


List<DirectorDetail> Details = new List<DirectorDetail>();
BindingList<string> Test = new BindingList<string>();
List<string> Line = new List<string>();
List<string> output = new List<string>();
WebDetails BTlibrary;




OpenFileDialog ofd = new OpenFileDialog();
List<string> Months = new List<string>();
List<string> Year = new List<string>();
BTBill Billfile = new BTBill();


protected void Page_Load(object sender, EventArgs e)
{


if(!IsPostBack)
{
addMonth();
addyear();
foreach (var x in Months)
{
DropDownList1.Items.Add(x);
}

foreach (var y in Year)
{
DropDownList2.Items.Add(y);
}
}

try
{
ListBox4.DataSource = Test;
ListBox4.DataBind();

}
catch(Exception x)
{
Console.Write(x);
}



}

protected void Submit_Click(object sender, EventArgs e)
{

Thread current = Thread.CurrentThread;

WebDetails BTlibrary2 =BTlibrary;

Thread t = new Thread(() => { BTlibrary2 = processes(BTlibrary2); }, 2500000);
t.Start();
t.Join();


BTlibrary = BTlibrary2;
// Thread.Sleep(10000);



}



public WebDetails processes(WebDetails library)
{

if (FileUpLoad1.HasFile)
{
string location = @"C:/BTBill/" + FileUpLoad1.FileName;
Billfile.Tablename = FileUpLoad1.FileName;

try

{
Billfile.Month = DropDownList1.SelectedItem.ToString();
Billfile.Year = DropDownList2.SelectedItem.ToString();
Billfile.Filename = Cleaning.PathCleaning(location);

Billfile.Limit = TextBox3.Text.ToString();
string fpath = Billfile.Month + " " + Billfile.Year + " Query " + "Limit -£ " + Billfile.Limit + "\";
string filename = Billfile.Month + " " + Billfile.Year + " Query " + "Limit -£ " + Billfile.Limit;

Billfile.Savelocation = "//lonvmfs01/commonwealth_Data/BT BILL Alert/" + filename;
Billfile.Sfilename = "//lonvmfs01/commonwealth_Data/BT BILL Alert/" + filename;

}
catch (Exception x)
{
Error1.Text = "Please Select Month and yearnERROR : " + x;
}


FileUpLoad1.SaveAs(@"C:BTBill" + FileUpLoad1.FileName);



library = Executable.executable(Billfile);


// FileUpLoad1.
// = "File Uploaded: " + FileUpLoad1.FileName;
}
else
{
Label1.Text = "No File Uploaded.";
}


DataFrame Director = library.Director;
DataFrame bill = library.Query;
DataFrame limit = library.Btlim;


int colcount = Director.RowCount;

for (int x = 0; x < colcount; x++)
{
string cost_centre = Director[x, 0].ToString();
string director = Director[x, 1].ToString();
string email_address = Director[x, 2].ToString();
double total = SendMail.calculatetotal(bill, limit, cost_centre);

if (total > 0)
{
Line.Add(x + " )t" + cost_centre + "t" + director + "tt" + email_address + "t" + total);
Test.Add(x + " )t" + cost_centre + "t" + director + "tt" + email_address + "t" + total);
}


}

ListBox4.DataSource = Test;
ListBox4.DataBind();


//foreach (var x in getline().ToArray())
// {
// ListBox4.Items.Add(x);
// }

// ListBox4.DataBind();


return library;
}

protected void Sendmail(object sender, EventArgs e)
{


DataFrame Director = BTlibrary.Director;
DataFrame bill = BTlibrary.Query;
DataFrame limit = BTlibrary.Btlim;


string test;
foreach (Object selecteditem in ListBox4.GetSelectedIndices() )
{

test = ListBox4.Items[(int)selecteditem].Text;
System.Diagnostics.Debug.WriteLine(test);

test = test.Substring(0, 1);
output.Add(test);
}

List<int> index = new List<int>();

for (int y = 0; y < output.Count; y++)
{
index.Add(Convert.ToInt32(output[y]));
}


for (int y = 0; y < index.Count; y++)
{
DirectorDetail temp = new DirectorDetail();
temp.cost_centre = Director[index[y], 0].ToString();
temp.director = Director[index[y], 1].ToString();
temp.email_address = Director[index[y], 2].ToString();

for (int count = 0; count < limit.RowCount; count++)
{
if (limit[count, 0].ToString() == temp.cost_centre)
{
temp.limit = limit[count, 1].ToString();
}
}

Details.Add(temp);

}


SendMail.Mailing(BTlibrary.Query, BTlibrary.Deplist, BTlibrary.Btlim, BTlibrary.Bill, Details);

//Session["Details"] = Details;

// this.Close();

}









private void addyear()
{

DateTime time = System.DateTime.UtcNow;

int baseyear = time.Year - 3;
for (int x = baseyear; x < (baseyear + 10); x++)
{
Year.Add(x.ToString());
}

}



// returns the list of years
public List<string> getYear()
{
return Year;
}

// returns the list of months
public List<String> getMonth()
{
return Months;
}

//adds months to a list
private void addMonth()
{


Months.Add("January");
Months.Add("February");
Months.Add("March");
Months.Add("April");
Months.Add("May");
Months.Add("June");
Months.Add("July");
Months.Add("August");
Months.Add("September");
Months.Add("October");
Months.Add("November");
Months.Add("December");


}



public List<string> getline()
{
return Line;
}

}





public static WebDetails executable(BTBill bill)
{



StartupParameter rinit = new StartupParameter();
rinit.Quiet = true;
rinit.RHome = @"C:Program FilesRR-3.4.4";
rinit.Home = @"C:R";


REngine.SetEnvironmentVariables();

REngine engine = REngine.GetInstance(null,true,rinit);
// engine.Initialize();

//install and make connection to Postgres
// engine.Evaluate("install.packages('RPostgreSQL') n install.packages('gridExtra')");

engine.Evaluate("require('RPostgreSQL')" + "n" +
"pw <- {'admin'}" + "n" +
"drv <- RPostgreSQL::PostgreSQL()" + "n" +
"drv <- dbDriver('PostgreSQL')" +"n"+
"con <- dbConnect(drv, dbname = 'postgres'," +"n"+
"host = 'localhost', port = 5432," +"n"+
"user = 'postgres', password = pw)");

engine.Evaluate("postgresmfile<- dbGetQuery(con,'select * from masterfile')" + "n" +
"Contact <- dbGetQuery(con, 'select * from contact')"+"n"+
"btlim<- dbGetQuery(con, ' select * from bt_departmentlimit')"+"n"+
"dbDisconnect(con)");

engine.Evaluate("BTBill = read.csv(file<-'"+bill.Filename+"', header=TRUE, sep=',',skip=1)");




// building dataframes and queries
DataFrame BTBill = engine.Evaluate("BTBill").AsDataFrame();
DataFrame MasterFile = engine.Evaluate("postgresmfile").AsDataFrame();
DataFrame BTLim = engine.Evaluate("btlim").AsDataFrame();
DataFrame Contact= engine.Evaluate("Contact ").AsDataFrame();
DataFrame Query = engine.Evaluate("Merged <- merge(BTBill,postgresmfile,by.x='SERVICE.NO',by.y = 'service_number')" + "n"+ "Merged_2 <- merge(Merged,Contact,by.x='cost_centre',by.y='cost_centre') " + "n"+
"query <- Merged_2[c('SERVICE.NO','username','cost_centre','job_post','USAGE.CHARGES','TOTAL.COST','USAGE.START.DATE','USAGE.END.DATE','director','email_address')]").AsDataFrame();
DataFrame Merge2 = engine.Evaluate("Merged_2").AsDataFrame();

DataFrame maillist = engine.Evaluate("data.frame(query)" +"n"+
"test <-subset(query, TOTAL.COST >= "+bill.Limit+ ", select = c(SERVICE.NO,username,cost_centre,job_post, TOTAL.COST, USAGE.START.DATE, USAGE.END.DATE,director,email_address,USAGE.CHARGES))").AsDataFrame();
DataFrame DepList = engine.Evaluate("x<-test[c('cost_centre','director','email_address')]" + "n" +
"ux<-unique(x) ").AsDataFrame();

DataFrame DepList2=engine.Evaluate("y <-query[c('cost_centre', 'director', 'email_address')]" + "n" +
"uy<-unique(y) ").AsDataFrame();

engine.Evaluate("dir.create(file.path('" + bill.Savelocation + "'))");

//creating pdf files for each department head



engine.Evaluate("write.csv(Merged_2, file = '" + bill.Savelocation + "/MasterFile.csv');");
for (int count = 0; count < DepList.RowCount; count++)
{
DataFrame temp = engine.Evaluate("data.frame(query);" +
"test2 <-subset(query, USAGE.CHARGES >= " + bill.Limit + " & cost_centre=='"+DepList[count,0]+"', select = c(SERVICE.NO,username,cost_centre,job_post, USAGE.CHARGES, USAGE.START.DATE, USAGE.END.DATE,director,email_address))").AsDataFrame();
engine.Evaluate("library(gridExtra);");



engine.Evaluate("pdf('" + bill.Sfilename +"/"+ DepList[count, 0] + ".pdf', height=20, width=20);" );

try
{
engine.Evaluate("grid.table(test2); dev.off() ;");
}
catch (Exception e)
{

}

}


SendMailForm form = new SendMailForm();


WebDetails web = new WebDetails(DepList2, Query, BTLim);
web.Deplist = DepList;
web.Bill = bill;

engine.Evaluate("write.csv(test, file = '" + bill.Savelocation + "/Users over threshold.csv')");

engine.Dispose();

return web;



// form.Director=DepList2;
//form.bill = Query;
//form.limit = BTLim;

List<DirectorDetail> output = form.Details;









SendMail.Mailing(Query, DepList,BTLim, bill,output);
// to filter by department
// DataFrame maillist = engine.Evaluate("data.frame(query)" + "n" +
// "test <-subset(query, TOTAL.COST >= " + bill.Limit + "& Cost.Centre=='"+bill.Dep+"', select = c(SERVICE.NO, User.Name, Cost.Centre, ROLE.JOB.POST, TOTAL.COST, USAGE.START.DATE, USAGE.END.DATE,DIRECTOR,EMAIL.ADDRESS))").AsDataFrame();


//engine.Evaluate("install.package('dplyr')");








}
}


}










share|improve this question
















This question is an exact duplicate of:




  • Asp.net losing values of variables

    1 answer




I am running a web Form where I call a new thread on a button press to execute an Rdotnet snippet of code ( I needed the thread because the current thread kept sending stack overload errors ).



In the Rdotnet function , I have it access a global class variable and put data into it. Once the function completes and the button submit function is over I try to access that global class on another button click ( separate button ), however it is empty. I assume that the thread ends so all data in it ends to, so I tried returning the class and putting it into the global variable in the buttonclick function itself,same result.



I need help.



public partial class BTForm : Page
{


List<DirectorDetail> Details = new List<DirectorDetail>();
BindingList<string> Test = new BindingList<string>();
List<string> Line = new List<string>();
List<string> output = new List<string>();
WebDetails BTlibrary;




OpenFileDialog ofd = new OpenFileDialog();
List<string> Months = new List<string>();
List<string> Year = new List<string>();
BTBill Billfile = new BTBill();


protected void Page_Load(object sender, EventArgs e)
{


if(!IsPostBack)
{
addMonth();
addyear();
foreach (var x in Months)
{
DropDownList1.Items.Add(x);
}

foreach (var y in Year)
{
DropDownList2.Items.Add(y);
}
}

try
{
ListBox4.DataSource = Test;
ListBox4.DataBind();

}
catch(Exception x)
{
Console.Write(x);
}



}

protected void Submit_Click(object sender, EventArgs e)
{

Thread current = Thread.CurrentThread;

WebDetails BTlibrary2 =BTlibrary;

Thread t = new Thread(() => { BTlibrary2 = processes(BTlibrary2); }, 2500000);
t.Start();
t.Join();


BTlibrary = BTlibrary2;
// Thread.Sleep(10000);



}



public WebDetails processes(WebDetails library)
{

if (FileUpLoad1.HasFile)
{
string location = @"C:/BTBill/" + FileUpLoad1.FileName;
Billfile.Tablename = FileUpLoad1.FileName;

try

{
Billfile.Month = DropDownList1.SelectedItem.ToString();
Billfile.Year = DropDownList2.SelectedItem.ToString();
Billfile.Filename = Cleaning.PathCleaning(location);

Billfile.Limit = TextBox3.Text.ToString();
string fpath = Billfile.Month + " " + Billfile.Year + " Query " + "Limit -£ " + Billfile.Limit + "\";
string filename = Billfile.Month + " " + Billfile.Year + " Query " + "Limit -£ " + Billfile.Limit;

Billfile.Savelocation = "//lonvmfs01/commonwealth_Data/BT BILL Alert/" + filename;
Billfile.Sfilename = "//lonvmfs01/commonwealth_Data/BT BILL Alert/" + filename;

}
catch (Exception x)
{
Error1.Text = "Please Select Month and yearnERROR : " + x;
}


FileUpLoad1.SaveAs(@"C:BTBill" + FileUpLoad1.FileName);



library = Executable.executable(Billfile);


// FileUpLoad1.
// = "File Uploaded: " + FileUpLoad1.FileName;
}
else
{
Label1.Text = "No File Uploaded.";
}


DataFrame Director = library.Director;
DataFrame bill = library.Query;
DataFrame limit = library.Btlim;


int colcount = Director.RowCount;

for (int x = 0; x < colcount; x++)
{
string cost_centre = Director[x, 0].ToString();
string director = Director[x, 1].ToString();
string email_address = Director[x, 2].ToString();
double total = SendMail.calculatetotal(bill, limit, cost_centre);

if (total > 0)
{
Line.Add(x + " )t" + cost_centre + "t" + director + "tt" + email_address + "t" + total);
Test.Add(x + " )t" + cost_centre + "t" + director + "tt" + email_address + "t" + total);
}


}

ListBox4.DataSource = Test;
ListBox4.DataBind();


//foreach (var x in getline().ToArray())
// {
// ListBox4.Items.Add(x);
// }

// ListBox4.DataBind();


return library;
}

protected void Sendmail(object sender, EventArgs e)
{


DataFrame Director = BTlibrary.Director;
DataFrame bill = BTlibrary.Query;
DataFrame limit = BTlibrary.Btlim;


string test;
foreach (Object selecteditem in ListBox4.GetSelectedIndices() )
{

test = ListBox4.Items[(int)selecteditem].Text;
System.Diagnostics.Debug.WriteLine(test);

test = test.Substring(0, 1);
output.Add(test);
}

List<int> index = new List<int>();

for (int y = 0; y < output.Count; y++)
{
index.Add(Convert.ToInt32(output[y]));
}


for (int y = 0; y < index.Count; y++)
{
DirectorDetail temp = new DirectorDetail();
temp.cost_centre = Director[index[y], 0].ToString();
temp.director = Director[index[y], 1].ToString();
temp.email_address = Director[index[y], 2].ToString();

for (int count = 0; count < limit.RowCount; count++)
{
if (limit[count, 0].ToString() == temp.cost_centre)
{
temp.limit = limit[count, 1].ToString();
}
}

Details.Add(temp);

}


SendMail.Mailing(BTlibrary.Query, BTlibrary.Deplist, BTlibrary.Btlim, BTlibrary.Bill, Details);

//Session["Details"] = Details;

// this.Close();

}









private void addyear()
{

DateTime time = System.DateTime.UtcNow;

int baseyear = time.Year - 3;
for (int x = baseyear; x < (baseyear + 10); x++)
{
Year.Add(x.ToString());
}

}



// returns the list of years
public List<string> getYear()
{
return Year;
}

// returns the list of months
public List<String> getMonth()
{
return Months;
}

//adds months to a list
private void addMonth()
{


Months.Add("January");
Months.Add("February");
Months.Add("March");
Months.Add("April");
Months.Add("May");
Months.Add("June");
Months.Add("July");
Months.Add("August");
Months.Add("September");
Months.Add("October");
Months.Add("November");
Months.Add("December");


}



public List<string> getline()
{
return Line;
}

}





public static WebDetails executable(BTBill bill)
{



StartupParameter rinit = new StartupParameter();
rinit.Quiet = true;
rinit.RHome = @"C:Program FilesRR-3.4.4";
rinit.Home = @"C:R";


REngine.SetEnvironmentVariables();

REngine engine = REngine.GetInstance(null,true,rinit);
// engine.Initialize();

//install and make connection to Postgres
// engine.Evaluate("install.packages('RPostgreSQL') n install.packages('gridExtra')");

engine.Evaluate("require('RPostgreSQL')" + "n" +
"pw <- {'admin'}" + "n" +
"drv <- RPostgreSQL::PostgreSQL()" + "n" +
"drv <- dbDriver('PostgreSQL')" +"n"+
"con <- dbConnect(drv, dbname = 'postgres'," +"n"+
"host = 'localhost', port = 5432," +"n"+
"user = 'postgres', password = pw)");

engine.Evaluate("postgresmfile<- dbGetQuery(con,'select * from masterfile')" + "n" +
"Contact <- dbGetQuery(con, 'select * from contact')"+"n"+
"btlim<- dbGetQuery(con, ' select * from bt_departmentlimit')"+"n"+
"dbDisconnect(con)");

engine.Evaluate("BTBill = read.csv(file<-'"+bill.Filename+"', header=TRUE, sep=',',skip=1)");




// building dataframes and queries
DataFrame BTBill = engine.Evaluate("BTBill").AsDataFrame();
DataFrame MasterFile = engine.Evaluate("postgresmfile").AsDataFrame();
DataFrame BTLim = engine.Evaluate("btlim").AsDataFrame();
DataFrame Contact= engine.Evaluate("Contact ").AsDataFrame();
DataFrame Query = engine.Evaluate("Merged <- merge(BTBill,postgresmfile,by.x='SERVICE.NO',by.y = 'service_number')" + "n"+ "Merged_2 <- merge(Merged,Contact,by.x='cost_centre',by.y='cost_centre') " + "n"+
"query <- Merged_2[c('SERVICE.NO','username','cost_centre','job_post','USAGE.CHARGES','TOTAL.COST','USAGE.START.DATE','USAGE.END.DATE','director','email_address')]").AsDataFrame();
DataFrame Merge2 = engine.Evaluate("Merged_2").AsDataFrame();

DataFrame maillist = engine.Evaluate("data.frame(query)" +"n"+
"test <-subset(query, TOTAL.COST >= "+bill.Limit+ ", select = c(SERVICE.NO,username,cost_centre,job_post, TOTAL.COST, USAGE.START.DATE, USAGE.END.DATE,director,email_address,USAGE.CHARGES))").AsDataFrame();
DataFrame DepList = engine.Evaluate("x<-test[c('cost_centre','director','email_address')]" + "n" +
"ux<-unique(x) ").AsDataFrame();

DataFrame DepList2=engine.Evaluate("y <-query[c('cost_centre', 'director', 'email_address')]" + "n" +
"uy<-unique(y) ").AsDataFrame();

engine.Evaluate("dir.create(file.path('" + bill.Savelocation + "'))");

//creating pdf files for each department head



engine.Evaluate("write.csv(Merged_2, file = '" + bill.Savelocation + "/MasterFile.csv');");
for (int count = 0; count < DepList.RowCount; count++)
{
DataFrame temp = engine.Evaluate("data.frame(query);" +
"test2 <-subset(query, USAGE.CHARGES >= " + bill.Limit + " & cost_centre=='"+DepList[count,0]+"', select = c(SERVICE.NO,username,cost_centre,job_post, USAGE.CHARGES, USAGE.START.DATE, USAGE.END.DATE,director,email_address))").AsDataFrame();
engine.Evaluate("library(gridExtra);");



engine.Evaluate("pdf('" + bill.Sfilename +"/"+ DepList[count, 0] + ".pdf', height=20, width=20);" );

try
{
engine.Evaluate("grid.table(test2); dev.off() ;");
}
catch (Exception e)
{

}

}


SendMailForm form = new SendMailForm();


WebDetails web = new WebDetails(DepList2, Query, BTLim);
web.Deplist = DepList;
web.Bill = bill;

engine.Evaluate("write.csv(test, file = '" + bill.Savelocation + "/Users over threshold.csv')");

engine.Dispose();

return web;



// form.Director=DepList2;
//form.bill = Query;
//form.limit = BTLim;

List<DirectorDetail> output = form.Details;









SendMail.Mailing(Query, DepList,BTLim, bill,output);
// to filter by department
// DataFrame maillist = engine.Evaluate("data.frame(query)" + "n" +
// "test <-subset(query, TOTAL.COST >= " + bill.Limit + "& Cost.Centre=='"+bill.Dep+"', select = c(SERVICE.NO, User.Name, Cost.Centre, ROLE.JOB.POST, TOTAL.COST, USAGE.START.DATE, USAGE.END.DATE,DIRECTOR,EMAIL.ADDRESS))").AsDataFrame();


//engine.Evaluate("install.package('dplyr')");








}
}


}





This question is an exact duplicate of:




  • Asp.net losing values of variables

    1 answer








c# multithreading webforms






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 12:24

























asked Nov 8 at 11:06









Kale Williams

112




112




marked as duplicate by mjwills, Community Nov 8 at 12:50


This question was marked as an exact duplicate of an existing question.






marked as duplicate by mjwills, Community Nov 8 at 12:50


This question was marked as an exact duplicate of an existing question.














  • The variable i am referring to is WebDetails BTlibrary; which should be visible by all functions in the form behindcode (which it is). My problem is, after the Submit_Click function accesses it and changes it and ends, the Sendmail function tries to access it, but it sees it as null.
    – Kale Williams
    Nov 8 at 12:08










  • The fundamental issue here is that web requests are stateless. Over multiple requests you aren't dealing with a single instance of BTForm. Each button click = new instance of BTForm. As such, you likely want to assign a value of BTLibrary in Page_Load so you know it is always set.
    – mjwills
    Nov 8 at 12:24












  • @mjwills that worked! thanks alot
    – Kale Williams
    Nov 8 at 12:46


















  • The variable i am referring to is WebDetails BTlibrary; which should be visible by all functions in the form behindcode (which it is). My problem is, after the Submit_Click function accesses it and changes it and ends, the Sendmail function tries to access it, but it sees it as null.
    – Kale Williams
    Nov 8 at 12:08










  • The fundamental issue here is that web requests are stateless. Over multiple requests you aren't dealing with a single instance of BTForm. Each button click = new instance of BTForm. As such, you likely want to assign a value of BTLibrary in Page_Load so you know it is always set.
    – mjwills
    Nov 8 at 12:24












  • @mjwills that worked! thanks alot
    – Kale Williams
    Nov 8 at 12:46
















The variable i am referring to is WebDetails BTlibrary; which should be visible by all functions in the form behindcode (which it is). My problem is, after the Submit_Click function accesses it and changes it and ends, the Sendmail function tries to access it, but it sees it as null.
– Kale Williams
Nov 8 at 12:08




The variable i am referring to is WebDetails BTlibrary; which should be visible by all functions in the form behindcode (which it is). My problem is, after the Submit_Click function accesses it and changes it and ends, the Sendmail function tries to access it, but it sees it as null.
– Kale Williams
Nov 8 at 12:08












The fundamental issue here is that web requests are stateless. Over multiple requests you aren't dealing with a single instance of BTForm. Each button click = new instance of BTForm. As such, you likely want to assign a value of BTLibrary in Page_Load so you know it is always set.
– mjwills
Nov 8 at 12:24






The fundamental issue here is that web requests are stateless. Over multiple requests you aren't dealing with a single instance of BTForm. Each button click = new instance of BTForm. As such, you likely want to assign a value of BTLibrary in Page_Load so you know it is always set.
– mjwills
Nov 8 at 12:24














@mjwills that worked! thanks alot
– Kale Williams
Nov 8 at 12:46




@mjwills that worked! thanks alot
– Kale Williams
Nov 8 at 12:46












1 Answer
1






active

oldest

votes

















up vote
1
down vote













It seems my problem turned out to be unrelated to variable sharing through threads, but instead to deal with the way button click instances worked.



long story short, in order to share a variable between two event instances, the easiest way to do it is through Sessions.



I needed to put the BTlibrary in a session and access it in the second button click event for it to get the value saved from the previous.



protected void Submit_Click(object sender, EventArgs e)
{

Thread current = Thread.CurrentThread;

WebDetails BTlibrary2 =BTlibrary;

Thread t = new Thread(() => { BTlibrary2 = processes(BTlibrary2); }, 2500000);
t.Start();
t.Join();


Session["BTLib"] = BTlibrary2;

// Thread.Sleep(10000);



}

protected void Sendmail(object sender, EventArgs e)
{
List<DirectorDetail> Details = new List<DirectorDetail>();

BTlibrary = (WebDetails) Session["BTLib"];


this worked






share|improve this answer





















  • It would be useful if you could provide a Minimal, Complete, and Verifiable example.
    – AndreaT
    Nov 9 at 7:58


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













It seems my problem turned out to be unrelated to variable sharing through threads, but instead to deal with the way button click instances worked.



long story short, in order to share a variable between two event instances, the easiest way to do it is through Sessions.



I needed to put the BTlibrary in a session and access it in the second button click event for it to get the value saved from the previous.



protected void Submit_Click(object sender, EventArgs e)
{

Thread current = Thread.CurrentThread;

WebDetails BTlibrary2 =BTlibrary;

Thread t = new Thread(() => { BTlibrary2 = processes(BTlibrary2); }, 2500000);
t.Start();
t.Join();


Session["BTLib"] = BTlibrary2;

// Thread.Sleep(10000);



}

protected void Sendmail(object sender, EventArgs e)
{
List<DirectorDetail> Details = new List<DirectorDetail>();

BTlibrary = (WebDetails) Session["BTLib"];


this worked






share|improve this answer





















  • It would be useful if you could provide a Minimal, Complete, and Verifiable example.
    – AndreaT
    Nov 9 at 7:58















up vote
1
down vote













It seems my problem turned out to be unrelated to variable sharing through threads, but instead to deal with the way button click instances worked.



long story short, in order to share a variable between two event instances, the easiest way to do it is through Sessions.



I needed to put the BTlibrary in a session and access it in the second button click event for it to get the value saved from the previous.



protected void Submit_Click(object sender, EventArgs e)
{

Thread current = Thread.CurrentThread;

WebDetails BTlibrary2 =BTlibrary;

Thread t = new Thread(() => { BTlibrary2 = processes(BTlibrary2); }, 2500000);
t.Start();
t.Join();


Session["BTLib"] = BTlibrary2;

// Thread.Sleep(10000);



}

protected void Sendmail(object sender, EventArgs e)
{
List<DirectorDetail> Details = new List<DirectorDetail>();

BTlibrary = (WebDetails) Session["BTLib"];


this worked






share|improve this answer





















  • It would be useful if you could provide a Minimal, Complete, and Verifiable example.
    – AndreaT
    Nov 9 at 7:58













up vote
1
down vote










up vote
1
down vote









It seems my problem turned out to be unrelated to variable sharing through threads, but instead to deal with the way button click instances worked.



long story short, in order to share a variable between two event instances, the easiest way to do it is through Sessions.



I needed to put the BTlibrary in a session and access it in the second button click event for it to get the value saved from the previous.



protected void Submit_Click(object sender, EventArgs e)
{

Thread current = Thread.CurrentThread;

WebDetails BTlibrary2 =BTlibrary;

Thread t = new Thread(() => { BTlibrary2 = processes(BTlibrary2); }, 2500000);
t.Start();
t.Join();


Session["BTLib"] = BTlibrary2;

// Thread.Sleep(10000);



}

protected void Sendmail(object sender, EventArgs e)
{
List<DirectorDetail> Details = new List<DirectorDetail>();

BTlibrary = (WebDetails) Session["BTLib"];


this worked






share|improve this answer












It seems my problem turned out to be unrelated to variable sharing through threads, but instead to deal with the way button click instances worked.



long story short, in order to share a variable between two event instances, the easiest way to do it is through Sessions.



I needed to put the BTlibrary in a session and access it in the second button click event for it to get the value saved from the previous.



protected void Submit_Click(object sender, EventArgs e)
{

Thread current = Thread.CurrentThread;

WebDetails BTlibrary2 =BTlibrary;

Thread t = new Thread(() => { BTlibrary2 = processes(BTlibrary2); }, 2500000);
t.Start();
t.Join();


Session["BTLib"] = BTlibrary2;

// Thread.Sleep(10000);



}

protected void Sendmail(object sender, EventArgs e)
{
List<DirectorDetail> Details = new List<DirectorDetail>();

BTlibrary = (WebDetails) Session["BTLib"];


this worked







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 8 at 12:49









Kale Williams

112




112












  • It would be useful if you could provide a Minimal, Complete, and Verifiable example.
    – AndreaT
    Nov 9 at 7:58


















  • It would be useful if you could provide a Minimal, Complete, and Verifiable example.
    – AndreaT
    Nov 9 at 7:58
















It would be useful if you could provide a Minimal, Complete, and Verifiable example.
– AndreaT
Nov 9 at 7:58




It would be useful if you could provide a Minimal, Complete, and Verifiable example.
– AndreaT
Nov 9 at 7:58



Popular posts from this blog

Schultheiß

Liste der Kulturdenkmale in Wilsdruff

Android Play Services Check