Solved

Adding to a list from a static event

Posted on 2011-09-29
10
211 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying 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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…

749 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