Re: Best practice to display data received through event from external source. by Stoitcho
Stoitcho
Tue Sep 26 08:52:16 CDT 2006
Rajat,
I don't have any article, but here you start new UI thread:
Thread t = new Thread( new ThreadStart(ThreadProc));
t.SetApartmentState(ApartmentState.STA);
t.Start();
and the minmum code needed to make a thread to be a UI thread is
void ThreadProc()
{
Application.Run();
}
Depending on the COM object I guess you may need to have a hosting form.
Since win2k there is a support for message-only-windows. Such windows can be
created in WindowsForms using the NativeWindow class, but I don't think this
is going to help in your case. Anyhow if need a form you can create on
hidden form just to host your COM object
void ThreadProc()
{
Form f = new Form();
f.ShowInTaskbar = false;
f.WindowState = FormWindowState.Minimized;
Application.Run(f);
}
Here I use minimized form that doesn't show in the taskbar just because it
is easier than setting the Visible property. When the form starts the
Visible is automatically set to *true* thus simple setting the property
won't work. If you do it in some other place in the form you risk the form
to show-up for a split of a second before disappear. I believe this is
easier to do hidden form.
One more thing... The thread created this way is not a background thread
that is it will keep the application alive unless something kills the
thread. The correct way of doing it is to call the form's Close method.
Other possible solution would be to make the thread a background one, but in
this case it will be kill in a very "brutal" way. The form will be disposed,
but it won't return close notifications.
--
HTH
Stoitcho Goutsev (100)
"Rajat" <rajat.tandon@nirvana-sol.com> wrote in message
news:uxCwaGU4GHA.292@TK2MSFTNGP02.phx.gbl...
> Stoitcho,
>
> I got your point. I will check the comparative performance by applying the
> a thread safe queue. Also I am looking forward to
> receive the data of COM control events on separate thread at my end. If
> you have any handy article dealing with the similar
> situation (receive the data of COM control events on separate thread),
> please share.
>
> Thanks a lot for wonderful suggestions.
>
> Regards,
> Rajat.
>
> "Stoitcho Goutsev (100)" <100@100.com> wrote in message
> news:uXyPZIL4GHA.4820@TK2MSFTNGP06.phx.gbl...
>> Rajat,
>>
>>> 1) I have taken the Alternative arraylist, as the time elapses I send
>>> the "Arraylist.Values" in case of alternative arraylist approach. So
>>> here values are being passed in the event rather than the reference . At
>>> the calculation end, I reconstruct the ArrayList (using ICollection
>>> interface) and then perform the calculations. Since calculation algos
>>> are working on the copies of objects, hence even if events come faster,
>>> we are safe. In case of clone, again we have a deep copy, so working on
>>> this copy should not interfere with the original copy's
>>> processing(Receiving and adding in collection). Do you still think that
>>> applying the queue here can help us?
>>
>> What the queue was trying to solve is scenarios where calculations are
>> slower than receiving data. What will happen in your alternative array if
>> the array in the receiving part is filled out ant the processing part is
>> not done with the old data? If the reveiver keeps adding the objects to
>> the exisiting array list that will prbably solve the problem but the
>> chunks of work that the processing part must do next time will be bigger
>> and will keep growing. However the situations won't be much different
>> with the queue either - the data still needs to be processed. One
>> advantages of the queue is the it doesn't need to do deep copies of the
>> objects.
>>
>>
>>>
>>> 2) I agree that we need to shift as much job possible on new thread.
>>> Here I need to tell you that we have wired up the events on a "COM
>>> control". Through these events we are getting the external data. I have
>>> dragged the COM control on the form (Data receiver class). Thus I think,
>>> I am bounded to receive the data on the UI thread. Once I tried to send
>>> the exact received object in a new thread and fetch data in new thread
>>> itself, I got garbage data. Am I thinking here in the right direction?
>>
>> Windows forms uses Single Thread Apartment model, so yes the events will
>> come in the UI thread. What you can do is to run separate UI thread for
>> the COM control and receive the data there and then build your
>> multithread application using proper sync.
>>
>>
>> --
>> HTH
>> Stoitcho Goutsev (100)
>>
>
>