Je développe un programme qui reçoit des données et après le traitement je veux l'imprimer.Il devrait être l'impression automatique. Les données sont ajoutées en tant qu'enregistrements à la file d'attente printList. Ensuite un fil est censé les imprimer un par un. le code:Comment implémenter l'impression automatique avec des threads d'arrière-plan?
private void button4_Click(object sender, EventArgs e)
{
rwl.AcquireWriterLock(10);
try
{
if (automaticPrint == false)
{
automaticPrint = true;
_automaticPrintThread = new Thread(new ThreadStart(AutomaticPrintA4));
_automaticPrintThread.IsBackground = true;
_automaticPrintThread.Start();
}
else
{
automaticPrint = false;
if (_automaticPrintThread != null)
{
_automaticPrintThread.Join(1);
}
}
}
finally
{
rwl.ReleaseWriterLock();
}
}
private void AutomaticPrintA4()
{
try
{
this.AutomaticPrintA4Delegate();
}
catch (Exception e)
{
this._automaticPrintThread.Abort();
MessageBox.Show(e.StackTrace);
}
}
private void AutomaticPrintA4Delegate()
{
try
{
if (this.InvokeRequired)
this.Invoke(new MethodInvoker(AutomaticPrintA4Delegate));
else
{
rwl.AcquireReaderLock(100);
Telerik.Reporting.Processing.ReportProcessor rp = new Telerik.Reporting.Processing.ReportProcessor();
System.Drawing.Printing.PrinterSettings ps = new System.Drawing.Printing.PrinterSettings();
try
{
while (automaticPrint)
{
rwlprintlist.AcquireReaderLock(10);
try
{
if (Session.printList != null)
{
if (Session.printList.Count != 0)
{
if (Session.printList[0] != null)
{
rp.PrintReport(new Report(Session.printList[0]), ps);
LockCookie lc = rwlprintlist.UpgradeToWriterLock(10);
Session.printList.RemoveAt(0);
rwlprintlist.DowngradeFromWriterLock(ref lc);
}
}
else
{
Thread.Sleep(1000);
}
}
else
{
Thread.Sleep(1000);
}
}
finally
{
rwlprintlist.ReleaseReaderLock();
}
}
}
finally
{
rwl.ReleaseReaderLock();
}
}
}
catch (Exception e)
{
MessageBox.Show("Print : " + e.StackTrace + e.Message);
}
}
Un clic sur un bouton contrôle la variable d'empreinte automatique. rwl contrôle l'accès à print automatique et rwlprintlist contrôle l'accès à printList. mais mon principal gui se bloque chaque fois que je commence ce fil. Je ne sais pas pourquoi?
P.S. toute autre idée de design serait appréciée.
Juste comme une note: C'est une idée horrible dans de nombreux environnements. Je suis sûr que vous avez vos raisons mais vous devriez réfléchir à deux fois chaque fois qu'une application décide d'utiliser (lire: les déchets) les ressources de l'entreprise sans intervention humaine. –
C'était la demande de mon utilisateur, je ne peux pas le nier. – armin