【问题标题】:Print with Task使用任务打印
【发布时间】:2015-05-28 20:43:30
【问题描述】:

我正在使用 Graphics 创建一个标签并使用 PrintDocument 打印它,之前我没有遇到任何问题,因为标签上的信息重复了多次,所以我只需设置副本,一切都会正常工作。

但是现在每个标签都有不同的信息,所以它冻结了 UI,因为我必须循环 PrintDocument.Print() 多次而不是设置副本。

我试过了:

 Task t = new Task(() =>
                    {

                        for (int i = 0; i < Copies; i++)
                        {
                            printDocument1.Print();                                
                            Label_Copies++;
                        }
                    });
 t.Start();

我正在使用以下代码在 printDocument1_PrintPage 上绘制标签:

int base_y = 5;
int base_x = 7;
int fontsize_bold = 8;
int fontsize_regular = 7;

RectangleF baserect = new RectangleF(base_x, base_y + 20, 0, 0);
SolidBrush whiteBrush = new SolidBrush(System.Drawing.Color.White);
SolidBrush blackBrush = new SolidBrush(System.Drawing.Color.Black);
string font = "Cabriolli";
Pen mypen = new Pen(blackBrush);
e.Graphics.FillRectangle(whiteBrush, new Rectangle(0, 0, 320, 130));
//e.Graphics.DrawRectangle(mypen, 1, 1, 225, 140);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.DrawString("REMETENTE:",
     new Font(font, fontsize_regular, System.Drawing.FontStyle.Regular),
     Brushes.Black, new RectangleF(base_x, base_y, 0, 0));
base_y += 10;
e.Graphics.DrawString("BETA COMERCIAL IMPORTADORA LTDA",
    new Font(font, fontsize_bold, System.Drawing.FontStyle.Bold),
    Brushes.Black, new RectangleF(base_x, base_y, 0, 0));
base_y += 15;
e.Graphics.DrawString("DESTINATARIO:",
   new Font(font, fontsize_regular, System.Drawing.FontStyle.Regular),
   Brushes.Black, new RectangleF(base_x, base_y, 0, 0));
base_y += 10;
e.Graphics.DrawString(Etiqueta_Destinatario, // *cliente
    new Font(font, fontsize_bold, System.Drawing.FontStyle.Bold),
    Brushes.Black, new RectangleF(base_x, base_y, 0, 0));
base_y += 15;
e.Graphics.DrawString("NOTA FISCAL:",
  new Font(font, fontsize_regular, System.Drawing.FontStyle.Regular),
  Brushes.Black, new RectangleF(base_x, base_y + 3, 0, 0));
e.Graphics.DrawString(Etiqueta_Nota, // *nota
    new Font(font, 10, System.Drawing.FontStyle.Bold),
    Brushes.Black, new RectangleF(base_x + 68, base_y, 0, 0));
base_y += 15;
e.Graphics.DrawString("PEDIDO:",
   new Font(font, fontsize_regular, System.Drawing.FontStyle.Regular),
   Brushes.Black, new RectangleF(base_x, base_y + 3, 0, 0));
e.Graphics.DrawString(Etiqueta_Pedido, // *pedido
    new Font(font, 10, System.Drawing.FontStyle.Bold),
    Brushes.Black, new RectangleF(base_x + 43, base_y, 0, 0));
base_y += 15;
e.Graphics.DrawString("Etiqueta numero:",
new Font(font, fontsize_regular, System.Drawing.FontStyle.Regular),
Brushes.Black, new RectangleF(base_x + 140, base_y - 20, 0, 0));
if (Copias.Checked)
{
    e.Graphics.DrawString(Etiqueta_Copias.ToString(),
    new Font(font, 15, System.Drawing.FontStyle.Regular),
   Brushes.Black, new RectangleF(base_x + 140, base_y, 0, 0));
}
else
{
    e.Graphics.DrawString(Etiqueta_Copia.ToString(),
     new Font(font, 15, System.Drawing.FontStyle.Regular),
    Brushes.Black, new RectangleF(base_x + 140, base_y, 0, 0));
}
e.Graphics.DrawString("VOLUMES:",
   new Font(font, fontsize_regular, System.Drawing.FontStyle.Regular),
   Brushes.Black, new RectangleF(base_x, base_y + 3, 0, 0));
