我正在开发一个C#项目,我需要在30秒后删除该文件。因此,一旦文件发送到机器,我需要软件计数到30秒,同时显示一个启动窗体,一旦30秒越过关闭启动画面,然后删除文件。
我添加了一个名为“image”的启动画面。所以现在发生的事情是,数据仅在启动屏幕关闭后发送到打印机。我需要多线程的工作。我的意思是数据应该在一面打印,而启动画面应该同时显示。有没有办法可以出来!! ..请帮帮我。
所以在我的情况下,我将文件复制到bin / debug文件夹。然后将数据发送到机器同时显示启动画面30秒并关闭启动画面然后我需要删除文件..
代码:
private void button4_Click(object sender, EventArgs e)
{
//string filePath = image_print();
// MessageBox.Show(filePath, "path");
string s = image_print() + Print_image();
if (String.IsNullOrEmpty(s) || img_path.Text == "")
{
return;
}
else
{
//here its coming to the splash screen code, But data is transferred to the machine only after the splash screen is close :-(
this.Hide();
omg = new image();
omg.ShowDialog();
this.Show();
//splash screen closed and then data is transferred.. which i don't need.. i need simultaneous job to be done at the same time..
PrintFactory.sendTextToLPT1(s);
}
}
private string image_print()
{
OpenFileDialog ofd = new OpenFileDialog();
string path = "";
string full_path = "";
string filename_noext = "";
ofd.InitialDirectory = @"C:\ZTOOLS\FONTS";
ofd.Filter = "GRF files (*.grf)|*.grf";
ofd.FilterIndex = 2;
ofd.RestoreDirectory = true;
if (ofd.ShowDialog() == DialogResult.OK)
{
filename_noext = System.IO.Path.GetFileName(ofd.FileName);
path = Path.GetFullPath(ofd.FileName);
img_path.Text = filename_noext;
//MessageBox.Show(filename_noext, "Filename"); - - -> switching.grf
// MessageBox.Show(full_path, "path");
//move file from location to debug
string replacepath = @"\\bin\Debug";
string fileName = System.IO.Path.GetFileName(path);
string newpath = System.IO.Path.Combine(replacepath, fileName);
// string newpath = string.Empty;
if (!System.IO.File.Exists(filename_noext))
System.IO.File.Copy(path, newpath);
filename_noext = img_path.Text;
MessageBox.Show(filename_noext, "path");
}
if (string.IsNullOrEmpty(img_path.Text))
return "";//
StreamReader test2 = new StreamReader(img_path.Text);
string s = test2.ReadToEnd();
return s;
}
private string Print_image()
{
//some codes
return s;
}
以图片形式:我有以下代码
public partial class image : Form
{
string filePath;
public image()
{
InitializeComponent();
// this.filePath = FileToDeletePath;
System.Timers.Timer timer1 = new System.Timers.Timer();
timer1.Interval = 30000;
timer1.Elapsed += timer1_Elapsed;
timer1.Start();
}
private void image_Load(object sender, EventArgs e)
{
}
void timer1_Elapsed(object sender, ElapsedEventArgs e)
{
//delete the file using "filePath"
string Filename = img_path.Text; // here i cannot pass the old string file name with extension to this form.. Any ways please help me out
if (string.IsNullOrEmpty(Filename))
return;
if (Filename.ToCharArray().Intersect(Path.GetInvalidFileNameChars()).Any())
return;
File.Delete(Path.Combine(@"\\bin\Debug", Filename));
}
}
答案 0 :(得分:2)
Task waitfordelete = Task.Run(() =>
{
image im = new image();
});
答案 1 :(得分:2)
假设:窗口image
应显示为对话框(模态),且仅在调用PrintFactory.sendTextToLPT1
时才会显示。
如果这是正确的,那么这样的事情对你有用:
// Don't forget, you need to dispose modal dialogs
image omg = new image();
// Ensure the dialog has been shown before starting task. That
// way the task knows for sure the dialog's been opened and can
// be closed.
omg.Loaded += (sender, e) =>
{
// Run the print task in a separate task
Task.Run(() =>
{
PrintFactory.sendTextToLPT1(s);
// But get back onto the main GUI thread to close the dialog
Dispatcher.Invoke(() => omg.Close());
});
};
this.Hide();
omg.ShowDialog();
this.Show();
提前为任何拼写错误/语法错误/等道歉。希望上述内容足以表达一般概念。
答案 2 :(得分:1)
Narzul 和 Peter 给出的答案都是正确的。你可以实现任何一个。但是,我知道您的下一个问题是如何在代码中实现该方法。
您可以使用Thread
或Task
类对象来分隔流程。因此,当一个进程正在运行时,其他进程可以在那时执行其进程。登录中有两个进程。第一个是将文件发送到打印机,第二个是显示对话框30秒,然后删除文件。您应该创建另一个线程来调用任何一个进程,以便其他进程可以异步执行。
1st:为打印文件创建单独的进程。
Task waitfordelete = Task.Run(() =>
{
PrintFactory.sendTextToLPT1(s);
});
this.Hide();
omg = new image();
omg.ShowDialog();
this.Show();
2nd:为show对话框创建单独的进程并删除该文件。但是,我认为你可能会在这种方法中得到错误。 您无法从其他线程更改UI
Task waitfordelete = Task.Run(() =>
{
Dispatcher.Invoke(() => this.ShowSplashScreen());
});
PrintFactory.sendTextToLPT1(s);
private void ShowSplashScreen()
{
this.Hide();
omg = new image();
omg.ShowDialog();
this.Show();
}
如果您不想使用该线程或任务,那么只需处理Image
表单的关闭事件
this.Hide();
omg = new image();
omg.Show();
PrintFactory.sendTextToLPT1(s);
omg.FormClosed += (object sender, EventArgs e) => {
File.Delete(Path.Combine(Application.StartupPath, Path.GetFileName(img_path.Text));
this.Show();
};
并以timer_tick
形式修改Image
事件中的代码,并在删除文件语句后添加this.Close()
。
void timer1_Elapsed(object sender, ElapsedEventArgs e)
{
....
//File.Delete(Path.Combine(@"\\bin\Debug", Filename)); comment this line
this.Close();
}
我在这里找到的另一个隐藏的问题。 在这里,我无法传递旧的字符串文件名,扩展名为此表单..任何方式请帮助我
void timer1_Elapsed(object sender, ElapsedEventArgs e)
{
//delete the file using "filePath"
string Filename = img_path.Text; // here i cannot pass the old string file name with extension to this form.. Any ways please help me out
为此,您可以在Image类中创建属性,并从父窗体中分配文件名。
Image omg = new Image()
omg.FileName = Path.Combine(Application.StartupPath, Path.GetFileName(img_path.Text));
omg.Show();
并且Image形式的属性将像这样创建
public class Image : Form
{
public string FileName { get; set; }
public Image()
{
}
void timer1_Elapsed(object sender, ElapsedEventArgs e)
{
....
File.Delete(Path.Combine(Application.StartupPath, this.Filename));
this.Close();
}
}
注意:使用Application.StartupPath
而不是\\bin\debug