473,787 Members | 2,989 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

launching events from a thread

Hello all,

I've been searching for a solution on google for a problem related to
creating events from a worker thread, with no luck..

Basically, the problem is when my events are caught by a UI using my
class, they get the usual "Control 'blah' accessed from a thread other
than the thread it was created on.". I understand that of course, but
my class is to be distributed to people who don't want to have to worry
about invoking delegates in the event handler, I want to keep it all
neat and tidy inside the class and they can do whatever they want in
the events I throw. Is there a way to launch my event on the thread the
class was created on, instead of my worker thread? I've tried using a
delegate thinking that would work, but it doesn't. I've also tried
inheriting from Control and using Invoke, which doesn't seem to work as
well.

Is there something I'm missing to easily accomplish this within my
class?

<?
.....
public myclass()
{
readThread = new Thread(new ThreadStart(Thr eadLoop));
}

public void ThreadLoop()
{
while(some waithandle)
{
if(something)
if(myevent!=nul l) myevent(this, new args(1,2,3,4));
// instead of calling the above event here, I need to trigger
something
// in the thread the class was created on (UI Thread).
}
}
.....
?>

Thanks!

Michael Brown
360 Replays Ltd.

Jun 14 '06 #1
2 1683
MIke Brown wrote:
Hello all,

I've been searching for a solution on google for a problem related to
creating events from a worker thread, with no luck..

Basically, the problem is when my events are caught by a UI using my
class, they get the usual "Control 'blah' accessed from a thread other
than the thread it was created on.". I understand that of course, but
my class is to be distributed to people who don't want to have to worry
about invoking delegates in the event handler, I want to keep it all
neat and tidy inside the class and they can do whatever they want in
the events I throw. Is there a way to launch my event on the thread the
class was created on, instead of my worker thread? I've tried using a
delegate thinking that would work, but it doesn't. I've also tried
inheriting from Control and using Invoke, which doesn't seem to work as
well.

Is there something I'm missing to easily accomplish this within my
class?

<?
....
public myclass()
{
readThread = new Thread(new ThreadStart(Thr eadLoop));
}

public void ThreadLoop()
{
while(some waithandle)
{
if(something)
if(myevent!=nul l) myevent(this, new args(1,2,3,4));
// instead of calling the above event here, I need to trigger
something
// in the thread the class was created on (UI Thread).
}
}
....
?>

Thanks!

Michael Brown
360 Replays Ltd.


Hi,

You need to have access to one of your UI Controls inside your thread.
This enables you to invoke your delegate in the UI thread. I can think
of 2 ways to do this:

(1)
In my applications I usually have some sort of service for this, which
is initialized at startup:

using System;
using System.Threadin g;
using System.Windows. Forms;

