Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

vb.net 2008 Event loop

Posted on 2009-07-14
6
Medium Priority
?
651 Views
Last Modified: 2013-12-12
Using vb.net 2008 i noticed while debugging that an event such as a rightClickMenuItem.itemClicked runs throught the procedure code multiple times. Upwards of 30 i counted once while debugging and while running. This results in my code generating a null value when it shouldn't and ultimately crashing??  Any suggestions would be most helpful. Below is the code for rightClickMenuStrip.itemClicked.  The function getEmployeeNameScheduling returns the text of a control on a tableLayoutPanel at a given row passed in by the rightClickMenuItem Sender.sourceControl. The deleteEntryFromEmployeeTable function deletes an entry in an SQL database which tracks controls on the tableLayoutPanel.
Private Sub rightClickMenuStrip_ItemClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles rightClickMenuStrip.ItemClicked
 
        Dim count As Integer = 0
        Dim jobLabel As String = Nothing
        Dim controlStartColumn As Integer = 0
        Dim controlStartRow As Integer = 0
        Dim controlRowSpan As Integer = 0
        Dim controlColumnSpan As Integer = 0
 
        Dim sqlDeleteString As String = Nothing
        Dim employeeName As String = Nothing
        Dim entryDate As DateTime = Nothing
        Dim startLocation As Integer = Nothing
        Dim startRow As Integer = 0
 
        If e.ClickedItem.Text = "Delete" Then
                'get information need to delete entry
                startRow = tableLayout.GetRow(sender.sourceControl)
 
                employeeName = getEmployeeNameScheduling(startRow)
                entryDate = sender.sourceControl.tag
                startLocation = tableLayout.GetRow(sender.sourceControl) Mod 6
 
                deleteEntryFromEmployeeTable(employeeName, entryDate, startLocation)
 
                
 
                'delete the control from the table 
                tableLayout.Controls.Remove(sender.sourcecontrol)
        End If
End Sub

Open in new window

0
Comment
Question by:kooch83
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 8

Expert Comment

by:dericstone
ID: 24853848
It sounds like you are in a recursive situation. To debug this you will need to look at the stack and see who is calling this method, and who is calling that, etc. The other thing to look at is which line in this method is resulting in the recursive call to it. I can't see all your code, but keep in mind that modifications to a table might result in an event getting called -- if the event handler makes changes to the table, this could result in a recursive behavior.

You might be able to fix this by declaring a variable in the class like
  bool active = false;
then when you enter the method do this
  if (active)
    return;
  active = true;
and just before the method returns put
  active = false;
0
 

Author Comment

by:kooch83
ID: 24854053
To be more elaborate in helping you understand::
- my program basically is a tableLayoutPanel in which a user can drag and drop another control onto it. in this case the control is a button of the class resizable. when the button is created i pass in the rightclickmenu and the tableLayoutpanel which the button is added to. The  code i provided above is from the resizable class. The recursive part you mentioned i have pondered before.  I have even tried the bool test which you provided above before but on debugging the bool value which i declared in the resizeable class gets reset every time the itemClicked event runs. With that said the function in the above code getEmployeeNameScheduling only returns the text value associated with a name control on the tableLayoutPanel that is related to the button right clicked on. Could this be where the recursion is happening. When i call this function and use the command tableLayout.GetControlFromPosition(0,row).text. Does the compiler then fire the itemClicked event again?? Hope this helps a little
0
 
LVL 8

Expert Comment

by:dericstone
ID: 24856815
There's about a million things that could be wrong. I'm afraid you'll have to do some debugging on the code yourself. Things to check:
- Look at the stack trace when you hit a breakpoint in the method. How does it get called?
- If it were my code, I would add a print to console at every line in the method. Then analyze if any particular line is resulting in a call to the same method.
- Look for some other point of entry like, for example, is some event getting called because the previous control lost focus?

Sorry I can't help more.
0
How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

 
LVL 4

Expert Comment

by:pepepaco
ID: 24860050
try to avoid the program crash validating for null before use objects, does it crash anyway?

instead of going directly to check the Text on e.ClickedItem.Text check if e is not null and then if e.ClickedItem is not null.. etc...
Also try to suspend table layout before doing any modification on it using a code like this:

private void AddButtons()
{
   // Suspend the form layout and add two buttons.
   this.SuspendLayout();
   Button buttonOK = new Button();
   buttonOK.Location = new Point(10, 10);
   buttonOK.Size = new Size(75, 25);
   buttonOK.Text = "OK";
 
   Button buttonCancel = new Button();
   buttonCancel.Location = new Point(90, 10);
   buttonCancel.Size = new Size(75, 25);
   buttonCancel.Text = "Cancel";
 
   this.Controls.AddRange(new Control[]{buttonOK, buttonCancel});
   this.ResumeLayout();
}

Open in new window

0
 

Accepted Solution

by:
kooch83 earned 0 total points
ID: 24864474
i believe my issue stands from the fact that i am controling tablelayoutpanel operations from within the Class resizable. I moved such operations out of the class and into the main form where the tableLayoutPanel is defined. Once i did this the problem has vanished. No more error. Is it possible to create a recursive Class?? This is what appears happened.
0
 
LVL 8

Assisted Solution

by:dericstone
dericstone earned 2000 total points
ID: 24866869
A class by itself does not execute code, but a method of the class could certainly be recursive. This could happen in your case if something was changing the size, which resulted in something changing the size, etc. Glad you fixed it!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article was originally published on Monitis Blog, you can check it here . If you have responsibility for software in production, I bet you’d like to know more about it. I don’t mean that you’d like an extra peek into the bowels of the sourc…
In this article I discuss my selections of the Top Four free Outlook OST File Viewers available. Open, view and read even damaged OST files by using these tools. They all provide a clear preview of all data such as emails, notes, tasks, calendars, e…
The viewer will learn how to successfully download and install the SARDU utility on Windows 8, without downloading adware.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question