473,657 Members | 2,504 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is running a Control in a separate thread allowed?

RWF
I have a form, and from the form when a user clicks a button, it instantiates
control that will be doing a lot of logic. I am trying to use
System.Threadin g.ThreadPool.Qu eueUserWorkItem to spawn a background thread
off the pool. I stepped through the logic, and everything seems to go
through fine when the control is initialized, and my main form continues to
work correctly. The only issue is that the control seems to not render
properly and freeze (even after its contructor has been called and completed).

Any ideas or suggestions on how to run a control in a different thread than
the main windows form?
Jun 1 '06 #1
5 2534
You don't. You have to marshal updates to the control back to the main
thread using Invoke/BeginInvoke (I would recommend BeginInvoke for most
cases)

Cheers,

Greg Young
MVP - C#
http://geekswithblogs.net/gyoung
"RWF" <RW*@discussion s.microsoft.com > wrote in message
news:18******** *************** ***********@mic rosoft.com...
I have a form, and from the form when a user clicks a button, it
instantiates
control that will be doing a lot of logic. I am trying to use
System.Threadin g.ThreadPool.Qu eueUserWorkItem to spawn a background thread
off the pool. I stepped through the logic, and everything seems to go
through fine when the control is initialized, and my main form continues
to
work correctly. The only issue is that the control seems to not render
properly and freeze (even after its contructor has been called and
completed).

Any ideas or suggestions on how to run a control in a different thread
than
the main windows form?

Jun 1 '06 #2
Hi,

"RWF" <RW*@discussion s.microsoft.com > wrote in message
news:18******** *************** ***********@mic rosoft.com...
I have a form, and from the form when a user clicks a button, it
instantiates
control that will be doing a lot of logic.
A control? You better put your logic in a separate class.
I am trying to use
System.Threadin g.ThreadPool.Qu eueUserWorkItem to spawn a background thread
off the pool. I stepped through the logic, and everything seems to go
through fine when the control is initialized, and my main form continues
to
work correctly. The only issue is that the control seems to not render
properly and freeze (even after its contructor has been called and
completed).


You cannot do that, all the UI interactions have to be done from the UI
thread.
What you can do is do your processing in a worker thread and using
Control.Invoke to send messages back to the UI thread.

--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jun 1 '06 #3
RWF,

You should not run a control in a separate thread, especially if it is
parented in a window that was created on another thread.

What you want to do is not actually try and retrieve values from your
control or set them in another thread. Store the values in variables, then
access those from another thread. When you want to update the control, you
will need to make a call to the Invoke method on the control, passing a
delegate which will set the values on your control for you. The Invoke
method will guarantee that the fields get updated correctly.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"RWF" <RW*@discussion s.microsoft.com > wrote in message
news:18******** *************** ***********@mic rosoft.com...
I have a form, and from the form when a user clicks a button, it
instantiates
control that will be doing a lot of logic. I am trying to use
System.Threadin g.ThreadPool.Qu eueUserWorkItem to spawn a background thread
off the pool. I stepped through the logic, and everything seems to go
through fine when the control is initialized, and my main form continues
to
work correctly. The only issue is that the control seems to not render
properly and freeze (even after its contructor has been called and
completed).

Any ideas or suggestions on how to run a control in a different thread
than
the main windows form?

Jun 1 '06 #4
RWF
I tried using BeginInvoke, but that seemed to still lockup my main form from
receiving an update, or even being moved around the screen.

"Greg Young" wrote:
You don't. You have to marshal updates to the control back to the main
thread using Invoke/BeginInvoke (I would recommend BeginInvoke for most
cases)

Cheers,

Greg Young
MVP - C#
http://geekswithblogs.net/gyoung
"RWF" <RW*@discussion s.microsoft.com > wrote in message
news:18******** *************** ***********@mic rosoft.com...
I have a form, and from the form when a user clicks a button, it
instantiates
control that will be doing a lot of logic. I am trying to use
System.Threadin g.ThreadPool.Qu eueUserWorkItem to spawn a background thread
off the pool. I stepped through the logic, and everything seems to go
through fine when the control is initialized, and my main form continues
to
work correctly. The only issue is that the control seems to not render
properly and freeze (even after its contructor has been called and
completed).

Any ideas or suggestions on how to run a control in a different thread
than
the main windows form?


Jun 1 '06 #5
Can you post up a simple example? btw note I am saying to not start your
other control in another thread .. I am saying to have a background threadin
your other control that then calls Control.BeginIn voke to have the main
thread update the control.

Cheers,