e.Graphics.DrawString(peças_textbox.Text, // *quantidade
    new Font(font, 10, System.Drawing.FontStyle.Bold),
    Brushes.Black, new RectangleF(base_x + 53, base_y, 0, 0));
base_y += 25;
e.Graphics.DrawString("TRANSPORTADORA:",
   new Font(font, fontsize_regular, System.Drawing.FontStyle.Regular),
   Brushes.Black, new RectangleF(base_x, base_y, 0, 0));
base_y += 14;
e.Graphics.DrawString(nota_transportadora_combobox.Text, // *transportadora
    new Font(font, 10, System.Drawing.FontStyle.Bold),
    Brushes.Black, new RectangleF(base_x, base_y, 0, 0));
whiteBrush.Dispose();
blackBrush.Dispose();
mypen.Dispose();

但它不起作用有没有办法解决它?

-信息来自ui
-500~ 标签
- 在这些副本中,从标签到标签的唯一信息是

    if (Copias.Checked)
{
    e.Graphics.DrawString(Etiqueta_Copias.ToString(),
    new Font(font, 15, System.Drawing.FontStyle.Regular),
   Brushes.Black, new RectangleF(base_x + 140, base_y, 0, 0));
}
else
{
    e.Graphics.DrawString(Etiqueta_Copia.ToString(),
     new Font(font, 15, System.Drawing.FontStyle.Regular),
    Brushes.Black, new RectangleF(base_x + 140, base_y, 0, 0));
}

它基本上是一个标签编号的计数器......

【问题讨论】:

  • 我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
  • it doesn't work。什么不起作用?什么都没有打印?抛出异常?
  • @LarsTech 是的,什么都没有打印出来
  • 这段代码在没有任务的情况下有效吗?如果是这样,我的猜测是绘制图形需要在调度程序上进行调用,并且您的任务在任务池线程上执行。
  • @Chris 是的,代码可以在没有任务的情况下工作,但 ui 冻结,这是我的问题;/

标签: c# printing task-parallel-library


【解决方案1】:

在我看来,如果我们忽略打印机的假脱机端,您的代码就是纯 WinForms UI。 由于工作量成倍增加,比如说 100 份,它变得太多了 Continous UI。

Tasking 对你没有帮助,因为我认为打印面是“一劳永逸”,所以即使打印每张贴纸需要 30 秒,打印面也会立即返回,让 UI 开始它的繁重工作下一个贴纸,几乎在它完成最后一个之后立即。

假设每个贴纸 UI 必须工作 1 秒,您正在查看 100 秒的 UI 阻塞... 我的建议:

依靠后面的打印需要 20 秒这一事实:使用计时器而不是循环启动打印。将其设置为 5、10 或 20 秒(与您的打印机速度相匹配)。每次触发计时器时,UI 只会处理 1 张贴纸,然后静置 5、10 或 20 秒...

【讨论】:

  • 我认为这首先不会有帮助,因为使 ui 冻结的是 printDocument1.Print(); ,并且我打印每种类型的 500~ 个标签,打印机打印一个标签的时间不到一秒钟,这里的问题是只有 1 个不是来自 ui 的信息发生变化,所以我无法制作副本,我必须是页面或像这样的循环,计时器不起作用,因为打印所有内容需要很长时间,它仍然会冻结用户界面。
  • 也许你可以试试这个Draw on a form by a separate thread。也许它可以让您在不实际将位图绘制到表单的情况下进行打印。无论如何,定时器很容易尝试,虽然一些改进不是解决方案,但它仍然是一种改进......
猜你喜欢
  • 2015-03-10
  • 2020-06-29
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-26
相关资源
最近更新 更多