How does this help? There is only one instance of the DataAccess Class and
it is being used by multiple instances of 1 to N types of Forms. Each
"Kevin Spencer" <kevin@DIESPAMMERSDIEtakempis.com> wrote in message
news:e2mUGTaWGHA.2064@TK2MSFTNGP03.phx.gbl...
> You have a couple of options. You can pass the data back with the
> EventArgs, or you can store the data in the class and have the form fetch
> it when it's ready.
>
> --
> HTH,
>
> Kevin Spencer
> Microsoft MVP
> Professional Numbskull
>
> Show me your certification without works,
> and I'll show my certification
> *by* my works.
>
> "Techno_Dex" <nospamchurst@osi-corp.com> wrote in message
> news:uwM3vPZWGHA.1220@TK2MSFTNGP02.phx.gbl...
>> This brings me back full circle to my original problem. Say I do spawn a
>> new thread in the DataAccess.GetMyCustomerData() method which queries the
>> database and gets the results back and raises an event. There is no way
>> for me (that I can think of, thus this posting) to determine which Form
>> was the calling form and should get the results. The use could have two
>> instances of Form1 open (fForm1_1 and fForm1_2). Each form is dealing
>> with a different Client's data (fForm1_1 -> ClientID = 1000, fForm1_2 ->
>> ClientID = 2000). If the users executes GetMyCustomerData from both
>> Form1 instances back to back (say it take 5 minutes to query client data,
>> the user starts one process then starts the second process). Both
>> fForm1_1 and fForm1_2 will be listening for the
>> GetMyCustomerDataCompleted Event. They will both get data from ClientID
>> 1000 then both get the data for ClientID 2000.
>>
>> //MDI Parent
>> Form1 fForm1_1 = new Form1();
>> Form1 fForm1_2 = new Form1();
>>
>> fForm1_1.Show();
>> fForm1_2.Show();
>>
>>
>> //fForm1_1 instance
>> private button1_Click(object sender, EventArgs e)
>> {
>> Controller.DataAccess.GetCustomerDataCompleted += new
>> EventHandler(DataAccess_GetCustomerDataCompleted)
>> Controller.DataAccess.GetCustomerData(1000);
>> }
>>
>>
>> //fForm1_2 instance
>> private button1_Click(object sender, EventArgs e)
>> {
>> Controller.DataAccess.GetCustomerDataCompleted += new
>> EventHandler(DataAccess_GetCustomerDataCompleted)
>> Controller.DataAccess.GetCustomerData(2000);
>> }
>>
>>
>>
>> "Kevin Spencer" <kevin@DIESPAMMERSDIEtakempis.com> wrote in message
>> news:eCJ$sjYWGHA.3684@TK2MSFTNGP05.phx.gbl...
>>> The *method* is in the DataAccess class. *How* it is executed is
>>> determined by the client using it. The client may or may not want to use
>>> a separate thread to execute the method. However, if you wish, you can
>>> simply spawn another Thread in the DataAccess class that performs the
>>> operation. There is no need to use a BackgroundWorker component to do
>>> this. The class can then raise an event when the Thread has finished
>>> executing. However, it should be noted that a Windows Form is STA
>>> threaded, and you will still have to deal with the threading problem in
>>> the client Form.
>>>
>>> --
>>> HTH,
>>>
>>> Kevin Spencer
>>> Microsoft MVP
>>> Professional Numbskull
>>>
>>> Show me your certification without works,
>>> and I'll show my certification
>>> *by* my works.
>>>
>>> "Techno_Dex" <nospamchurst@osi-corp.com> wrote in message
>>> news:OAWKf$XWGHA.3328@TK2MSFTNGP02.phx.gbl...
>>>>I came to a similar conclusion, but am not very happy with the solution.
>>>>I would rather see the threading be handled inside of the DataAccess
>>>>class in order to hide the details and minimize the amount of code that
>>>>is rewritten in every form that uses the same DataAccess methods....
>>>>Any other suggestions?
>>>>
>>>>
>>>> "Kevin Spencer" <kevin@DIESPAMMERSDIEtakempis.com> wrote in message
>>>> news:%23UAf28WWGHA.4292@TK2MSFTNGP04.phx.gbl...
>>>>> Well, your BackgroundWorker should be a member of the form. It can
>>>>> execute the method in the Controller Class. The DoWork Event Handler
>>>>> receives an instance of DoWorkEventArgs, which can have any object
>>>>> passed to it as an argument. It also has a member called "result," to
>>>>> which the return value of a function can be passed. The "result"
>>>>> member is then accessed in the RunWorkerCompleted event handler, which
>>>>> operates in the main thread of the Form, and can then do anything with
>>>>> it.
>>>>>
>>>>> Here's an example from MSDN2:
>>>>>
>>>>>
http://msdn2.microsoft.com/en-us/library/hybbz6ke(VS.80).aspx
>>>>>
>>>>> In your case, you would do something like the following:
>>>>>
>>>>> public class Form1
>>>>> {
>>>>> private System.ComponentModel.BackgroundWorker backgroundWorker1;
>>>>> private DataSet dsDataSet;
>>>>>
>>>>> public Form1()
>>>>> {
>>>>> this.backgroundWorker1 = new
>>>>> System.ComponentModel.BackgroundWorker();
>>>>> this.backgroundWorker1.DoWork += new
>>>>> System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork);
>>>>> this.backgroundWorker1.RunWorkerCompleted += new
>>>>> System.ComponentModel.RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted);
>>>>> }
>>>>>
>>>>> private button1_Click(object sender, EventArgs e)
>>>>> {
>>>>> backgroundWorker1.RunWorkerAsync();
>>>>> }
>>>>>
>>>>> private void backgroundWorker1_DoWork(object sender,
>>>>> DoWorkEventArgs e)
>>>>> {
>>>>> e.Result = Controller.DataAccess.GetMyCustomerData(12345);
>>>>> }
>>>>>
>>>>> private void backgroundWorker1_RunWorkerCompleted(object sender,
>>>>> RunWorkerCompletedEventArgs e)
>>>>> {
>>>>> dsDataSet = (DataSet)e.Result;
>>>>> }
>>>>>
>>>>> }
>>>>>
>>>>> --
>>>>> HTH,
>>>>>
>>>>> Kevin Spencer
>>>>> Microsoft MVP
>>>>> Professional Numbskull
>>>>>
>>>>> Show me your certification without works,
>>>>> and I'll show my certification
>>>>> *by* my works.
>>>>>
>>>>> "msnews.microsoft.com" <Techno_Dex@aol.com> wrote in message
>>>>> news:%23KHgzIRWGHA.4292@TK2MSFTNGP04.phx.gbl...
>>>>>>I might not have explained myself well enough. Here is some sample
>>>>>>code. My BackgroundWorker is not in the Form
>>>>>>
>>>>>> //Controller.cs
>>>>>> public class Controller
>>>>>> {
>>>>>> private DataAccess _DataAccess;
>>>>>> public static DataAccess DataAccess
>>>>>> {
>>>>>> get
>>>>>> {
>>>>>> if (_DataAccess == null)
>>>>>> {
>>>>>> _DataAccess = new DataAccess();
>>>>>> }
>>>>>> return (_DataAccess);
>>>>>> }
>>>>>> }
>>>>>> }
>>>>>>
>>>>>> //DataAccess.cs
>>>>>> public class DataAccess
>>>>>> {
>>>>>> private DataSet _DataSet;
>>>>>>
>>>>>> public DataSet GetMyCustomerData(int ClientID)