Click here to Skip to main content
15,845,785 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi guys,

Im making an application sending sms via 3g modem using the gsmcomm library.

After i connect to the port via clicking Connect button , the code below is executed.Then when I close the form and when i try to connect again to the same port..It says "The process cant access the file..because it is being used by another process " ..and it works behind Task manager..if i say "drop the task"..there is no prob as well

But if i close the form by clicking disconnect button..there is no problem after i reopen form and reconnect..

What can i do it ?

For closing event , i wrote port.Disconnect(); but doesnt execute..
C#
int port = int.Parse(drpPort.Text);
int baud=int.Parse(drpBaudRate.Text);
int timeout = int.Parse(drpTimeOut.Text);

 comm = new GsmCommMain(port, baud,timeout);
 comm.Open();
 if (comm.IsOpen())
 {
     btnBaglan.Enabled = false;
     btniptal.Enabled = true;
     MessageBox.Show("Porta başarılı bir şekilde bağlandınız!!", "Dikkat!", MessageBoxButtons.OK, MessageBoxIcon.Information);
     lblBeklemede.Text = "Porta Bağlandı";
     bilgiler();

 }
 else
 {
     comm.Open();
 }
Posted
Updated 19-Sep-12 10:30am
v4
Comments
[no name] 19-Sep-12 15:24pm    
Try closing the port before you exit your application.
beratxt 19-Sep-12 15:34pm    
for closing event , i tried..but doesnt work..what else can be done ?
[no name] 19-Sep-12 15:36pm    
"doesn't work" is completely meaningless.

1 solution

Because you open the port and do not close it, the port remains open and in use until teh Garbage Collector comes along to free up the memory and Disposes of it. I would suggest that if possible you use a using block to construct your port instance, or ensure that you call Close and Dispose on it when you have finished with it.

It is worth getting into the habit of doing this for all objects that implement IDisposable - at some point if you don't, it will come back to bite you, as it does here.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900