Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A clever use of <code>ReaderWriterLockSlim</code> will definitely help you improve performance.</p> <pre><code> private ReaderWriterLockSlim _readerWriterLock = new ReaderWriterLockSlim(); private DataTable RunSelectSQL(string Sql) { DataTable selectDataTable = null; try { _readerWriterLock.EnterReadLock(); //Function to acess your database and return the selected results } finally { _readerWriterLock.ExitReadLock(); } return selectDataTable; } private DataTable RunInsertSQL(string Sql) { DataTable selectDataTable = null; bool isbreaked = false; try { _readerWriterLock.EnterWriteLock(); if (_readerWriterLock.WaitingReadCount &gt; 0) { isbreaked = true; } else { //Function to insert data in your database } } finally { _readerWriterLock.ExitWriteLock(); } if (isbreaked) { Thread.Sleep(10); return RunInsertSQL(Sql); } return selectDataTable; } </code></pre> <p>Try this it will, improve your responsiveness and you have <code>Select</code> query to fire having higher priority over <code>Insert</code> SQL.</p> <p>Please note, if some insertion is already running then Select will at least wait for that insertion to complete. This code will always give priority to <code>SELECT</code> over <code>INSERT</code>. </p> <p><strong>One more point</strong>, never perform the <strong>long ongoing operation</strong> on main thread like you have <strong>selecting from database</strong>, rather perform the operation in background and then reflect the latest results on UI using main thread. This will ensure that your UI will never freeze.</p> <p><strong>EDIT</strong> There can be a <strong>starvation</strong> case where all <code>INSERT</code> may be waiting, if there are continuous <code>SELECT</code> queries being fired without any gap.<br> But I believe in your case, this will not happen as the <code>UI</code> will not always be refreshing to get the latest changes so frequently without any time slice in between.</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