Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello

When i execute the code i get a parameter mismatch exception.
How do i do it correctly? Is a C# Windows Form

C#
private void events_EntryWritten(object sender, EntryWrittenEventArgs e)
{
  if(InvokeRequired)
  {
    Invoke(new EntryWrittenEventHandler(events_EntryWritten));
  }
  else
  {                
    foreach (EventLogEntry entry in events.Entries)
    {
      lvEventlog.Items.Add(entry.TimeWritten.ToString());
      lvEventlog.Items[lvEventlog.Items.Count].SubItems.Add(entry.Message);
    }
  }
}
Posted
Comments
Sergey Alexandrovich Kryukov 12-Aug-15 15:56pm    
In what line?
Why would you use this.events? Your event arguments have just one property, EventLogEntry Entry, you are not even using it.
One entry is added, but you add all existing entries in a list, why?
—SA

1 solution

The events_EntryWritten method expects two parameters (sender and e) when you call it using the Invoke method. However, you're not passing them with the call so instead of
C#
...
if(InvokeRequired)
{
  Invoke(new EntryWrittenEventHandler(events_EntryWritten));
}
...

try
C#
...
if (InvokeRequired) {
   Invoke(new System.Diagnostics.EntryWrittenEventHandler(events_EntryWritten), new object[] {sender, e});
} 
...

Also you probably mean to add the new entry to your list view so perhaps the remaining code should be something like
C#
...
} else {
      lvEventlog.Items.Add(e.Entry.TimeWritten.ToString());
      lvEventlog.Items[lvEventlog.Items.Count - 1].SubItems.Add(e.Entry.Message);
}
 
Share this answer
 
v4

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