I am working on SAP B1 add-on and try to make it possible to select printername from PrintDialog. Because of this error:
Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it.
I made a version of this solution for OpenFileDialog: https://stackoverflow.com/a/49553212
Thread t = new Thread(() =>
{
using (var printerDialog = new System.Windows.Forms.PrintDialog())
{
var dr = printerDialog.ShowDialog(new System.Windows.Forms.Form());
if (dr == System.Windows.Forms.DialogResult.OK)
{
var printer = printerDialog.PrinterSettings.PrinterName;
var cel = matrix.Columns.Item(colUID).Cells.Item(row).Specific;
((EditText)cel).String = printer;
}
}
});
// Kick off a new thread
t.IsBackground = true;
t.SetApartmentState(ApartmentState.STA);
t.Start();
But for code works only 1 time. Second time program breaks on the following line, without error.
var dr = printerDialog.ShowDialog(new System.Windows.Forms.Form());
After restarting application is works again 1 time.