Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / October 2006

Tip: Looking for answers? Try searching our database.

Using BackgroundWorker in a browser's hosted user control

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Carlo Folini - 26 Sep 2006 10:36 GMT
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?

Signature

Carlo Folini

Linda Liu [MSFT] - 27 Sep 2006 06:14 GMT
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?

>The SincronizationContext is null in the RunWorkerCompleted when the
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.
Linda Liu [MSFT] - 04 Oct 2006 13:01 GMT
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
Linda Liu [MSFT] - 06 Oct 2006 10:43 GMT
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
Carlo Folini - 09 Oct 2006 08:16 GMT
Hi Linda,
tried your suggestion and it works!

Thanks for your help
Ciao
Signature

Carlo Folini

> Hi Carlo,
>
[quoted text clipped - 19 lines]
> Linda Liu
> Microsoft Online Community Support

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.