Skip to content

Commit

Permalink
fix bug where print command was done before loading document
Browse files Browse the repository at this point in the history
Release 1.1.0.0
  • Loading branch information
Vlad Somai authored and Vlad Somai committed Jul 9, 2022
1 parent 2f4ce1c commit b6a500f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
21 changes: 17 additions & 4 deletions Necta/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
using Necta.API;
using Necta.NectaServices;
using System.Windows.Threading;
using System.Drawing.Printing;

namespace Necta
{
public partial class Necta : MaterialForm
{
private static readonly WebBrowser webBrowser = new WebBrowser();
public static readonly WebBrowser webBrowser = new WebBrowser();
public static bool mHtmlDocumentIsLoaded { get; set; }

//the following dispatcher object will be used to invoke the print method from another thread
public static Dispatcher MainThreadDispatcher = Dispatcher.CurrentDispatcher;

public Necta()
{
InitializeComponent();
Expand All @@ -39,6 +39,8 @@ public Necta()
{
NectaLogService.WriteLog(ex.Message, LogLevels.ERROR);
}

webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(OnHtmlDocumentIsLoaded);
}

private void SaveButton_Click(object sender, EventArgs e)
Expand Down Expand Up @@ -101,7 +103,6 @@ private void SaveAndValidateURI(bool isFirstPaint = false)
public static void PrintReceipt(Receipt receipt)
{
bool defaultPrinterResult = WinPrinter.SetDefaultPrinter(receipt.PrinterName);
webBrowser.DocumentText = receipt.HTML;

if (defaultPrinterResult)
{
Expand All @@ -112,6 +113,7 @@ public static void PrintReceipt(Receipt receipt)
try
{
webBrowser.Print();
mHtmlDocumentIsLoaded = false;
}
catch (Exception ex)
{
Expand All @@ -128,6 +130,17 @@ public static void PrintReceipt(Receipt receipt)
}
}

public static void LoadHtmlDocument(string receiptHtml)
{
webBrowser.DocumentText = receiptHtml;
}

public static void OnHtmlDocumentIsLoaded(object sender, WebBrowserDocumentCompletedEventArgs e)
{
NectaLogService.WriteLog("HTML document successfully loaded! ", LogLevels.INFO);
mHtmlDocumentIsLoaded = true;
}

private void NectaNotifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
Show();
Expand Down
2 changes: 1 addition & 1 deletion Necta/Necta.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<Deterministic>true</Deterministic>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<IsWebBootstrapper>false</IsWebBootstrapper>
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
Expand All @@ -26,7 +27,6 @@
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup>
Expand Down
13 changes: 11 additions & 2 deletions Necta/NectaServices/NectaService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Necta.API;
using System;
using System.Collections.Generic;
using System.Media;
using System.Text.Json;
using System.Threading;

Expand All @@ -10,10 +9,12 @@ namespace Necta.NectaServices
class NectaService
{
public delegate void PrintReceiptDelegate(Receipt receipt);
public delegate void LoadHtmlDelegate(string receiptHTML);

public static void RunService()
{
PrintReceiptDelegate PRdel = new PrintReceiptDelegate(Necta.PrintReceipt);
LoadHtmlDelegate LHDdel = new LoadHtmlDelegate(Necta.LoadHtmlDocument);

List<Receipt> receipts = null;

Expand Down Expand Up @@ -92,7 +93,15 @@ public static void RunService()
continue;
}

Necta.MainThreadDispatcher.Invoke(PRdel, new object[] { receipt });//call PrintReceipt from main thread
//load the html document on the main thread
Necta.MainThreadDispatcher.Invoke(LHDdel, new object[] { receipt.HTML });//call PrintReceipt from main thread

//wait for the document to complete loading(done in main thread)
while (!Necta.mHtmlDocumentIsLoaded)
Thread.Sleep(100);

//call PrintReceipt from main thread
Necta.MainThreadDispatcher.Invoke(PRdel, new object[] { receipt });
}
catch (Exception ex)
{
Expand Down
4 changes: 2 additions & 2 deletions Necta/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]

0 comments on commit b6a500f

Please sign in to comment.