473,769 Members | 5,072 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Threading, Delegates and the Invoke method.

78 New Member
Hi guys. I was messing with Threading and stuff, and i have reached a point where i'm not sure what's causing the current behavior.

Here's the code:
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.  
  3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  4.         Dim nt As New newThread(Me)
  5.         Dim t As New Threading.Thread(AddressOf nt.ThreadCode)
  6.         t.Start()
  7.     End Sub
  8.  
  9.     Public Sub CreateMessageBox()
  10.         Dim f As New Form()
  11.         f.Show()
  12.     End Sub
  13. End Class
  14.  
  15.  
  16. Public Class newThread
  17.  
  18.     Public Delegate Sub Inform()
  19.     Public sendInform As Inform
  20.     Public main As Form
  21.  
  22.     Public Sub New(ByRef main As Form1)
  23.         Me.main = main
  24.         sendInform = New Inform(AddressOf main.CreateMessageBox)
  25.     End Sub
  26.  
  27.     Public Sub ThreadCode()
  28.        Threading.Thread.Sleep(2000)
  29.     '*********************************************
  30.        sendInform() (VS) main.Invoke(sendInform)
  31.     '***********************************
  32.     End Sub
  33. End Class
The problem is in the last statement, when i use main.Invoke() the CreatemessageBo x creates a msgbx, which is part of the Form1. But when i use sendInform() I get a messagebx that appears, but is part of the new thread. So if it were a Form instead of a msgbx, it would appear and then close as soon as the new thread exits.

My questionis , obviously, why? Why is one working the way i want it to and the other not.

My theory is that, when the main.invoke() is used, the loader(?) already assigns the address of the method as the main thread's Createmsgbx. But so when i use invoke(), it is somehow assigning the new thread's createmsgbx address. But i have a feeling i'm wrong, cause i know that threads share code, so the addresses must be the same. So why i one getting "assigned" as a call from the main thread, and the other as a method call from the new thread?

Thanks a lot.
Sep 1 '07 #1
0 1070

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

Similar topics

13
371
by: RCS | last post by:
I have a UI that needs a couple of threads to do some significant processing on a couple of different forms - and while it's at it, update the UI (set textboxes, fill in listviews). I created a base class for the worker class, and made up some functions/delegates to handle the invoke stuff for the UI and that was fine for a prototype. I rewrote this chunk, broke things out into different classes - but the threading is still the same - and...
17
1502
by: Arun Kumar | last post by:
What is wrong with this code. All i am trying to test is 3 progressbar and one button. On buttonclick i create 3 threads and each thread calls a method which in turn updates the progressbar and it works. I would to know if this can be used. Thanks private void button1_Click(object sender, System.EventArgs e) { ThreadStart job = new ThreadStart(onemethod);
3
4512
by: Asad | last post by:
Hi, I am trying to write some threading code to my application. The reason I've been tempted to do this is because, I am doing some FTP uploads, and sometimes during the put method, the application just hangs ("Not Responding") instead of the regular timeout and return. I am using a library to perform FTP stuff and so there may be a bug in there. In any case, what I am concerned with is my application executing with graceful execution...
9
1071
by: Kelly Adams | last post by:
I am stumped at the moment by something, and the searching I've done hasn't led to a clear answer. Here is what I want to do: I have two threads: thread A and thread B. I want Thread B to raise an event that will be handled within the context of thread A. By default, an event raised by Thread B will be processed within thread B's context. That's not what I want. What is the best / simplest way to accomplish this? I've looked at...
3
3296
by: Pavils Jurjans | last post by:
(I am sorry to crosspost both here and in asp.net group. I think, this question quite certainly belongs better to this group, but by error I posted the question to asp.net group) Hello, I am reading the article http://msdn.microsoft.com/msdnmag/issues/04/01/BasicInstincts/ , and, since the sample code is VB.NET, I am wondering what the heck is the "Invoke" method :) I used to think that I can simply directly call the delegate
4
2818
by: Cedric Rogers | last post by:
I wasn't sure if I could do this. I believe I am stretching the capability of what generics can do for me but here goes. I have a generic delegate defined as public delegate bool RuleDelegate<T>(T item); In my class my goal is to use a generic list collection to contain my generic delegates. This will allow me to pass this List to another library and call this list of functions. This could provide a new way to build rule base...
3
3240
balabaster
by: balabaster | last post by:
I have a class that I want to make thread-safe and am investigating the ISyncronizeInvoke interface and wondering just what it will take to implement this interface. So far the basic concept of my code is: Public Class MyClass1 Implements System.ComponentModel.ISynchronizeInvoke Private _InvokeRequired As Boolean = False Public Function BeginInvoke(ByVal method As System.Delegate, ByVal args() As Object) As...
69
5592
by: raylopez99 | last post by:
They usually don't teach you in most textbooks I've seen that delegates can be used to call class methods from classes that are 'unaware' of the delegate, so long as the class has the same signature for the method (i.e., as below, int Square (int)). Here is an example to show that feature. Note class "UnAwareClass" has its methods Square and Cuber called by a class DelegateClass. This is because these methods in UnAwareClass have the...
1
1096
by: Rymfax | last post by:
I was hoping someone could help me figure something. I have a class that is a downloader and I've created some events on it so that a Windows Form can use those to be notified when to update screen information. The problem I'm running into is that when the downloader runs on a different thread and those Events fire, if the corresponding method on the GUI for that even updates any Controls, I get a threading error. I know that I can...
0
9423
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
10214
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...
1
9996
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
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7410
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
6674
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
5304
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
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3563
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.