BackgroundWorker - have no idea

P

Piotrekk

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(sfName.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();
}
}
 
D

David

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top