Click here to Skip to main content
15,904,348 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to pass one arraylist 'searchtext' and another arraylist of arraylists str[] to the backgroundworker1_DoWork and then perform operations on them. Please help. Stuff on other websites were very unclear. A link to a webpage explaining this concept in a simple fashion is also appreciated.
Posted
Updated 16-Oct-14 8:49am
v3
Comments
Kornfeld Eliyahu Peter 16-Oct-14 14:21pm    
http://mattgemmell.com/what-have-you-tried/
Tejas Shastri 16-Oct-14 14:48pm    
Haha... I'm sorry.. I expected more similar replies though.. I haven't tried because I have no clue as to how.. I would appreciate a link which deals with this concept.. Thank you..
BillWoodruff 16-Oct-14 14:36pm    
Why are you using an ArrayList instead of using strongly typed generic Lists ?
Tejas Shastri 16-Oct-14 14:44pm    
I'm a newbie.. I'm using stuff I'm comfortable with.. Lot to learn..
BillWoodruff 16-Oct-14 14:51pm    
It's very easy to use Generic Lists like List<string> and your code will be much faster than using ArrayLists. If you were my student, I would expect you to understand generic lists before I would expect you understood using BackGroundWorker :)

ArrayLists are an outmoded object Type from the early versions of .NET. If you need to pass a Collection that contains mixed Types of objects, the typical solution is to define a Class or a Struct and pass a collection of those.

It's quite simple:
  • Pass the state object to the RunWorkerAsync method;
  • In the DoWork event handler, you are passed an instance of the DoWorkEventArgs class. Whatever object you passed as the state to the RunWorkerAsync method is available from the Argument property.
  • Since the Argument property is an object, you'll need to cast it to the type you passed in.


Example:
C#
private sealed class BackgroundWorkState
{
    public List<string> List1 { get; set; }
    public List<MyClass> List2 { get; set; }
    public string SomethingElse { get; set; }
}

private void StartBackgroundWork()
{
    BackgroundWorkState workerState= new BackgroundWorkState
    {
        List1 = ...,
        List2 = ...,
        SomethingElse = ...,
    };
    
    backgroundWorker.RunWorkerAsync(workerState);
}

private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
    BackgroundWorkState myState = (BackgroundWorkState)e.Argument;
    // The myState variable is now the same object you passed to the RunWorkerAsync method.
    ...
}
 
Share this answer
 
v2
Comments
Tejas Shastri 16-Oct-14 14:40pm    
Please check the updated question. Really sorry for the delayed update..
Richard Deeming 16-Oct-14 14:42pm    
As OG said, create a helper class for the state.
Tejas Shastri 16-Oct-14 14:54pm    
I am not familiar with the generic List concept yet and I have already used the ArrayList throughout my program.. does the above prog apply to ArrayList too?
Richard Deeming 16-Oct-14 14:55pm    
Yes, you can use any type you like.
You pass it to the DoWorkAsync method, and retrieve it from the eventargs:
C#
    List<string> list = new List<string>(){"hello", "Goodbye"};
    BackgroundWorker worker = new BackgroundWorker();
    worker.DoWork += new DoWorkEventHandler(worker_DoWork);
    worker.RunWorkerAsync(list);
    ...
    }

void worker_DoWork(object sender, DoWorkEventArgs e)
    {
    object o = e.Argument;
    List<string> list = o as List<string>;
    if (list != null)
        {
        ...
        }
 
Share this answer
 
Comments
Tejas Shastri 16-Oct-14 14:27pm    
I have to pass one array list and another array list of array lists(ArrayList[] str= new arraylist[];) kind
OriginalGriff 16-Oct-14 14:39pm    
No, you can only pass one object to the RunWorkerAsync method: so create a "helper class" which contains the info the worker is to process and pass an instance of that instead. (It also saves on casting in the DoWork handler)
Instead of using BackgroundWorker, you can create a thread explicitly (using a constructor) and reuse it as needed.

The problem if data exchange with other thread is solved nicely using the conception of thread wrapper I introduced:
How to pass ref parameter to the thread[^],
Change parameters of thread (producer) after it is started[^],
MultiThreading in C#[^].

This way, you can hide thread synchronization detail inside a wrapper; beside, you pass its instance "this" at initialization.

This is how you can reuse the thread of the wrapper:
Making Code Thread Safe[^],
Running exactly one job/thread/process in webservice that will never get terminated (asp.net)[^].

(You can find some related ideas in my past answers referenced above.)

—SA
 
Share this answer
 

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