Tech
Forums
Jobs
Books
Events
Videos
Conference
Annual Conference
Bcrypt
Ai Conference
Live
More
Interviews
Certification
Training
Career
Members
News
Blogs
Contribute
An Article
A Blog
A Video
An Ebook
An Interview Question
Register
Login
2
Answers
Can backgroundworker create another upon request?
Ask a question
Ask a question
Upload Source Code:
Select only zip and rar file.
Christopher Doty
7y
246
1
Reply
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();
// Setting up all Property of Notifyicon
}
void
m_oWorker_ProgressChanged(
object
sender, ProgressChangedEventArgs e)
{
//Here you play with the main UI thread
//progressBar1.Value = e.ProgressPercentage;
//lblStatus.Text = "Processing......" + progressBar1.Value.ToString() + "%";
}
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)
{
// Hide The Form when it's minimized
if
(FormWindowState.Minimized == WindowState)
Hide();
}
private
void
MyNotify_DoubleClick(
object
sender, System.EventArgs e)
{
// Show the form when Dblclicked on Notifyicon
Show();
WindowState = FormWindowState.Normal;
}
private
void
closeToolStripMenuItem_Click(
object
sender, EventArgs e)
{
// Will Close Your Application
MyNotify.Dispose();
Application.Exit();
}
private
void
restoreToolStripMenuItem_Click(
object
sender, EventArgs e)
{
//Will Restore Your Application
Show();
WindowState = FormWindowState.Normal;
}
private
void
settingsToolStripMenuItem_Click(
object
sender, EventArgs e)
{
//Settings 1
MessageBox.Show(
"Your Application Settings 1"
);
}
private
void
settings2ToolStripMenuItem_Click(
object
sender, EventArgs e)
{
//Settings 2
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)
{
//var Myform = new Settings();
//Myform.Show();
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)
{
///////////Passing values to another form//////////
//DicomMonitor DM = new DicomMonitor();
//DM.ShowDialog();
//////////SETUP VALUES//////////////////////////////
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);
//fsw.Renamed += new FileSystemEventHandler(fsw_Renamed);
/////////////////////////////////////////////////////
//System.Windows.Forms.MessageBox.Show("Server is running...");
//textBox1.Text = otherForm.TextBox1.Text;
//string port;
//Settings Settings = new Settings(Port1.Text);
////
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
;
//int seconds = await SleepAsync(2000);
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
;
//int seconds = await SleepAsync(2000);
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)
{
//NOTE : Never play with the UI thread here...
//time consuming operation
//
// for (int i = 0; i < 100; i++)
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 dicompath = @"C:\MyDir";
string
finalpath = Host +
" "
+ port +
" "
+ s;
//textBox1.Text = finalpath;
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);
//File.Move(s, @"C:\MyDels");
//Provider Source Folder Path
string
SourceFolder = Form1.dicompath;
//Provide Destination Folder path
//string DestinationFolder = @"C:\MyDels\";
var filess =
new
DirectoryInfo(SourceFolder).GetFiles(
"*.*"
);
//Loop throught files and Move to destination folder
foreach
(FileInfo file
in
filess)
{
//delete file if already exists
//Process.Start("cmd.exe", string.Format("/c del \"{0}", SourceFolder + file.Name));
//Move the file to destination folder
//file.MoveTo(DestinationFolder + file.Name);
System.IO.File.Delete(s);
//File.Delete(s);
}
//pr.WaitForExit();
//Thread.Sleep(50);
m_oWorker.ReportProgress(i);
//If cancel button was pressed while the execution is in progress
//Change the state from cancellation ---> cancel'ed
if
(m_oWorker.CancellationPending)
{
e.Cancel =
true
;
m_oWorker.ReportProgress(0);
return
;
}
}
//Report 100% completion on operation completed
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);
}
//listBox1.Text = (e.FullPath + "Has been added. Sending file now....");
}
void
fsw_Created(
object
sender, FileSystemEventArgs e)
{
try
{
//if (!m_oWorker.IsBusy)
m_oWorker.CancelAsync();
m_oWorker.RunWorkerAsync();
}
catch
(Exception ex)
{
//Errors.Text = (ex.Source + "Has been added. Sending file now....");
}
m_oWorker.CancelAsync();
m_oWorker.RunWorkerAsync();
//listBox1.Text = (e.FullPath + "Has been added. Sending file now....");
}
void
fsw_Renamed(
object
sender, FileSystemEventArgs e)
{
try
{
//if (!m_oWorker.IsBusy)
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 it was cancelled midway
if
(e.Cancelled)
{
//lblStatus.Text = "Task Cancelled.";
}
else
if
(e.Error !=
null
)
{
// lblStatus.Text = "Error while performing background operation.";
}
else
{
Errors.Text =
"Task Completed..."
;
}
//btnStartAsyncOperation.Enabled = true;
//btnCancel.Enabled = false;
}
private
void
Errors_Click(
object
sender, EventArgs e)
{
}
private
void
tESTMEOUTToolStripMenuItem_Click(
object
sender, EventArgs e)
{
DicomMonitor F3 =
new
DicomMonitor();
F3.Show();
}
}
}
Post
Reset
Cancel
Answers (
2
)
Next Recommended Forum
optimize the code
how to subtract two strings in sql server