namespace ConsoleApplicat ion
{
static class ApplicationHost
{
public static void Invoke(Delegate method, params object[] args)
{
aControlInTheUi Thread.Invoke(m ethod, args);
}
public static void BeginInvoke(Del egate method,
params object[] args)
{
aControlInTheUi Thread.BeginInv oke(method, args);
}
// initialiaized at startup
private static Control aControlInTheUi Thread;
}
class Program
{

static void Main()
{
ThreadHelper helper = new ThreadHelper();
Thread readThread =
new Thread(new ThreadStart(hel per.ThreadLoop) );
}
}

class ThreadHelper
{
private bool something = false;
public EventHandler SomeEvent;
public void ThreadLoop()
{
while(true)
{
if(something)
{
ApplicationHost .Invoke(
new RaiseSomeEventI nUiThreadHandle r(
RaiseSomeEventI nUiThread)
;
}
}
}
private delegate void RaiseSomeEventI nUiThreadHandle r();
private void RaiseSomeEventI nUiThread()
{
if (SomeEvent != null)
SomeEvent(this, EventArgs.Empty );
}
}
}

(2)
Pass a control as an argument into your thread:

namespace ConsoleApplicat ion
{

class Program
{
static void Main()
{
Control ctrl = null;// Some control of the GUI,
// e.g. main form
ThreadHelper helper = new ThreadHelper(ct rl);
Thread readThread =
new Thread(new ThreadStart(hel per.ThreadLoop) );
}
}

class ThreadHelper
{
public ThreadHelper(Co ntrol ctrlInUiThread)
{
this.ctrlInUiTh read = ctrlInUiThread;
}
private Control ctrlInUiThread;
private bool something = false;
public EventHandler SomeEvent;
public void ThreadLoop()
{
while(true)
{
if(something)
{
ctrlInUiThread. Invoke(
new RaiseSomeEventI nUiThreadHandle r(
RaiseSomeEventI nUiThread)
);
}
}
}
private delegate void RaiseSomeEventI nUiThreadHandle r();
private void RaiseSomeEventI nUiThread()
{
if (SomeEvent != null)
SomeEvent(this, EventArgs.Empty );
}
}
}

HTH,
Andy
Jun 14 '06 #2
A minor obo; to use Invoke and BeginInvoke you can actually limit to the
ISynchronizeInv oke interface, rather than Control - but still pass a control
in as the object to provide this interface.
Not only is this more abstract, but in a partitioned assembly model it means
you don't need to reference the windows.forms assembly from your console
wrapper, as this is in the "System" assembly (System.Compone ntModel
namespace).

But it works either way ;-p

Marc
Jun 14 '06 #3

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

Similar topics

10
4951
by: Drakier Dominaeus | last post by:
This is my first time posting here, so please forgive me if I do anything incorrectly. I've been learning C# and working with different things and decided I wanted to get into Multi-Threading. My problem is that I must not be doing it right because only some of the stuff works as would be expected. I'll post what exactly is happening, then I'll post the sample code I'm using that is giving me the problems. I'm sure its something I've...
3
14311
by: Stampede | last post by:
Hi, I want to use the FileSystemWatcher in a Windows Service. I read an article, where the author created the FileSystemWatcher object in a seperate thread and when the event is fired, he started a working thread for processing the file, created a new FileSystemWatcher (as he said for real time processing), and then called the join method for the first thread. I can't really see the sence in this. Aren't the events of the...
12
2807
by: scsharma | last post by:
Hi, I am working on creating a webapplication and my design calls for creating main webform which will have menu bar on left hand side and a IFrame which will contain all the forms that are shown when menu items are clicked.Besides these i would like to put a custom status bar. Any error message encountered in any of the webpage will be displayed in the banner. The problem iam encountering is how to access the customer status bar in child...
1
3439
by: Ryan Malone | last post by:
I have a situation where I need to download multiple files in a vb.net application. To speed up the process, I am trying to download multiple files at one time looping through each of the files and launching them in their own thread (code below). The problem is that when there are hundreds of files to download, it launches them all at the same time and half don't get downloaded. Any idea how I could control it to launch say 4 at a time,...
1
1237
by: Tom | last post by:
First of all, is it 'legal' (i.e. thread-safe) to pass events back from a worker thread to the main thread? I.E. Can one put PUBLIC EVENT XYZ in their worker thread and then raise that event for processing back in the main thread? I believe this is OK, but I want to check first... Secondly, if this is 'legal', what happens in the following scenario: Lets say that in my main (Win Form) program/thread I spawn two worker threads. Thread one...
0
1029
by: Shahzad Godil | last post by:
VB.net solution with this issue http://dotsystem.biz/Files/Shahzad/ThreadCheck.zip Required Visio 2003. I have one remoting server which is launch Visio activex based application on remote request. I have this code to launch my application. But it is giving "Class Not registered" error in InitializeComponent of form which has ActiveX control. My application is working perfectly when I am launching this without any
6
2121
by: cyberco | last post by:
In my wxPython app a non-GUI thread (that reads info from the network) tries to open a frame to show the new info. This results in my app hanging (which is not too surprising). Coming from a C# environment I wonder if there is some sort of delegate mechanism in wxPython to do this sort of thing. 2B
4
2054
by: jehugaleahsa | last post by:
Hello: Is there a way to prevent one event from firing while another event is already being fired? I have a tool that extracts media from web pages and it has multiple events firing when the status of the download changes. Some of the events are used to tell the next file to download while others manager other resources. However, on occasion, one event will
15
6550
by: damiensawyer | last post by:
Hi, I am creating a class (from a base class) that needs to trigger events (code at bottom). I am instatiating the classes and wiring up the events as follows. clsDetermineConnection oDC = new clsDetermineConnection(Request); oDC.LogMessage += new RunTraceBase.TraceArguments(LogMessagesFromEvents);
0
9655
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
9497
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10169
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
10110
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
9964
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
5398
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
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
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.