Jump to content
PamkY

Threads

Recommended Posts

PamkY

Boas,

Gostava de executar 2 threads, uma após a outra e que o programa esperasse pela conclusão das duas para prosseguir.

Até agora tenho isto...  :)

public void Chamar()
{
        ThreadGerarDocumentos();
//Quero que aqui, so continue depois das threads estarem completas
        ImprimirDocumentos();
}

public void ThreadGerarDocumentos()
    {
      System.Threading.ThreadStart PDF =
        new System.Threading.ThreadStart(GerarPDF);
      System.Threading.Thread TPDF =
        new System.Threading.Thread(PDF);
      TPDF.IsBackground = true;
      TPDF.Start();
      
      System.Threading.ThreadStart DOC =
        new System.Threading.ThreadStart(GerarDOC);
      System.Threading.Thread TDOC =
        new System.Threading.Thread(DOC);
      TDOC.IsBackground = true;
      TDOC.Start();
      
    }

    private void GerarPDF()
    {
      Global.HeaderText = DateTime.Now + "; Orçamento número: " + Global.ID_OrcamentoActual + "; Contribuinte: " + Global.FacturaContribuinte + "; Nome: " + Global.FacturaNome;
      try
      {
        DGVEPdfExportSettings exportSettings = new DGVEPdfExportSettings();
        exportSettings.ExportFileName = Global.PDF;
        exportSettings.OpenFileAfterGeneration = false;
        DGVEPdfExporter exporter = new DGVEPdfExporter();
        exportSettings.HeaderText = Global.HeaderText;
        exportSettings.ExportHeaderTextOnEveryPage = true;
        exportSettings.ExportHeaderText = true;
        exportSettings.OpenFolderAfterGeneration = false;
        exportSettings.HeaderTextHorizontalAlignment = CompletIT.Windows.Forms.Export.Pdf.ParagraphHorizontalAlignment.Justified;
        exportSettings.TableHorizontalAlignment = CompletIT.Windows.Forms.Export.Pdf.TableHorizontalAlignment.Center;
        exporter.Export(dataGridView1, exportSettings);
        MessageBox.Show("ok");
      }
      catch (Exception e1)
      {
        MessageBox.Show(e1.ToString());
      }
    }

    private void GerarDOC()
    {
      string Data = DateTime.Today.ToShortDateString();
      string Factura = Convert.ToString(Global.Factura);
      string Rua = Global.FacturaRua;
      string CodPostal = Global.FacturaCodPostal;
      AmmendWordDoc("global::SalvAçor.Resources.Orçamento.doc", Global.DOC, Global.FacturaNome, Global.FacturaContribuinte, Factura, Data, Rua, CodPostal);
    }

private void AmmendWordDoc(object fileName, object saveAs, string Nome, string Contribuinte, string Factura, string Data, string Rua, string CodPostal)
    {
      //This method will open a word document, search for some text, 
      //replace the text and then save the document back to the local machine.
      //Set the missing value paremeter - used to represent a missing value when calling values through interop.

      object missing = System.Reflection.Missing.Value;

      //Setup Word.Application class
      Microsoft.Office.Interop.Word.Application wordApp = new ApplicationClass();

      //Setup the Word.Document class
      Microsoft.Office.Interop.Word.Document aDoc = null;
      fileName = "global::SalvAçor.Properties.Resources.Orçamento.doc";
      //Check to see if the file exists
      if (File.Exists((string)fileName))
      {
        object readOnly = false;
        object isVisible = false;
        wordApp.Visible = false;

        //Open the word doc
        aDoc = wordApp.Documents.Open(ref fileName, ref missing,
        ref readOnly, ref missing, ref missing, ref missing,
        ref missing, ref missing, ref missing, ref missing,
        ref missing, ref isVisible, ref missing, ref missing,
        ref missing, ref missing);

        //Activate the document
        aDoc.Activate();

        //Find the place holders and replace with values
        object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;

        wordApp.Selection.Find.ClearFormatting();
        wordApp.Selection.Find.Text = "[Nome]";
        wordApp.Selection.Find.Replacement.ClearFormatting();
        wordApp.Selection.Find.Replacement.Text = Nome;

        wordApp.Selection.Find.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,
        ref missing, ref missing, ref missing, ref missing, ref missing,
        ref replaceAll, ref missing, ref missing, ref missing, ref missing);

        wordApp.Selection.Find.ClearFormatting();
        wordApp.Selection.Find.Text = "[Contribuinte]";
        wordApp.Selection.Find.Replacement.ClearFormatting();
        wordApp.Selection.Find.Replacement.Text = Contribuinte;

        wordApp.Selection.Find.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,
        ref missing, ref missing, ref missing, ref missing, ref missing,
        ref replaceAll, ref missing, ref missing, ref missing, ref missing);

        wordApp.Selection.Find.ClearFormatting();
        wordApp.Selection.Find.Text = "[Valor]";
        wordApp.Selection.Find.Replacement.ClearFormatting();
        wordApp.Selection.Find.Replacement.Text = Factura;

        wordApp.Selection.Find.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,
        ref missing, ref missing, ref missing, ref missing, ref missing,
        ref replaceAll, ref missing, ref missing, ref missing, ref missing);

        wordApp.Selection.Find.ClearFormatting();
        wordApp.Selection.Find.Text = "[Data]";
        wordApp.Selection.Find.Replacement.ClearFormatting();
        wordApp.Selection.Find.Replacement.Text = DateTime.Today.Day.ToString() + " de " + DateTime.Today.Month.ToString() + " de " + DateTime.Today.Year.ToString(); ;

        wordApp.Selection.Find.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,
        ref missing, ref missing, ref missing, ref missing, ref missing,
        ref replaceAll, ref missing, ref missing, ref missing, ref missing);

        wordApp.Selection.Find.ClearFormatting();
        wordApp.Selection.Find.Text = "[Rua]";
        wordApp.Selection.Find.Replacement.ClearFormatting();
        wordApp.Selection.Find.Replacement.Text = Rua;

        wordApp.Selection.Find.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,
        ref missing, ref missing, ref missing, ref missing, ref missing,
        ref replaceAll, ref missing, ref missing, ref missing, ref missing);

        wordApp.Selection.Find.ClearFormatting();
        wordApp.Selection.Find.Text = "[CodPostal]";
        wordApp.Selection.Find.Replacement.ClearFormatting();
        wordApp.Selection.Find.Replacement.Text = CodPostal;

        wordApp.Selection.Find.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,
        ref missing, ref missing, ref missing, ref missing, ref missing,
        ref replaceAll, ref missing, ref missing, ref missing, ref missing);

        //Save document as the correct file name.
        saveAs = Global.DOC;
        aDoc.SaveAs(ref saveAs, ref missing, ref missing, ref missing,
        ref missing, ref missing, ref missing, ref missing,
        ref missing, ref missing, ref missing, ref missing,
        ref missing, ref missing, ref missing, ref missing);

        aDoc.Close(ref missing, ref missing, ref missing);
        MessageBox.Show("doc OK");
      }
    

Share this post


Link to post
Share on other sites
bruno1234

Podes guardar as threads que vais correr num array ou numa lista, e quando quiseres esperar que todas terminem fazes um foreach a esse array que por cada Thread chama o método Join().

Claro que existem mais formas de fazer isso, mas esta é uma solução simples.


Matraquilhos para Android.

Gratuito na Play Store.

https://play.google.com/store/apps/details?id=pt.bca.matraquilhos

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.