Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8609931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:59:57+00:00 2026-06-12T03:59:57+00:00

I am developing a .NET 4.0 WPF application in C# that controls a motor

  • 0

I am developing a .NET 4.0 WPF application in C# that controls a motor via RS232. I am having problems when exiting my application that the application sometimes deadlocks when closing the comport.

After some research on the internet I noticed this was a common problem and that using BeginInvoke in the DataReceivedEvent or closing the serialport in a different thread should solve the problem.
The problem with those workarounds are that.
1. I dont use the DataReceiveEvent.
2. closing the serialport in another thread doesnt make any difference. What happens is that the GUI shutsdown but you can see in the TaskManager that the process is still running.

Other things I have tried is:

  • Not closing the serialport and just exiting the application. This successfully closes the application and process but the serialport is still blocked, and to unblock the serialport I need to restart the computer.
  • Sleeping a couple of seconds before and after I close the serialport.
  • Having the application be a WinForms app. instead of WPF. No difference in the deadlock between the two.

The computer I run the program on uses com ports that are mounted on the motherboard and have Microsoft drivers.

And now for some code:

Window_Closing event looks like this:

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    window.Closing -= Window_Closing;            
    Thread CloseDown = new Thread(new ThreadStart(server.Dispose)); //Closes serialport everything in another thread to avoid hang on serialport close.
    CloseDown.Start();
}

Where server is the object that manages the serialport. And Dispose calls the serialport close function.

Serialport close function:

public void Close()
{
    DebugLog.Write(3, "-->MacCommSerialPort.Close");
    _com.Close();                        
    DebugLog.Write(3, "<--MacCommSerialPort.Close");
}

Serialport settings:

_com = new SerialPort(portNumber);
_com.BaudRate = 19200;
_com.Parity = Parity.None;
_com.DataBits = 8;
_com.Encoding = Encoding.GetEncoding("Windows-1252");
_com.StopBits = StopBits.One;
_com.RtsEnable = false;
_com.DtrEnable = false;
_com.WriteTimeout = 400;
_com.ReadTimeout = 1000;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T03:59:58+00:00Added an answer on June 12, 2026 at 3:59 am

    I just happened to look at your code and guess you should use the AutoThreadRest event before closing your GUI.

    private static AutoResetEvent PortClosedEvent = new AutoResetEvent(false);
    
    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) 
        { 
            window.Closing -= Window_Closing;             
            Thread CloseDown = new Thread(new ThreadStart(server.Dispose));
            CloseDown.Start(); 
            PortClosedEvent.WaitOne(); 
        } 
    

    and inside the server.Dispose method after you are done disposing the connection add this below line of code.

    PortClosedEvent.Set();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing an application in WPF and .Net 4 which contains animations. My controls
I'm developing an application in WPF (.NET 4). I have an attached property that
I am developing a commercial VB.net WPF application that needs user generated scripts for
I'm developing a .NET 4.0 application using PRISM and MVVM, as well as WPF.
I'm developing client-server application by using .Net, C#, WCF, WPF. And now I need
I am developing a .NET application (using C#) that needs to massage some Adobe
I'm currently developing an application in WPF (.NET 4 / VS 2010) and I'm
i'm developing a WPF (.NET 3.5) application where i need to validate a textbox
I'm developing a C#/.NET application (Winforms/WPF, not entirely decided yet) which needs to display
I am developing an application in WPF in .NET 3.5. I need help on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.