Solved

Adding to a list from a static event

Posted on 2011-09-29
10
212 Views
Last Modified: 2013-12-17
(I don't often write C# code with forms so....)

I have a form that contains a listview.
I wish to add an item to the listview from a static event.
The event is the  FileSystemEventHandler(onchanged) event.
Obviously, this is staic, but how do I write to the list from a static event or method?

Many thanks i advance.
0
Comment
Question by:Spike_66
[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
  • 6
  • 3
10 Comments
 
LVL 7

Expert Comment

by:Eoin Ryan
ID: 36813833
what do you want added when the event triggers? are you reading a file folder or something? what do you want added to the list?

event happens
assign a string a value
add sting to list

something like that?
0
 

Author Comment

by:Spike_66
ID: 36813873
Yes, basically
0
 

Author Comment

by:Spike_66
ID: 36813889
I can get the string value (from the- as in e.Name)- but can't add it to the list view
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 8

Accepted Solution

by:
jagrut_patel earned 500 total points
ID: 36814175
It is not necessary to have Changed event-handler to be static.
0
 
LVL 8

Expert Comment

by:jagrut_patel
ID: 36814190
If you are referring this example then here is it static because event-handler is not accessing any instance members.
0
 

Author Comment

by:Spike_66
ID: 36814327
I'm using something similar.

However, if I remove static, although I can call a method to populate the list, but get the error
"Cross-thread operation not valid. 'lstThing' accessed from a thread other than the thread it was created on.
0
 
LVL 8

Expert Comment

by:jagrut_patel
ID: 36814449
That is a different issue. It is because UI controls have thread affinity.
See if this thread help you to understand how to access UI control from a different thread http://www.experts-exchange.com/Programming/Languages/.NET/Visual_CSharp/Q_27283181.html

You can get this error if your FileSystemWatcher is created on a different thread than the one (main thread) on which your listview is created.


0
 

Author Comment

by:Spike_66
ID: 36814500
Hi
I ony posted the error to demonstate why I could remove "static".

Back to the original issue- I can't update the listview from the FileSytemEventhandler(OnChanged).
0
 

Author Comment

by:Spike_66
ID: 36814504
sorry "could" should have read "couldn't"!
0
 

Author Comment

by:Spike_66
ID: 36814717
sorted it!

removed Static (as suggested) then just used a delegate and Invoke

delegate  void MyNewDel(string thing1);
i.e.

private vioid AddThing(string thing2)
{
if Ithis.InvokeRequired)
{
        MyNewDel nd= new MyNewDel(AddThing);
        this.Invoke(nd, new objrct[] {thing2};
}
else
{
       list1.Items.Add(thing2);
}
}
       

Thanks for pointing me in the right direction.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why use this lambda? 12 96
.NET - find out if application is running on Windows 2012 server 3 57
Data Analysis 7 59
What is needed to become a DBA? 7 56
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

738 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