Two Form and event communication. 
Author Message
 Two Form and event communication.

Hi,
I have two forms.
FormA updates data.
FormB is looking at the data with a dataset / datagrid
I want to refresh the datagrid when ever formA updates the data.
The refresh logic works OK in that a push button on formB can call the
refresh subroutine OK
I must have something wrong in my event handling because a call by the FormB
event handler doesn't work.
I have :
Form A with a public event.
On form A is a business class which has a public formA variable (myForm)
that points to its FormA parent.
It is listening to a phone line on its own thread.
When a call comes in it updates the database and asks its parent to raise
the event.
It does this by calling Myform.raiseeventwrtitten()
This is where I think I am going wrong.
Ideally I would like
raiseevent myform.theevent
But you can't do that. The event is not visible to the variable.
My code seems to work ok except if I try to refresh the formB datagrid.
Things like message boxes come up in the event handler OK and I can update
the Form B status bar OK But the datagrid either crashes or doesn't refresh
properly in that it develops a second set of scroll bars.
So...
Given that the business class is sitting on its own thread which was started
by FormA. How does it let tell FormB that it should update its Datagrid?
Thanks
Bob


Tue, 20 Sep 2005 11:36:34 GMT  
 Two Form and event communication.
It looks like you are trying to update the UI from a thread other than the
UI thread.  Generally none of the WinForms instance methods are thread safe.
If the method that raises the event is called from a seperate thread then it
will cause the whole sequence to be called on that thread as well.  When you
need to manipulate a control on a nonUI thread you should use its Invoke or
BeginInvoke methods to feed it a delegate to run on the UI thread.
Basically you want to remove all UI code from the event handler and instead
put it in a function.  That would make my code look like the following

public sub YourEventHandlerForFormAEvent()
    DataGrid1.BeginInvoke(new methodinvoker(addressof
me.UpdateUIOnRightThread))
end sub

public sub UpdateUIOnRightThread()
    'do grid updating here
end sub

Jeremy Samples

****************************
below is a snippet that proves that the events are raised on the workers
thread instead of the UI thread
****************************

imports System.Windows.Forms
imports System
imports System.Threading

public delegate sub RefreshGridEventHandler(byval sender as object, byval ea
as eventargs)

public class FormA : Inherits Form
 public event RefreshGrid as RefreshGridEventHandler

 public sub New()
  dim worker as new WorkerClass(me)
  dim t as new Thread(new ThreadStart(addressof worker.Start))
  MessageBox.Show(String.Format("FormA new {0}",
Thread.CurrentThread.GetHashCode()))
  t.Start()
  dim btn as new Button()
  me.controls.add(btn)
  addhandler btn.click, addressof testID
 end sub

 public sub TestID(byval sender as object, byval ea as EventArgs)
  MessageBox.Show(String.Format("Click [MUST BE UI THREAD] {0}",
Thread.CurrentThread.GetHashCode()))
 end sub

 public sub RaiseEventWritten()
  MessageBox.Show(String.Format("REW {0}",
Thread.CurrentThread.GetHashCode()))
  RaiseEvent RefreshGrid(me, EventArgs.Empty)
 end sub
end class

public class WorkerClass
 private myForm as FormA

 public sub New(byval frm as FormA)
  myForm = frm
 end sub

 public sub Start()
  MessageBox.Show(String.Format("Start {0}",
Thread.CurrentThread.GetHashCode()))
  myForm.RaiseEventWritten()
 end sub
end class

public class FormB : Inherits Form
 public shared sub Main()
  dim frmA as new FormA()
  dim frmB as new FormB()
  frmA.Show()
  AddHandler frmA.RefreshGrid, addressof frmB.RefreshNow
  MessageBox.Show(String.Format("Main {0}",
Thread.CurrentThread.GetHashCode()))
  Application.Run(frmB)
 end sub

 public sub RefreshNow(byval sender as object, byval ea as EventArgs)
  MessageBox.Show(String.Format("RefreshNow {0}",
Thread.CurrentThread.GetHashCode()))
 end sub
end class


Quote:
> Hi,
> I have two forms.
> FormA updates data.
> FormB is looking at the data with a dataset / datagrid
> I want to refresh the datagrid when ever formA updates the data.
> The refresh logic works OK in that a push button on formB can call the
> refresh subroutine OK
> I must have something wrong in my event handling because a call by the
FormB
> event handler doesn't work.
> I have :
> Form A with a public event.
> On form A is a business class which has a public formA variable (myForm)
> that points to its FormA parent.
> It is listening to a phone line on its own thread.
> When a call comes in it updates the database and asks its parent to raise
> the event.
> It does this by calling Myform.raiseeventwrtitten()
> This is where I think I am going wrong.
> Ideally I would like
> raiseevent myform.theevent
> But you can't do that. The event is not visible to the variable.
> My code seems to work ok except if I try to refresh the formB datagrid.
> Things like message boxes come up in the event handler OK and I can update
> the Form B status bar OK But the datagrid either crashes or doesn't
refresh
> properly in that it develops a second set of scroll bars.
> So...
> Given that the business class is sitting on its own thread which was
started
> by FormA. How does it let tell FormB that it should update its Datagrid?
> Thanks
> Bob



Tue, 20 Sep 2005 12:08:24 GMT  
 Two Form and event communication.
Jeremy,
Spot On.
Thanks very much.

regards
Bob Clegg

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



Fri, 23 Sep 2005 12:51:13 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Pressing a key produce two events in two different forms

2. Two-Way Communication in Automation

3. Communication between two VB 5 apps?

4. Communication between two VB programs

5. Communication between two PCs with TCP/IP

6. Two-way communication with a DOS child process.

7. communication between two browsers

8. communication between two VB application

9. communication between two VB application

10. Two-way communication between automation servers

11. communication between two VB application

12. Communication between two programs

 

 
Powered by phpBB® Forum Software