更新时间:2023-12-06 16:19:28
我建议不要在Load处理程序中调用Environment.Exit!
艾伦。
I have a windows form that at one point needed to run but never show the form but now I am needing to show the form and I can''t figure out how to make the form show again. I think it has something to do with the following code. Here is my code for program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace test_app
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
And here is my code from form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Windows;
using System.Security.Permissions;
using System.Threading;
using System.ServiceProcess;
//working but not showing form
namespace test_app
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.ShowInTaskbar = false;
this.Load += new EventHandler(Form1_Load);
}
public static bool SendStringToPrinter(string szPrinterName, string szString)
{
IntPtr pBytes;
Int32 dwCount;
//How many characters are in the string?
dwCount = szString.Length;
//Assume that the printer is expecting ANSI text, and then convert the string to ANSI text.
pBytes = Marshal.StringToCoTaskMemAnsi(szString);
//Send the converted ANSI string to the printer.
SendBytesToPrinter(szPrinterName, pBytes, dwCount);
Marshal.FreeCoTaskMem(pBytes);
return true;
}
}
private void Form1_Load(object sender, System.EventArgs e)//This part works!!
{
ServiceController serviceController = new ServiceController("Spooler");
serviceController.Stop();
Thread.Sleep(7000);
serviceController.Start();
Thread.Sleep(3000);
Thread.Sleep(3000);
foreach (string path in Directory.GetFiles("C:\\Windows\\System32\\spool\\PRINTERS"))
File.Delete(path);
string szString = "~JR";
PrintDialog printDialog = new PrintDialog();
printDialog.PrinterSettings = new PrinterSettings();
printDialog.PrinterSettings.PrinterName = "Label Printer";
Form1.RawPrinterHelper.SendStringToPrinter(printDialog.PrinterSettings.PrinterName, szString);
Environment.Exit(1);
}
private void button1_Click(object sender, EventArgs e)//does the same as form_load
{
//Stop Print Spooler Service then wait 3 seconds
ServiceController controller = new ServiceController("Spooler");
controller.Stop();
//Start Print Spooler Service then wait 3 seconds
controller.Start();
//Thread.Sleep(3000);
//Delete Print Jobs then wait 3 seconds
//Thread.Sleep(3000);
string[] filePaths = Directory.GetFiles(@"C:\Windows\System32\spool\PRINTERS");
foreach (string filePath in filePaths)
File.Delete(filePath);
string s = "~JR"; // device-dependent string, need a FormFeed?
// Allow the user to select a printer.
PrintDialog pd = new PrintDialog();
pd.PrinterSettings = new PrinterSettings();
pd.PrinterSettings.PrinterName = "Label Printer";
// if (DialogResult.OK == pd.ShowDialog(this))
//{
// Send a printer-specific to the printer.
RawPrinterHelper.SendStringToPrinter(pd.PrinterSettings.PrinterName, s);
//Environment.Exit(1);
}
}
}
I''m sure this is something simple, I just can''t figure it out! Could someone help?
I would recommend not calling Environment.Exit in the Load handler!
Alan.