473,385 Members | 1,317 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to invoke a delegate from a sub class

Hi all
I have a limited understanding of delegates, so bear with me.
In my main Form, I have a listview control.
I also have a class inside the main namespace in which I perform actions on the listview (such as removing a row).
Problem is, the actions the class performs are time consuming, so in order for the Form to avoid freezing, I am executing the class's methods in a separate thread using a 'backGroundWorker'.
Here is where I get stuck. I now can't do anything with the listview because I get this exception:
"Cross-thread operation not valid: Control 'listOfViews' accessed from a thread other than the thread it was created on."
So, I figure that delegates are the answer. I know how to do it in the same class, but I can't figure out how to do it when the actions are performed in a different class.
I would very much appreciate any help !!

Thanks in advance.
Feb 1 '09 #1
2 1904
Bassem
344 100+
Hi,
Delegete is most likely to function pointer, the main difference that it's safe code. Use delegetes to pass methods with a certian signature as a parameter.
Expand|Select|Wrap|Line Numbers
  1. namespace N1
  2. {
  3.     class C
  4.     {
  5.         public delegete void F ( int a1, string a2);
  6.         public static void f1 ( int a, string b)
  7.         { }
  8.         public static void f2 ( int x, string y)
  9.         { }
  10.     }
  11.     class B : C
  12.     {
  13.         public void Any ( F f)
  14.         {
  15.             f ( 1, "h" );
  16.        }
  17.     }
  18.     class D
  19.     {
  20.         B b = new B ( );
  21.         b.Any ( C.f1 );
  22.         b.Any ( C.f2 );
  23.     }
  24. }
  25.  
Only you've to be within the declaration space of both Delegete object and the method you pass it.

Thanks,
Bassem
Feb 1 '09 #2
mldisibio
190 Expert 100+
BackgroundWorker is the correct approach, but you cannot modify the ListView except from the RunWorkerCompleted event. If you modify the ListView inside your background thread, you will receive the cross-thread error.
If you search the forum for BackgroundWorker, you will see several examples and explanations. Here is a recent post with the same issues:
http://bytes.com/topic/c-sharp/answe...-worker-thread
Feb 2 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Ben Reese | last post by:
Simplified. I have 2 forms running on seperate threads (and message queues) I am having some odd problems with with the form on the second Thread/message queue freezing and/or dyinig, seemingly at...
2
by: Tom | last post by:
Hi Everybod I want to update some controls in a form from another threads. I did it by passing the form to that thread and calling a delegate with Form1.Invoke, I want to have just one delegeate...
13
by: Christian Westerlund | last post by:
Hi! I'm trying to use P/Invoke and a Method which takes an IntPtr where I am supposed to put an address to a method which the native method will use to communicate back to me. How do I convert a...
2
by: rawCoder | last post by:
Hi I am having this InvalidOperationException with message Cannot call Invoke or InvokeAsync on a control until the window handle has been created This is raised when i try to invoke a method...
4
by: Charles Law | last post by:
Hi guys. I have two threads: a main thread and a background thread. Lots of stuff happens in the background thread that means I have to update several (lots) of controls on a form. It is...
1
by: Lore Leunoeg | last post by:
If I call a delegates BeginInvoke a new thread from the threadpool is started. But what happens if I call the Invoke method? Does this also start a new thread? Thank you sincerely Lore
9
by: Terry Olsen | last post by:
I'm running an asynchronous Socket. In the ReceiveCallback method, I need to append what is received to a textbox on the main form. I have this code: Private Sub ToChatWindow(ByVal msg As...
1
by: Steve | last post by:
I need to update my UI from a Process or worker thread. I did some readinf and basically ended up adapting an MS example to fot my needs. It all made sense until I tried it :) My process...
6
by: Dom | last post by:
I'm teaching myself about delegates and the Invoke method, and I have a few newbie questions for the gurus out there: Here are some CSharp statements: 1. public delegate void MyDelegate (int k,...
3
balabaster
by: balabaster | last post by:
I have a class that I want to make thread-safe and am investigating the ISyncronizeInvoke interface and wondering just what it will take to implement this interface. So far the basic concept of my...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.