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

Home Posts Topics Members FAQ

vb 2005, .net 2.0 - ? how to make a cross-thread call, independent of GUI ?

MTEXX
5 New Member
Hi all-

I'm trying to wrap the Socket class to raise events on the same thread that the class is instantiated on. For example: connected(), stateChanged(), dataArrival() etc- similar to old vb6 Winsock. The class interface should not block. Eg subs and events. Likewise, the events should fire back on the same thread that created the class. This must work for a Windows Service as well.

It's easy to spawn a new thread. But when something interesting happens in there, how do I make a call on another thread?

Is there something I could do such as mOriginalThread .invoke(onDataA rrival) ?

Thanks in advance
Jan 9 '08 #1
2 2518
MTEXX
5 New Member
Well, I'll answer my own question. Or at least post a workaround.

Purpose:
Use asynchronous methods, whose callbacks are on unknown threads, to raise events on a known thread.
ALSO must NOT utilize Windows.Forms such that this can be used in a Windows Service (though it may be possible...)

Method:
I will use a queue to store information about events to raise. Appending and reading from the queue shall all be thread safe.

Example:
I will demonstrate wrapping the System.Net.Sock et class to appear similar to VB6's Winsock object.

Partial Sample Code:
* I'm not going to post an entire class, but I'll give the essentials.
1) In the "Winsock" class, we'll need to make a private enum to describe the style of event.
Expand|Select|Wrap|Line Numbers
  1. Private Enum EventStyles
  2.     stateChanged
  3.     sendProgress
  4.     dataArrival
  5.     newConnection
  6. End Enum
2) Now make a struct to contain the least common multiple of all event data.
Expand|Select|Wrap|Line Numbers
  1. Private Structure EventData
  2.     Dim style As EventStyles
  3.     Dim prev As States
  4.     Dim curr As States
  5.     Dim bytesSent As Integer
  6.     Dim data As String
  7.     Dim peer As Winsock
  8. End Structure
3) Define your events as public:
Expand|Select|Wrap|Line Numbers
  1. Public Event stateChanged(ByVal sender As Winsock, ByVal prevState As States, ByVal newState As States)
  2. Public Event sendProgress(ByVal sender As Winsock, ByVal bytesSent As Integer)
  3. Public Event dataArrival(ByVal sender As Winsock, ByVal data As String)
  4. Public Event newConnection(ByVal sender As Winsock, ByVal peer As Winsock)

4) Make a Queue for storing your EventData instances
Expand|Select|Wrap|Line Numbers
  1. Private mEventQueue As Queue 'System.Collections i think...
5) Instantiate the queue in New()
Expand|Select|Wrap|Line Numbers
  1. mEventQueue = new Queue
6) Where you would usually do a RaiseEvent, instead, populate a EventData struct and enqueue it using a helper method. You will see that I also lock when modifying the queue.
Expand|Select|Wrap|Line Numbers
  1. Private Sub enqueueStateChanged(ByVal prev As States, ByVal curr As States)
  2.     Dim ed As EventData
  3.     ed.style = EventStyles.stateChanged
  4.     ed.bytesSent = 0
  5.     ed.curr = curr
  6.     ed.data = ""
  7.     ed.peer = Nothing
  8.     ed.prev = prev
  9.     enqueueEvent(ed)
  10. End Sub
  11.  
  12. Private Sub enqueueEvent(ByVal ed As EventData)
  13.     Monitor.Enter(Me)
  14.     mEventQueue.Enqueue(ed)
  15.     Monitor.Exit(Me)
  16. End Sub
7) For the user to retrieve the event on a known thread, he must execute code on a known thread. I have not found a way to do this otherwise. Notice above that in order to keep the thread locked as short as possible, I make a copy of the queue, THEN consume the copy.
Expand|Select|Wrap|Line Numbers
  1. Public Sub doEvents()
  2.     Dim myQueue As Queue = New Queue
  3.  
  4.     Monitor.Enter(Me)
  5.     If (mEventQueue.Count > 0) Then
  6.         myQueue.Enqueue(mEventQueue.Dequeue)
  7.     End If
  8.     Monitor.Exit(Me)
  9.  
  10.     If (myQueue.Count > 0) Then
  11.         Dim ed As EventData
  12.         ed = myQueue.Dequeue
  13.         Select Case ed.style
  14.             Case EventStyles.dataArrival
  15.                 RaiseEvent dataArrival(Me, ed.data)
  16.             Case EventStyles.newConnection
  17.                 RaiseEvent newConnection(Me, ed.peer)
  18.             Case EventStyles.sendProgress
  19.                 RaiseEvent sendProgress(Me, ed.bytesSent)
  20.             Case EventStyles.stateChanged
  21.                 RaiseEvent stateChanged(Me, ed.prev, ed.curr)
  22.         End Select
  23.     End If
  24. End Sub
* Form code
8) In a form, for example, here is how I use the Winsock class. Define the reference using "WithEvents " as usual.
Expand|Select|Wrap|Line Numbers
  1. Private WithEvents sckClient As Winsock
9) Inside FormLoad or whatnot:
Expand|Select|Wrap|Line Numbers
  1. sckClient = New Winsock
  2. tmrDoEvents.Start() 'A WinForms Timer - set to 1 millisecond