Greg Young
MVP - C#
http://geekswithblogs.net/gyoung
"RWF" <RW*@discussion s.microsoft.com > wrote in message
news:C7******** *************** ***********@mic rosoft.com...
I tried using BeginInvoke, but that seemed to still lockup my main form
from
receiving an update, or even being moved around the screen.

"Greg Young" wrote:
You don't. You have to marshal updates to the control back to the main
thread using Invoke/BeginInvoke (I would recommend BeginInvoke for most
cases)

Cheers,

Greg Young
MVP - C#
http://geekswithblogs.net/gyoung
"RWF" <RW*@discussion s.microsoft.com > wrote in message
news:18******** *************** ***********@mic rosoft.com...
>I have a form, and from the form when a user clicks a button, it
>instantiates
> control that will be doing a lot of logic. I am trying to use
> System.Threadin g.ThreadPool.Qu eueUserWorkItem to spawn a background
> thread
> off the pool. I stepped through the logic, and everything seems to go
> through fine when the control is initialized, and my main form
> continues
> to
> work correctly. The only issue is that the control seems to not render
> properly and freeze (even after its contructor has been called and
> completed).
>
> Any ideas or suggestions on how to run a control in a different thread
> than
> the main windows form?


Jun 1 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
2838
by: BK | last post by:
Can someone point me to a code sample that illustrates executing long running tasks (asynchronous) from a web application in ASP.NET? I assume that Web Services might come into play at some point, but I'm not sure how to get started. For example, I have an application that, upon a user initiating through a button or link click, will go out and generate a bunch of files (this could take several minutes). This will happen in batches, so I...
1
1719
by: Daniel Goldman | last post by:
I have a C program that analyzes data. It makes tables, graphs, other output in response to user selections. It normally takes keyboard input into a curses-based user interface. However, it can also run itself, forever or until it crashes. For self-run, "input" is based on random numbers. The self-run is useful for testing (and fun). I'm porting the program to C#. It will have tab controls, buttons, menus, dialog boxes, list boxes......
2
5860
by: Javier Bertran | last post by:
Hi all, I have an ActiveX control developed in Visual C++ 6.0 that I want to use in a C# project. I want the ActiveX code to run on a separate thread, but can't seem to get it to work. If for example my ActiveX member has an infinite loop, the .NET app gives all processing to the ActiveX and never returns, not even to redraw the window. Here is what I tried (in a nutshell): VC++ 6.0 OCX code: void CMyAXControlCtrl::Loop()
2
7779
by: Raed Sawalha | last post by:
Hello: I have 2 windows application 1st one is periodically writing specific data to XML file , the 2nd application has a dialog with DataGrid , the datagrid filled with information in the XML generated by 1st application and should be updated as soon as the XML file is update, so I decide to code a continously running thread that checks the XML every specific time and refresh the contents of datagrid the question : 1. How can I code a...
2
1699
by: MattMenard | last post by:
I've written a C program that I want to run inside an Gui wrapper that I started to write in MFC. I have to select some files then kick off a process running the C program. I have the initial dialog file selection stuff written, but am not sure how to run the C program inside a seperate thread. I'm using VS.NET 2003 and am trying to use the Thread and Process objects, but am not sure if I can use that with MFC. I can't compile the code...
16
2581
by: TB | last post by:
Hi all: If you think that the following comments are absolute amateurish, then please bear with me, or simply skip this thread. A couple of months back I made the decision to initiate a gradual upgrade of my web programming skills from Classic ASP / VBS to ASP.NET / VB.NET. While the study of the language differences and all the new features in .NET has so far not been a traumatic experience, I am a bit shell-schocked after
15
4752
by: Nathan | last post by:
I have an aspx page with a data grid, some textboxes, and an update button. This page also has one html input element with type=file (not inside the data grid and runat=server). The update button will verify the information that has been entered and updates the data base if the data is correct. Update will throw an exception if the data is not validate based on some given rules. I also have a custom error handling page to show the...
9
3265
by: esakal | last post by:
Hello, I'm programming an application based on CAB infrastructure in the client side (c# .net 2005) Since my application must be sequencally, i wrote all the code in the UI thread. my problem occurs when i try to show a progress bar. The screen freezes. I know i'm not the first one to ask about it. but i'm looking
12
3106
by: titan nyquist | last post by:
I have a class with data and methods that use it. Everything is contained perfectly THE PROBLEM: A separate thread has to call a method in the current instantiation of this class. There is only ever ONE instantiation of this class, and this outside method in a separate thread has to access it. How do i do this?
0
8421
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8742
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8518
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8621
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1734
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.