Lettura di valori di celle in thread multipli contemporaneamente

Contents
[ ]

Per leggere i valori delle celle in più di un thread contemporaneamente, impostare Worksheet.Cells.MultiThreadReading su true. In caso contrario, si potrebbero ottenere valori di celle errati.

Il seguente codice:

  1. Crea un workbook.
  2. Aggiunge un foglio di lavoro.
  3. Popola il foglio di lavoro con valori di stringa.
  4. Quindi crea due thread che leggono contemporaneamente valori da celle casuali. Se i valori letti sono corretti, non succede nulla. Se i valori letti non sono corretti, viene visualizzato un messaggio.

Se si commenta questa riga:

 testWorkbook.Worksheets[0].Cells.MultiThreadReading = true;

allora viene visualizzato il seguente messaggio:

 if (s != "R" + row + "C" + col)

{

    MessageBox.Show("This message box will show up when cells read values are incorrect.");

}

In caso contrario, il programma viene eseguito senza mostrare alcun messaggio, il che significa che tutti i valori letti dalle celle sono corretti.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
public static void Main()
{
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
}
public static Workbook testWorkbook;
public static void ThreadLoop()
{
Random random = new Random();
while (Thread.CurrentThread.IsAlive)
{
try
{
int row = random.Next(0, 10000);
int col = random.Next(0, 100);
string s = testWorkbook.Worksheets[0].Cells[row, col].StringValue;
if (s != "R" + row + "C" + col)
{
Console.WriteLine("This message will show up when cells read values are incorrect.");
}
}
catch { }
}
}
public static void TestMultiThreadingRead()
{
testWorkbook = new Workbook();
testWorkbook.Worksheets.Clear();
testWorkbook.Worksheets.Add("Sheet1");
for (var row = 0; row < 10000; row++)
for (var col = 0; col < 100; col++)
testWorkbook.Worksheets[0].Cells[row, col].Value = "R" + row + "C" + col;
// Commenting this line will show a pop-up message
// testWorkbook.Worksheets[0].Cells.MultiThreadReading = true;
Thread myThread1;
myThread1 = new Thread(new ThreadStart(ThreadLoop));
myThread1.Start();
Thread myThread2;
myThread2 = new Thread(new ThreadStart(ThreadLoop));
myThread2.Start();
System.Threading.Thread.Sleep(5 * 1000);
myThread1.Abort();
myThread2.Abort();
}