10) Make the timer call the Winsock's DoEvents (as described above)
Private Sub tmrDoEvents_Tic k(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles tmrDoEvents.Tic k
sckClient.doEve nts()
End Sub

'Here you see that consuming an event WILL occur on the proper thread- in this case the GUI thread, which is where tmrDoEvents_Tic k() occurs:
Expand|Select|Wrap|Line Numbers
  1. Private Sub sckClient_stateChanged(ByVal sender As xxxxx.Winsock, ByVal prevState As xxxxxx.Winsock.States, ByVal newState As xxxxxx.Winsock.States) Handles sckClient.stateChanged
  2.     'Console.WriteLine("frmWinsock::sckClient_stateChanged() thread id " & System.Threading.Thread.CurrentThread.ManagedThreadId.ToString)
  3.     sckImgClient.setState(sender.state)
  4.     If (sender.state = Winsock.States.error_) Then
  5.         Console.WriteLine("frmClient::sckClient_StateChanged: " + sender.errorMessage)
  6.     End If
  7. End Sub


I hate doing lengthy workarounds, especially when they involve risk and time. But this demonstrates at least that the workaround does indeed work.
Cheers,
-MTEXX
Feb 4 '08 #2
Plater
7,872 Recognized Expert Expert
I am not sure you are using events in the manor that they were designed for.
I am also curious as to why you were going back to the archaic winsock style, just for familiarity sake?
Feb 4 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
2116
by: Unigroup of New York | last post by:
Content-Type: multipart/mixed; boundary="------------C465DF38DCB38DD2AF7117E0" Lines: 327 Date: Tue, 15 Feb 2005 23:36:38 -0500 NNTP-Posting-Host: 24.46.113.251 X-Complaints-To: abuse@cv.net X-Trace: fe12.lga 1108528794 24.46.113.251 (Tue, 15 Feb 2005 21:39:54 MST) NNTP-Posting-Date: Tue, 15 Feb 2005 21:39:54 MST Xref: number1.nntp.dca.giganews.com comp.lang.python:398656
0
1728
by: Rare Book School | last post by:
RARE BOOK SCHOOL 2005 Rare Book School is pleased to announce its schedule of courses for 2005, including sessions at the University of Virginia, the Walters Art Museum/Johns Hopkins University in Baltimore, and the Freer/Sackler Galleries in Washington, DC. Please visit our web site for a complete brochure, expanded course descriptions, and application forms: <www.rarebookschool.org>
0
1578
by: melledge | last post by:
Full Programme for XTech 2005 Announced Premier European XML Industry Event Expands Focus to "XML, the Web and Beyond"; Co-hosted by the Mozilla Foundation,W3C, and OASIS, Presenters Include BBC, IBM, Justsystem, Microsoft, Oracle, Sun Alexandria, Va. - March 7, 2005 - IDEAlliance, a leading industry association dedicated to fostering XML and other information technology
0
1903
by: melledge | last post by:
IDEAlliance's XTech 2005 to Cover Hot Trends in Browser Technology As Mozilla, Google, Flickr, Microsoft and Others Redefine Web Interfaces, Conference Will Feature Insights and Perspective from Field's Top Thought Leaders Alexandria, Va. - April 14, 2005 - XTech 2005, the premier European conference for developers and managers working with XML and Web technologies, will feature top minds covering one of the most dynamic - and...
6
1149
by: Paul Aspinall | last post by:
Hi I have VS 2005, but some of the components I'm using (Infragistics NetAdvantage 2005), have issues with .NET 2.0 For this reason, I'm being forced to compile against .NET 1.1 How do I do this?? Is it possible?? Is it supported to use VS 2005 with .NET 1.1?? Thanks
3
2095
by: jason | last post by:
I've been working with C# for over a year now without touching vb.net code. I had a few light years of vb.net before that. No real vb6 or windows form experience. Suddenly, I have an assignment where I've been asked to create a few new pages on an exsisting website. The website is written in vb.net with vb.net codebehind and classes. My employer says I can use C# for new stuff If I want.
1
3643
by: Iwan Petrow | last post by:
Hi, I do this - take some data with sqldataadaptor (at this moment 2000rows) in fill datatable. For each two rows do some calculations and save data to the database with insert command. (the data which I insert are 5 int values for each two rows - so 2000*1999 insert commands) In this way it is very slow. I think this is because of the invoking so
1
11640
by: Alex | last post by:
Hi Everyone, Most of our MS SQL Servers are still running on SQL 2000, but being I will soon be upgrading my workstation to Vista Business I'd like to install MS SQL 2005 Enterprise Manager to manage them all. When I tried to install MS SQL 2000 Enterprise Manager on Vista, it gave me a message saying it's not compatible, so I'll have to use 2005 I guess. We have LOTS of DTS scripts on our SQL 2000 server, and will 2005 EM work with...
1
2097
by: tomzji | last post by:
I am using Sql server 2005. I have one table named 'tblJob'. This table have two fields say 'Staus' and 'ExpiryDate'. when 'Expirydate' come to less than current date, i want to upadte 'Status' field. (Can't use trigger,beacause we don't know when date cross the currrent date) Do i need to write a seperate application for that and add it in windows scheduler? Is there any option in Sql server 2005. ?
7
1683
by: puzzlecracker | last post by:
Is there way to format all my source file in project, perhaps follow some generic template (then I would also need one) since i have a lot of files and I don't want to screw it up manually? Thanks
0
8407
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
8319
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
8837
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8612
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...
1
6175
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5638
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
4171
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...
1
2739
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
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.