Jump to content



Welcome to AstaHost - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!

Toggle shoutbox Shoutbox Open the Shoutbox in a popup

@  yordan : (14 April 2014 - 05:28 PM) By The Way, This Could Be An Interesting Subject For A Topic, What About Posting This Question? Let's See If Other People Have The Same Feeling Concerning Bootlists!
@  yordan : (13 April 2014 - 09:36 AM) Boot Order : Cd, [Usb,] Hard Drive :D
@  yordan : (11 April 2014 - 07:23 PM) I Simply Let The Bios Do That
@  Ritesh : (11 April 2014 - 10:23 AM) Is It Possible To Launch Fedora Live Cd Or Installation Disk From Hard Drive On Windows Platform Using Grub Mbr File.
@  Ritesh : (11 April 2014 - 10:21 AM) No U Are Not.. Btw.. I Have Question For You.
@  yordan : (10 April 2014 - 08:02 AM) You Are Partially Right.
I Was Not.
Nevertheless, I Am Again :)
@  Ritesh : (09 April 2014 - 07:33 PM) :P
@  Ritesh : (09 April 2014 - 07:33 PM) I Think U R Not..
@  yordan : (09 April 2014 - 09:28 AM) I'm The Master Of The Shoutbox!
@  yordan : (05 April 2014 - 10:32 PM) He-He
@  Ritesh : (04 April 2014 - 06:59 PM) Ha Ha Ha ....
@  yordan : (04 April 2014 - 11:15 AM) Welcome Back, Starscream!
@  yordan : (03 April 2014 - 02:31 PM) And I Hope That He Will Come Back Soon :)
@  yordan : (01 April 2014 - 02:53 PM) Nice, Ritesh Came, I'm Not Home Alone Today.
@  Ritesh : (01 April 2014 - 08:51 AM) Oh!!! Poor Dear Yordan..
@  yordan : (31 March 2014 - 10:02 AM) I'm A Poor Lonesome Cow-Boy
@  yordan : (27 March 2014 - 02:22 PM) He Is Unpatient Due To His Patients!
@  Ritesh : (27 March 2014 - 10:46 AM) :(
@  Ritesh : (27 March 2014 - 10:46 AM) He Is Busy With His Patients.
@  yordan : (26 March 2014 - 08:12 PM) Ahsani, Where Are You?

Photo
- - - - -

Help: Multi-threading Trouble In Custom Socket Component


2 replies to this topic

#1 turbopowerdmaxsteel

turbopowerdmaxsteel

    Premium Member

  • [HOSTED]
  • 449 posts
  • Gender:Male
  • Location:Kolkata, India
  • Interests:Programming, Gaming, Cricket, Listening to Music and watching action cartoons.
  • myCENTs:30.99

Posted 15 August 2007 - 10:23 PM

I am creating a wrapper component for the System.Net.Sockets.Socket class which would resemble the Winsock control. The basic idea is to do operations such as Connect & Receive asynchronously. The component has events such as Connected, PacketArrival in response to these. To do this, I use the following code:-

Socket Code
Imports System.Net.Sockets

Public Class MySocket
	Dim Sck As Socket	   ' Socket Object

	' Connect Method
	Sub Connect(ByVal Host As String, ByVal Port As Integer)
		Sck = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)

		' Connect Multithreaded
		Sck.BeginConnect(Host, Port, New AsyncCallback(AddressOf ConnectCallBack), Sck)
	End Sub

	' CallBack For System.Net.Sockets.Socket's asynchronous Connect Operation
	Private Sub ConnectCallBack(ByVal ar As IAsyncResult)
		Sck.EndConnect(ar)
		RaiseEvent Connected()
	End Sub

	' The event to Notify the Owner Form
	Public Event Connected()
End Class

The problem here is that the Connected event is raised on the thread of the asynchronous Connect method of the Socket. So, if I try to access any control from this event's handler, a Cross-thread operation not valid exception is generated.

Window Code
Public Class Form1

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		mSck.Connect("www.microsoft.com", 80)
	End Sub

	Private Sub mSck_Connected() Handles mSck.Connected
		' Set the Window Text to Notify this Event
		Me.Text = "Connected"
	End Sub

End Class

I am able to get past this by following the process stated in MSDN for making thread safe calls.
Public Class Form1

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		mSck.Connect("www.microsoft.com", 80)
	End Sub

	Delegate Sub ConnectedCallBack()
	Private Sub mSck_Connected() Handles mSck.Connected
		Dim D As New ConnectedCallBack(AddressOf SocketConnected)
		Me.Invoke(D)
	End Sub

	Sub SocketConnected()
		' Set the Window Text to Notify this Event
		Me.Text = "Connected"
	End Sub

End Class

While, this works, I am not satisfied with having to create callbacks for these routine events. Is there a way to raise the event in the same thread as that of the Form?

#2 faulty.lee

faulty.lee

    Super Member

  • [HOSTED]
  • 500 posts
  • Interests:Electronics, Software Programming, Embedded Programming, Movies, Windows Shopping
  • myCENTs:79.88

Posted 16 August 2007 - 03:01 PM

While, this works, I am not satisfied with having to create callbacks for these routine events. Is there a way to raise the event in the same thread as that of the Form?

I don't think you have much choices. I'm also trying to learn more about multithreading, I found that it's actually very tedious to do. I've written a multithreaded multimedia software, running from 5 to 20 threads at a time. Took me quite a while to find tricks that can synchronize them all. Also, sometime when it fail, it doesn't just stop, but it runs for a while, then start to show some funny behavior. So have to spend more time debugging.

Anyway, as for your case, i've done something similar as well, but i'm using a main tread to loop and wait for the event, and then dispatches the job to worker threads. The worker thread contained in a separated class. Called using a function, and that function will then create the thread to do the job. So, when i call the function, a new instance is instantiated, which everything contain within itself. To keep track of the worker thread, you can add that into an array. Then have the main thread check for a bit that indicate if the job is done, then you can dispose and remove it from the array. The checking can be done at a preset duration, depending on the job load, or whenever a new worker thread need to be added.

#3 turbopowerdmaxsteel

turbopowerdmaxsteel

    Premium Member

  • [HOSTED]
  • 449 posts
  • Gender:Male
  • Location:Kolkata, India
  • Interests:Programming, Gaming, Cricket, Listening to Music and watching action cartoons.
  • myCENTs:30.99

Posted 16 August 2007 - 03:12 PM

I have manged to get the job done. What I did was inherit the class fron System.Windows.Forms.Control. This allows Invoke to be called from within the class and thus removes the overhead of having to do so everytime. This comes at a cost, though. The Socket control is no longer the design time only visible thingy. So, I had to handle the Paint messages for the control and it is now quite similar to the Winsock control.



Reply to this topic



  


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users