Oscarfh

Hi,

I am developing an app and i have to pass a reference to a grid and a string to a method that should be done asynchronously. Is there a way I can do it Or is wrong doing it and I shouldn't be passing these values

Thanks

Oscar



Re: Visual C# General Threads with more than 1 argument

Matthew Watson

There are several ways of doing that.

However, I'm going to assume that you want to launch a worker thread from your main user interface thread, and then have it (optionally) alert the main user interface thread about its progress, and alert the main user interface thread when it has completed.

If that is the case, then you should drop a BackgroundWorker component on the form that wants to launch the worker thread, and use the BackgroundWorker to manage the worker thread.

See this Microsoft article for more details.





Re: Visual C# General Threads with more than 1 argument

Oscarfh

Thanks, that is exactaly what I was looking for.

But can you help me a little more, please

I was using Threads to do it, so I had this code to allow my threads to alter a component in the GUI. But now I don't knkow how to fix this code to work.

Can you help me avoiding the "Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on." exception please

Thanks, Oscar

if (this.dataGridViewEntradas.InvokeRequired)

{

ParameterizedThreadStart parametrized = new ParameterizedThreadStart(InsereDados);

this.Invoke(parametrized, obj);

}

else

{

//work here. . . .

}





Re: Visual C# General Threads with more than 1 argument

Matthew Watson

Well, if you use the BackgroundWorker component, you don't have to worry about it, because it does it all for you.

There are two events that you would be interested in: ProgressChanged and RunWorkerCompleted.
There is one more, DoWork, which is the one in which you actually do all the asynchronous work.

So I highly recommend that you change your code to use BackgroundWorker...

However, for the sake of interest, here is a cut and paste of my own notes on using InvokeRequired.
(It looks like you've more or less sussed it out already)

-------------------

Handling InvokeRequired

To handle calling methods of a control from a thread other than the thread that owns the control (which is usually the main UI thread) you must check the InvokeRequired property, and transition to the correct thread using Invoke() via a delegate.

You commonly want to do this when handling an asynchronous event. You want to turn the asynchronous call into a synchronous one.

To do that:

(1) Declare a delegate with exactly the same signature as the method that wants to transition to the correct thread.
(2) In the transitioning function, check ¡°this.InvokeRequired¡±:
(3) If it is true, you have to create an object array containing all the method¡¯s arguments, and then call this.Invoke() passing an appropriate delegate and argument list.
(4) If it is false, do normal processing, since we¡¯re already in the correct thread.

An example will make this clear:

This is the event handling function:



private void file_watcher_Renamed( object sender, RenamedEventArgs e )
{
if ( this.InvokeRequired )
{
object[] pList = { this, e }; // Create the parameter array.
this.Invoke( new HandleFileRenamedDelegate( file_watcher_Renamed ), pList );
}
else
{
// Normal handling.
Trace.WriteLine( "File renamed from " + e.OldName + " to " + e.Name );
}
}



This is the delegate that you create solely for calling back using Invoke():

private delegate void HandleFileRenamedDelegate( object sender, RenamedEventArgs e );





Re: Visual C# General Threads with more than 1 argument

Oscarfh

Thanks a lot :)

everything is working fine now :)

Oscar