Note that there are some explanatory texts on larger screens.

plurals
  1. POExecuting a function that accesses global array on multiple threads
    text
    copied!<p>I have a function 'graph1' that calls another function as shown below. When I call </p> <pre><code>graph1threader(copy, date); </code></pre> <p>I get the answer I want, however this takes upwards of 30 seconds, so I tried using multithreading. However when I use</p> <pre><code>thread[copy] = new Thread(() =&gt; graph1threader(copy, date));//pass date,copy thread[copy].Start(); </code></pre> <p>I get no result i.e. the global variables hold only 0. Why is this? And how may I rectify this?</p> <pre><code>void graph1() { chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = false; chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = false; for (int k = 0; k &lt; 5; k++) { int copy = k; DateTime date = G.AddDays(copy); refValues[copy] = 0; threshValues[copy] = 0; y_Values[copy] = 0; y__Values[copy] = 0; yValues[copy] = 0; //task[copy] = new Task(() =&gt; graph1threader(copy, date));//pass date,copy //ask[copy].Start(); graph1threader(copy, date); } for (int j = 0; j &lt; 5; j++) { DateTime temp = G.AddDays(j); string temper = temp.ToShortDateString(); y__Values[j] = y__Values[j] - y_Values[j]; xNames[j] = temper; } chart1.Series[1].Points.DataBindXY(xNames, y_Values); chart1.Series[2].Points.DataBindXY(xNames, y__Values); chart1.Series[3].Points.DataBindXY(xNames, refValues); chart1.Series[4].Points.DataBindXY(xNames, threshValues); chart1.Series[5].Points.DataBindXY(xNames, yValues); } void graph1threader(int x, DateTime date) { DBConnect A = new DBConnect(); List&lt;string&gt;[] list = new List&lt;string&gt;[4]; list = A.mandetselect(); int numberofmen = A.Countmandet(); string[] man_name = list[0].ToArray(); string[] trade = list[1].ToArray(); string[] license = list[2].ToArray(); string[] training = list[3].ToArray(); string[] display_status = list[4].ToArray(); //string abc; List&lt;string&gt;[] lista = new List&lt;string&gt;[5]; List&lt;string&gt;[] listc = new List&lt;string&gt;[14]; for (int j = 0; j &lt; numberofmen; j++) { int flag = 0; if (!display_status[j].Equals("NO") &amp;&amp; (selection == "ALL" || (selection == "LAE" &amp;&amp; license[j] != "") || (selection == "NON LAE" &amp;&amp; license[j] == "") || (selection == "ALL AVIONICS" &amp;&amp; trade[j] == "Avionics") || (selection == "NON LAE AVIONICS" &amp;&amp; trade[j] == "Avionics" &amp;&amp; license[j] == "") || (selection == "LAE AVIONICS" &amp;&amp; trade[j] == "Avionics" &amp;&amp; license[j] != "") || (selection == "ALL AIRFRAMES" &amp;&amp; trade[j] == "Airframes") || (selection == "NON LAE AIRFRAMES" &amp;&amp; trade[j] == "Airframes" &amp;&amp; license[j] == "") || (selection == "LAE AIRFRAMES" &amp;&amp; trade[j] == "Airframes" &amp;&amp; license[j] != ""))) { refValues[x]++; threshValues[x] = 0.8 * refValues[x]; string abc = man_name[j].Replace(" ", "_"); int no_of_proj = A.Countproj(abc);//required lista = A.manprojselect(abc);//required string[] projname = lista[0].ToArray(); string[] country = lista[2].ToArray(); string[] startofproj = lista[3].ToArray(); string[] endofproj = lista[4].ToArray(); string Status = ""; listc = A.Select(); int numberc = A.Count();//number of projects, not required string[] nameofproj = listc[0].ToArray(); string[] status = listc[13].ToArray(); for (int l = 0; l &lt; A.Countproj(abc); l++) { for (int m = 0; m &lt; numberc; m++) { if (nameofproj[m] == projname[l]) { Status = status[m]; } } DateTime shuru = DateTime.ParseExact(startofproj[l], "dd-MM-yyyy hh:mm:ss", CultureInfo.InvariantCulture); DateTime anth = DateTime.ParseExact(endofproj[l], "dd-MM-yyyy hh:mm:ss", CultureInfo.InvariantCulture); if (date &gt;= shuru &amp;&amp; date &lt;= anth) { if (Status != "PLANNED" &amp;&amp; Status != "LO" &amp;&amp; flag == 0) { y_Values[x]++;//BASIC UTILISATION flag = 1; } if (Status == "IP" || Status == "OTD") y__Values[x]++;//EXCESS if (Status == "PLANNED") { yValues[x]++;//UNUTILISED } } } } } } </code></pre> <p>I came across multi-threading only recently. So excuse me if the code doesn't look very good. <code>threshValue[]</code>,<code>refValues[]</code>,<code>y_Values[]</code>, <code>y__Values[]</code>, <code>yValues[]</code> are all global variables</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload