Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Adding to a list from a static event

Posted on 2011-09-29
10
Medium Priority
?
214 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
PowerShell Core for Advanced Linux Administrators

Understand advanced principals around Powershell Core with a focus on the Linux Administrator.  This course covers how to administer numerous environments across multiple platforms including Linux, Azure, AWS, and Google Cloud from a single shell instance.

 
LVL 8

Accepted Solution

by:
jagrut_patel earned 2000 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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

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…
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

718 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