I keep getting errors when running my app. It keep telling me Im trying to run mutli backgroundworkers at the same time. It seems like the IsBusy is not working. Im basically using the backgroundworker to copy files from A to B over a network. Im using FileSystemWatcher to trigger the function of the start of the backgroundworker.
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Diagnostics;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Threading;
- using System.IO;
-
- namespace WindowsFormsApplication9
- {
- public partial class Form1 : Form
- {
- public static string port;
- public static string AE;
- public static string dicompath;
- public static string finalpath;
- public static string IPbox;
- BackgroundWorker m_oWorker;
- public Form1()
- {
- InitializeComponent();
- m_oWorker = new BackgroundWorker();
- m_oWorker.DoWork += new DoWorkEventHandler(m_oWorker_DoWork);
- m_oWorker.ProgressChanged += new ProgressChangedEventHandler(m_oWorker_ProgressChanged);
- m_oWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(m_oWorker_RunWorkerCompleted);
- m_oWorker.WorkerReportsProgress = true;
- m_oWorker.WorkerSupportsCancellation = true;
- }
- struct DataParameter
- {
- public int Process;
- public int Delay;
- }
- private DataParameter _inputparameter;
-
- private void Form1_Load(object sender, EventArgs e)
- {
- AEtitlescp.Text = Properties.Settings.Default.AEtitleSCP;
- Port1.Text = Properties.Settings.Default.Port1;
- Ports2.Text = Properties.Settings.Default.Port2;
- IPBox.Text = Properties.Settings.Default.IPBox;
- CalledAE.Text = Properties.Settings.Default.CalledAE;
- CallingAE.Text = Properties.Settings.Default.CallingAE;
- FileLocation.Text = Properties.Settings.Default.FileLocation;
- this.Location = new Point(Properties.Settings.Default.PX, Properties.Settings.Default.PY);
-
-
-
-
-
- this.WindowState = FormWindowState.Minimized;
- this.Visible = false;
- Use_Notify();
-
-
- }
- void m_oWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
- {
-
-
-
- }
-
-
- private void Use_Notify()
- {
- MyNotify.ContextMenuStrip = contextMenuStrip1;
- MyNotify.BalloonTipText = "Pacs Care Dicom Agent";
- MyNotify.BalloonTipTitle = "Please check Configuration and start agent...";
- MyNotify.ShowBalloonTip(1);
- }
-
- private void Form1_Resize(object sender, System.EventArgs e)
- {
-
- if (FormWindowState.Minimized == WindowState)
- Hide();
- }
- private void MyNotify_DoubleClick(object sender, System.EventArgs e)
- {
-
- Show();
- WindowState = FormWindowState.Normal;
- }
- private void closeToolStripMenuItem_Click(object sender, EventArgs e)
- {
-
- MyNotify.Dispose();
- Application.Exit();
- }
-
- private void restoreToolStripMenuItem_Click(object sender, EventArgs e)
- {
-
- Show();
- WindowState = FormWindowState.Normal;
- }
-
- private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
- {
-
- MessageBox.Show("Your Application Settings 1");
- }
-
- private void settings2ToolStripMenuItem_Click(object sender, EventArgs e)
- {
-
- MessageBox.Show("Your Application Settings 2");
- }
-
- private void MyNotify_MouseDoubleClick(object sender, MouseEventArgs e)
- {
-
- }
-
- private void settingsToolStripMenuItem_Click_1(object sender, EventArgs e)
- {
-
- }
-
- private void fileLocationToolStripMenuItem_Click(object sender, EventArgs e)
- {
-
- }
-
- private void FileLocation_TextChanged(object sender, EventArgs e)
- {
-
- }
-
- private void exitToolStripMenuItem_Click(object sender, EventArgs e)
- {
-
-
-
- }
-
- private void fileLocationToolStripMenuItem_Click_1(object sender, EventArgs e)
- {
-
- }
-
- private void configurationToolStripMenuItem_Click(object sender, EventArgs e)
- {
-
-
- WindowState = FormWindowState.Normal;
- Visible = true;
-
- }
-
- private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
- {
- Application.ExitThread();
- Application.Exit();
-
-
- }
-
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- Properties.Settings.Default.Port1 = Port1.Text;
- Properties.Settings.Default.Port2 = Ports2.Text;
- Properties.Settings.Default.AEtitleSCP = AEtitlescp.Text;
- Properties.Settings.Default.IPBox = IPBox.Text;
- Properties.Settings.Default.CalledAE = CalledAE.Text;
- Properties.Settings.Default.CallingAE = CallingAE.Text;
- Properties.Settings.Default.FileLocation = FileLocation.Text;
- Properties.Settings.Default.PX = this.Location.X;
- Properties.Settings.Default.PY = this.Location.Y;
- Properties.Settings.Default.Save();
-
-
-
-
-
- if (e.CloseReason == CloseReason.UserClosing)
- {
- DialogResult result = MessageBox.Show("Do you really want to exit?", "Dialog Title", MessageBoxButtons.YesNo);
- if (result == DialogResult.Yes)
- {
- Environment.Exit(0);
- }
- else
- {
- e.Cancel = true;
- }
- }
- else
- {
- e.Cancel = true;
- }
- }
-
- private void startToolStripMenuItem_Click(object sender, EventArgs e)
- {
-
-
-
-
- port = Port1.Text;
- AE = AEtitlescp.Text;
- dicompath = FileLocation.Text;
- finalpath = port + " -pm" + " --fork " + " -aet " + AE + " -od " + ((char)34) + dicompath + ((char)34);
-
- var WatchFolder = dicompath;
- FileSystemWatcher fsw = new FileSystemWatcher(WatchFolder);
- fsw.EnableRaisingEvents = true;
- fsw.IncludeSubdirectories = true;
- fsw.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite;
- fsw.Changed += new FileSystemEventHandler(fsw_Changed);
- fsw.Created += new FileSystemEventHandler(fsw_Created);
-
-
-
-
-
-
-
-
-
-
-
- Process startInfo2 = new Process();
- startInfo2.StartInfo.CreateNoWindow = true;
- startInfo2.StartInfo.UseShellExecute = false;
- startInfo2.StartInfo.RedirectStandardOutput = true;
- startInfo2.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
- startInfo2.StartInfo.FileName = @"C:\dcmtk\bin\storescp-tls.exe";
- startInfo2.StartInfo.Arguments = finalpath;
- startInfo2.StartInfo.RedirectStandardOutput = true;
-
- startInfo2.Start();
- }
-
- private void stopToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Process startInfo2 = new Process();
- startInfo2.StartInfo.CreateNoWindow = true;
- startInfo2.StartInfo.UseShellExecute = false;
- startInfo2.StartInfo.RedirectStandardOutput = true;
- startInfo2.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
- startInfo2.StartInfo.FileName = @"taskkill";
- startInfo2.StartInfo.Arguments = @"/f /im storescp-tls.exe";
- startInfo2.StartInfo.RedirectStandardOutput = true;
-
- startInfo2.Start();
- System.Windows.Forms.MessageBox.Show("Server is dead Jim...");
-
- DicomMonitor DM = new DicomMonitor();
- DM.Close();
-
-
-
- }
-
- private void OKbtn_Click(object sender, EventArgs e)
- {
- this.Text = Port1.Text;
- this.Text = Ports2.Text;
- this.Text = AEtitlescp.Text;
- this.Text = IPBox.Text;
- this.Text = CalledAE.Text;
- this.Text = CallingAE.Text;
- this.Text = FileLocation.Text;
-
-
-
- this.WindowState = FormWindowState.Minimized;
- this.Visible = false;
- }
-
- private void CancelBtn_Click(object sender, EventArgs e)
- {
- this.Close();
- }
-
- private void button3_Click(object sender, EventArgs e)
- {
-
- }
-
- private void button6_Click(object sender, EventArgs e)
- {
-
- }
-
- private void AEtitlescp_TextChanged(object sender, EventArgs e)
- {
-
- }
-
- private void button1_Click(object sender, EventArgs e)
- {
- FolderBrowserDialog fbd = new FolderBrowserDialog();
- fbd.Description = "Enter Path for images...";
- if (fbd.ShowDialog() == DialogResult.OK)
- MessageBox.Show(fbd.SelectedPath);
- FileLocation.Text = fbd.SelectedPath;
- }
-
- private void textBox2_TextChanged(object sender, EventArgs e)
- {
-
- }
-
- private void groupBox1_Enter(object sender, EventArgs e)
- {
-
- }
-
- private void label3_Click(object sender, EventArgs e)
- {
-
- }
- void m_oWorker_DoWork(object sender, DoWorkEventArgs e)
- {
-
-
-
-
-
-
- for (int i=0; i < 100; i++)
- {
-
- {
- string filepath = Form1.dicompath;
- string[] files = Directory.GetFiles(filepath, "*.*", SearchOption.AllDirectories);
- for (int index = 0; index < files.Length; index++)
- foreach (string s in files)
- {
- string fileToProcess = filepath;
- m_oWorker.ReportProgress(100 * index / files.Length, fileToProcess);
-
- string port;
- port = Ports2.Text;
- string Host;
- Host = IPBox.Text;
-
- string finalpath = Host + " " + port + " " + s;
-
- System.Diagnostics.Process pr = System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo
- {
- CreateNoWindow = true,
- UseShellExecute = false,
- RedirectStandardOutput = true,
- WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
- FileName = @"C:\dcmtk\bin\storescu-tls.exe",
- Arguments = finalpath
-
- });
-
- pr.Start();
- pr.WaitForExit();
- Thread.Sleep(500);
-
-
- string SourceFolder = Form1.dicompath;
-
-
-
- var filess = new DirectoryInfo(SourceFolder).GetFiles("*.*");
-
-
- foreach (FileInfo file in filess)
- {
-
-
-
-
-
-
- System.IO.File.Delete(s);
-
-
- }
-
-
-
- m_oWorker.ReportProgress(i);
-
-
-
- if (m_oWorker.CancellationPending)
- {
- e.Cancel = true;
- m_oWorker.ReportProgress(0);
- return;
- }
-
- }
-
-
- if (!m_oWorker.IsBusy)
-
- m_oWorker.ReportProgress(100);
-
- else
- MessageBox.Show("Can't run the worker twice!");
-
- }
- }
- }
- void fsw_Changed(object sender, FileSystemEventArgs e)
-
- {
- try
- {
- while (!m_oWorker.IsBusy)
- m_oWorker.RunWorkerAsync();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
-
-
-
-
-
- }
- void fsw_Created(object sender, FileSystemEventArgs e)
- {
- try
- {
-
- m_oWorker.CancelAsync();
- m_oWorker.RunWorkerAsync();
-
- }
- catch (Exception ex)
- {
-
- }
-
- m_oWorker.CancelAsync();
- m_oWorker.RunWorkerAsync();
-
-
-
- }
- void fsw_Renamed(object sender, FileSystemEventArgs e)
- {
- try
- {
-
- m_oWorker.RunWorkerAsync();
- m_oWorker.CancelAsync();
- }
- catch (Exception ex)
- {
- Errors.Text = (ex.Source + "Has been added. Sending file now....");
- }
-
- m_oWorker.CancelAsync();
- m_oWorker.RunWorkerAsync();
-
- }
- void m_oWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
- {
-
- if (e.Cancelled)
- {
-
- }
- else if (e.Error != null)
- {
-
- }
- else
- {
- Errors.Text = "Task Completed...";
-
- }
-
-
- }
-
-
- private void Errors_Click(object sender, EventArgs e)
- {
-
- }
-
- private void tESTMEOUTToolStripMenuItem_Click(object sender, EventArgs e)
- {
- DicomMonitor F3 = new DicomMonitor();
- F3.Show();
-
-
- }
- }
- }