0

I have a winform application developed in 3.5 framework. Intermittently, the buttons in the application becomes unresponsive. The application never shows a "Not Responding" in the title, but it's just the buttons. Even if I put a break-point in the button click code, the control never comes there when the button is clicked!!!

What could be the reason?

Sandeep
  • 5,581
  • 10
  • 42
  • 62
  • 2
    Are you updating data in he form or waiting for database responses? Could you show us some code to reproduce the problem? Thanks. – Jonathan Feb 06 '12 at 09:09

2 Answers2

0

Probably the reason is that you are doing something in the UI thread (accessing the database, connecting to the internet, reading files, etc) so it is busy.

Here is the solution:

  • Never have business logic in your code-behind files (say, never have it in your Button_Click methods, OnLoad methods, etc).
  • Use MVC/MVP patterns to separate your business logic from the UI.
  • Always perform your business logic asynchronously (worker threads, tasks, etc), it is trivial when you have your business logic separated.
Alexey Raga
  • 7,457
  • 1
  • 31
  • 40
  • 2
    @Jeremy Thompson: Ofcourse MVC, MVP is possible in Winforms too, though not quite well-developed and no singelton documentation is found, it is recommended. In crude language, you can separate your business logic and application logic. Kindly refer: 1. http://stackoverflow.com/questions/654722/implementing-mvc-with-windows-forms 2. http://social.msdn.microsoft.com/Forums/en-NZ/architecturegeneral/thread/904d4399-5ba6-4eee-b76f-6055a2dcb8e0 – Marshal Feb 07 '12 at 04:35
  • 1
    @JeremyThompson Oh, why ppl think that "code behind", "MVC", "MVP" are web terms? Just because of ASP.NET MVC?! C'mon guys, be serious. MVC/MVP are design patterns that are used everywhere and were used even before ASP.NET MVC was invented :) They are patterns you use to separate BL from UI, they are completely agnostic whether it is web or winforms or even console. That's the idea: your View can be anything. MS P&P division used to support Composite Application Block that recommended and used MVP in this framework... – Alexey Raga Feb 07 '12 at 09:54
  • @AlexeyRaga - I know perfectly well the concepts behind MVC, and MVP but afaik there isn't a WinForm MVC/MVP framework as well developed as the ones for web-dev such as MVC3 or WebFormsMVP.com. There are many patterns to separate BL from UI and DAL. Even the SO link Marshal pointed to shows how reluctant dev's are to use MVC in Winforms. Agree to disagree. With Code-Behind, it is a Web term. You can have code in the aspx <% %> as well as the .cs but there is no equivalent to that in WinForms. Sometimes it isn't better to perform your business logic asynchronously - eg field validation – Jeremy Thompson Feb 07 '12 at 21:47
  • Though Jeremy did not get what Alexy simply meant, he has a point when saying "do no access business logic asynchronously". Async database operation creates scope for lot of bugs. Have to be careful – nawfal Feb 07 '12 at 22:06
  • meh - this is getting off track for a question about missing Button event handler, no need to continue, Alexey probably lives round the corner from me - we'll chat about it over a beer! – Jeremy Thompson Feb 07 '12 at 22:55
0

Until you fix your accept rate, the most likely answer is that the Button Click event has lost it Handles clause. In Vb.Net its really easy to see the Handles xyz event:

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click

In c# you have to drill into the InitializeComponent (F12) call in your form's Constructor. Then check the button your clicking has the event assigned:

this.Button1.Click += new System.EventHandler(this.Button1_Click);
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321