471,350 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

BackgroundWorker - have no idea

Hi
I'am trying to force my background worker to finish, but i can't - its
still visible in windows processes.
have no idea why it's still there when file is sent.
Code is clear and visible so i wish someone could help me.
Thanks
PK

Here is the code:

private void backgroundWorker1_DoWork(object sender,
DoWorkEventArgs e)
{
String location = comboBox1.Text;

IPAddress newAddress = IPAddress.Parse(location);
byte[] fName =
ASCIIEncoding.ASCII.GetBytes(sfName.Substring(sfNa me.LastIndexOf('\\')
+ 1) + '\0');
FileStream file = null;

TcpClient Client = new TcpClient();
try
{
Client.Connect(newAddress, 5000);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Sender Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}

try
{
if (Client.Connected)
{
this.Text = "Sending File";
String line = null;
file = new FileStream(sfName, FileMode.Open);
fileLength = file.Length;
Client.Client.Send(fName, 0, fName.Length,
SocketFlags.None);

int BytesCount = 0;
mylegth = fileLength / 100;
long i = 0;
this.smoothprogressBar1.Value = 0;
byte[] buffer = new byte[64];

BytesCount = file.Read(buffer, 0, 64);
while ((BytesCount != 0))
{
i += Client.Client.Send(buffer, BytesCount,
SocketFlags.None);
this.smoothprogressBar1.Value = (int)((i * 100)
/ fileLength);
BytesCount = file.Read(buffer, 0, 64);
}
file.Close();
Client.Close();
this.Dispose();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Sender Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
file.Close();
Client.Close();
}
}

Dec 18 '05 #1
1 3575
Not being a great expert on this you may want to confirm but I've had
similar concerns with running Excel from .NET code. I think the system
wants to decide when to terminate the process. I'd start Excel and try to
shut it down when finished, Excel and try to shut it down when finished,
over and over. The system would have 5 or so Excel processes running before
it decided to terminate one. Similar things happen with memory management.

David
"Piotrekk" <Pi*************@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
Hi
I'am trying to force my background worker to finish, but i can't - its
still visible in windows processes.
have no idea why it's still there when file is sent.
Code is clear and visible so i wish someone could help me.
Thanks
PK

Here is the code:

private void backgroundWorker1_DoWork(object sender,
DoWorkEventArgs e)
{
String location = comboBox1.Text;

IPAddress newAddress = IPAddress.Parse(location);
byte[] fName =
ASCIIEncoding.ASCII.GetBytes(sfName.Substring(sfNa me.LastIndexOf('\\')
+ 1) + '\0');
FileStream file = null;

TcpClient Client = new TcpClient();
try
{
Client.Connect(newAddress, 5000);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Sender Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}

try
{
if (Client.Connected)
{
this.Text = "Sending File";
String line = null;
file = new FileStream(sfName, FileMode.Open);
fileLength = file.Length;
Client.Client.Send(fName, 0, fName.Length,
SocketFlags.None);

int BytesCount = 0;
mylegth = fileLength / 100;
long i = 0;
this.smoothprogressBar1.Value = 0;
byte[] buffer = new byte[64];

BytesCount = file.Read(buffer, 0, 64);
while ((BytesCount != 0))
{
i += Client.Client.Send(buffer, BytesCount,
SocketFlags.None);
this.smoothprogressBar1.Value = (int)((i * 100)
/ fileLength);
BytesCount = file.Read(buffer, 0, 64);
}
file.Close();
Client.Close();
this.Dispose();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Sender Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
file.Close();
Client.Close();
}
}

Dec 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by =?Utf-8?B?VE9NX1Bhc2FkZW5h?= | last post: by
8 posts views Thread by =?Utf-8?B?cmFuZHkxMjAw?= | last post: by
7 posts views Thread by =?Utf-8?B?SGVucnk5OQ==?= | last post: by
3 posts views Thread by Piotrekk | last post: by
1 post views Thread by =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.