all groups > dotnet clr > september 2006 >
You're in the

dotnet clr

group:

Using BackgroundWorker in a browser's hosted user control


Using BackgroundWorker in a browser's hosted user control Carlo Folini
9/26/2006 2:36:02 AM
dotnet clr:
I have a user control written in c# 2.0 hosted in ie6.
I'm doing some async stuff with BackgroundWorker.

I see that the RunWorkerCompleted method is executed in a thread different
from the UI one.
The same user control if hosted in a winform app works correctly.
Also if the RunWorkerAsync is called in the user control's constructor I got
the correct behavior.
The SincronizationContext is null in the RunWorkerCompleted when the
RunWorkerAsync is called outside the constructor.

Any idea on why this appens and how to solve it?

--
RE: Using BackgroundWorker in a browser's hosted user control v-lliu NO[at]SPAM online.microsoft.com
9/27/2006 12:00:00 AM
Hi Carlo,

I performed a test on this issue. The following is the walkthrough.

1. Create a Windows Control Library project named
'BackgroundWorkerInUserControl' .

2. Copy the code in the MSDN sample for BackgroundWorker component (you may
get this sample at
http://msdn2.microsoft.com/en-us/library/system.componentmodel.backgroundwor
ker.aspx ) into the UserControl1.

3. Build the project.

4. Create a folder, e.g 'c:\virtual path\backgroundworker' and copy the
project output file BackgroundWorkerInUserControl.dll into the folder.
Create an html file named test.html. The content in this html file is like
below.

<html>
<head>
<title>IE Hosted UserControl</title>
</head>
<body>
<object id="myControl" name="myControl" width="400" height="400"
classid="BackgroundWorkerInUserControl.dll#BackgroundWorkerInUserControl.Use
rControl1">
</object>
</body>
</html>

5. Open IIS Manager and create a new virtual directory named
'backgroundworker' which refers to the directory 'c:\virtual
path\backgroundworker'. Ensure to set the 'Execute permissions' of the
virtual directory to 'Scripts only'. To do this, right-click on the virtual
directory and choose Properties. In the Properties window, switch to
'Virtual Directory' tab. Select 'Scripts only' in the 'Execute permissions'
combobox. This ensures the usercontrol hosted in the web page to be loaded
correctly.

6. Open IE and type the url 'http://localhost/backgroundworker/test.html'
in the address bar and press Enter. The usercontrol is loaded in the web
page correctly. When I set the value in the numericUpDown control to 35 and
click the 'Start Async' button, the usercontrol begins to compute and the
progress of the process is displayed in the progressbar. All works fine.

Is there any difference between your walkthrough and mine?

[quoted text, click to view]
RunWorkerAsync is called outside the constructor.

Could you tell me what the 'SincronizationContext' is? What problem do you
have when you call the RunWorkerAsync outside the constructor?

You may perform a test based on the MSDN sample I mention above and see if
the problem exists in the sample. If the problem is still not solved in
your project, you may send me your sample project and html page hosting the
usercontrol. To get my actual email address, remove 'online' from my
displayed email address.

I look forward to your reply.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
RE: Using BackgroundWorker in a browser's hosted user control v-lliu NO[at]SPAM online.microsoft.com
10/4/2006 12:01:18 PM
Hi Carlo,

Thanks for your detailed explaination and sample project.

I have done more research on this issue and did reproduce the problem you
described. When the user control is used in a Windows application, the
ProgressChanged and RunWorkerComplete event handlers are executed by the UI
thread.

However, when the user control is hosted in IE, the ProgressChanged and
RunWorkerComplete event handlers seem to be executed by the background work
thread which executes the DoWork event handler.

I think this issue is probably related to the thread model in IE. Although
the above information about thread seems incorrect, the BackgroundWorker
works properly when hosted in IE.

I am consulting this issue in our discussion group. If there is any
message, I will get it back to you ASAP.



Sincerely,
Linda Liu
Microsoft Online Community Support
RE: Using BackgroundWorker in a browser's hosted user control v-lliu NO[at]SPAM online.microsoft.com
10/6/2006 9:43:36 AM
Hi Carlo,

BackgroundWorker relies on AsyncOperationManager, which is designed to have
the SynchronizationContext installed and managed by the host application.
When the backgroundworker is used in a Windows application, the
WindowsFormsSynchronizationContext is installed.

However, web applications doesn't have SynchronizationContext installed,
which causes a different behavior from the Windows application. If you want
the Windows forms behavior in web application, one way you could get it
would be

AsyncOperationManager.SynchronizationContext = new
WindowsFormsSynchronizationContext();

before calling BackgroundWorker.RunWorkerAsync.

Hope this helps.
If you have any concerns, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support
RE: Using BackgroundWorker in a browser's hosted user control Carlo Folini
10/9/2006 12:16:02 AM
Hi Linda,
tried your suggestion and it works!

Thanks for your help
Ciao
--
Carlo Folini


[quoted text, click to view]
AddThis Social Bookmark Button