Re: Adding a link to StudentProjectsinContributingand ThreadsBeginnersGuide
"Luciano _" <[email protected]> Thu, 15 Mar 2007 12:07:56 +0000
| Newsgroups | gmane.comp.gnome.mono.documentation |
|---|---|
| Message-ID | <[email protected]> |
I get no password or even write access :) So, i attach a text file with the info to add to this article. Bye Luciano >From: Miguel de Icaza <[email protected]> >To: Luciano _ <[email protected]> >CC: [email protected], [email protected] >Subject: Re: [Mono-docs-list] Adding a link to >StudentProjectsinContributingand ThreadsBeginnersGuide >Date: Tue, 13 Mar 2007 12:45:30 -0400 >MIME-Version: 1.0 >Received: from erandi.dom ([130.57.22.201]) by >bay0-mc5-f11.bay0.hotmail.com with Microsoft SMTPSVC(6.0.3790.2668); Tue, >13 Mar 2007 09:46:55 -0700 >Received: by erandi.dom (Postfix, from userid 500)id A086ACDB11; Tue, 13 >Mar 2007 12:45:30 -0400 (EDT) >X-Message-Info: >LsUYwwHHNt3660MmjhEvYg2f34OAemlK+ZzoV09lDsZmbz8QigGIQtU5Yvr3lK0P >References: <[email protected]> >Organization: Novell, Inc. X-Mailer: Evolution 2.6.0 Return-Path: >[email protected] >X-OriginalArrivalTime: 13 Mar 2007 16:46:55.0972 (UTC) >FILETIME=[35BDFA40:01C7658F] > >Hey, > > > It is an excelent article. I was looking for this info in the past, and > > someone help me with this at #Irc. Could i improved a little bit? Like > > adding info about passing info to thread method? How do i do this? I >haven't > > got write access. > >Send me your full name and I can set up an account for you > _________________________________________________________________ Visita MSN Latino Noticias: Todo lo que pasa en el mundo y en tu paÃn, ¡en tu idioma! http://latino.msn.com/noticias/ _______________________________________________ Mono-docs-list maillist - [email protected] http://lists.ximian.com/mailman/listinfo/mono-docs-list
threads.txt
(text/plain, 2.8 KB)
Passing parameters to threads
The ThreadStart delegate doesn't take any parameters, so, the parameters
must be stored in somewhere else.
A good way to do this, is to create a new object which contains the async
method, and store the parameters in some of
it properties.
An example of this is:
using System;
using System.Threading;
namespace ThreadGuideSamples
{
class ThreadCaller
{
static void Main()
{
ThreadCaller myCaller = new ThreadCaller();
}
public ThreadCaller()
{
SecondThreadObject myThreadObject = new SecondThreadObject("Test
Param"); // Constructor get the parameter
myThreadObject.Parameter2 = 10; // Another value, not assigned at
constructor
Thread myThread = new Thread(new
ThreadStart(myThreadObject.MyAsyncMethod));
myThread.Start();
}
}
class SecondThreadObject
{
protected string Param1;
protected int Param2;
public SecondThreadObject(string parameter1)
{
this.Param1 = parameter1;
}
public int Parameter2
{
set
{
this.Param2 = value;
}
}
public void MyAsyncMethod()
{
Console.WriteLine("The value passed in the Constructor is '{0}'",
this.Param1);
Console.WriteLine("The value passed in the Property is '{0}'",
this.Param2);
for(int x = 0; x < 50; x++)
Console.WriteLine(x);
Console.ReadKey();
}
}
}
Accessing to main thread with callback
In some applications you need to access to the caller thread (the main
thread) [0]. In this case you can use
a callback delegate to access to it, like this:
using System;
using System.Threading;
namespace ThreadGuideSamples
{
public delegate void MyCallBack(int Response);
class ThreadCaller
{
static void Main()
{
ThreadCaller myCaller = new ThreadCaller();
}
public ThreadCaller()
{
SecondThreadObject myThreadObject = new
SecondThreadObject(this.OnResponse); // Constructor get the callback
parameter
Thread myThread = new Thread(new
ThreadStart(myThreadObject.MyAsyncMethod));
myThread.Start();
}
protected void OnResponse(int response)
{
Console.WriteLine("Value returned to main thread {0}", response);
/*
If you on Gtk# you can use like:
Gtk.Application.Invoke ( delegate {
// My Changes to the gui
});
*/
}
}
class SecondThreadObject
{
protected MyCallBack OnResponse = null;
public SecondThreadObject(MyCallBack callback)
{
this.OnResponse = callback;
}
public void MyAsyncMethod()
{
for(int x = 0; x < 50; x++)
if(this.OnResponse != null)
this.OnResponse(x); // Go To Thrad Caller
Console.ReadKey();
}
}
}
[0] Some case like Gtk# need to be accessed across his own thread, so you
need to take care of it:
http://www.mono-project.com/Responsive_Applications