[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

StartUp Form, still no handle

Posted on 2009-02-24
9
Medium Priority
?
402 Views
Last Modified: 2013-11-07
HI!

If I do this frmMain.Show from a tcpsocket class I get this

The Undo operation encountered a context that is different from what was applied in the corresponding Set operation. The possible cause is that a context was Set on the thread and not reverted(undone)'

If I do this.

    Public Sub ShowForm()
        Dim wh As New MethodInvoker(AddressOf test)
        Me.Invoke(wh)
   End Sub

  Private Sub test()
        If Me.InvokeRequired Then
            Me.Invoke(New ShowServerConsoleDelegate(AddressOf test))
        Else
            Me.Opacity = 100
            Me.ShowInTaskbar = True
            Me.BringToFront()
            Me.Refresh()
        End If
    End Sub

I get

Invoke or BeginInvoke cannot be called on a control until the window handle has been created.

So when having the frmMain as the "Startup form" does not the "framework" create the "handle" so I can use it without creating the form manually?
0
Comment
Question by:AWestEng
  • 5
  • 2
  • 2
9 Comments
 
LVL 1

Expert Comment

by:KCFORSYTHE
ID: 23721461
I had what I think is a similar issue trying to Invoke from within a service.  I had to DIM a form object and do the Invoke on that form object.   What I realized is that the handle does not get assigned unless I set the form.visible=true.    You might want to try doing a Me.Visible=True just before you do the Me.Invoke(wh).   It just seems to me as the same root cause that I ran into.

Good luck,

Kent
0
 
LVL 1

Expert Comment

by:KCFORSYTHE
ID: 23721473
On other thing.....in my case, I did a Me.Visible=True and immediately followed it with a Me.Visible=False since I really did not want to see the form.   This forced the handle to be assigned though.

Kent
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 23722596
You are using "Me"...but you haven't indicated WHERE this code is occuring.  Is it from inside frmMain?...or from an instance of your tcpsocket class?

If the latter, you need to raise an event from tcpsocket that is trapped in frmMain.  Then, from the main from (in that event), you can use your code with "Me".

On a side note, your code to set Opacity is wrong.  At DESIGN-TIME (in the IDE) this value ranges from 0 to 100....but at RUN-TIME this property is actually a single value that ranges from 0 to 1 (for instance, .5 would be 50% opacity).

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Comment

by:AWestEng
ID: 23725719
The code above is in the frmMain.
Some more pseudo code
 When calling frmMain.Showform it will crash on invoke

Public Class TcpSocket
 
Public Sub ProcessCommands()
    -------
   Select case cmd
       Case "OpenForm"
        frmMain.ShowForm
  End Select
  -----
End Sub
 
  -------
End Class
 
 
Public Class frmMain
 
   Public Sub ShowForm
      If Me.InvokeRequired Then
          Me.Invoke(New ShowServerConsoleDelegate(AddressOf test))
      Else
            Me.Opacity = 100
            Me.ShowInTaskbar = True
            Me.BringToFront()
            Me.Refresh()
       End If
    End Sub
 
End Class

Open in new window

0
 
LVL 1

Author Comment

by:AWestEng
ID: 23725737
thx for some inputs
Ok then I can do the .show stuff, but it feels there should be another "more correct" way to do this?
0
 
LVL 1

Author Comment

by:AWestEng
ID: 23725786
one other problem is that if I raise an event in the Socketclass and trap in frmMain I also get an exception, if this method has a invoke in it. and it feels it also has something to do with the handle stuff.
I will try the anser you posted in my other PAQ Idle_Mind
  Me.Invoke(New UpdateListviewDelegate(AddressOf UpdateListview), New Object() {message, type, showdate})

http://www.experts-exchange.com/Programming/Languages/.NET/Q_24145802.html
 
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 23726134
In your latter pseudo code you are accessing frmMain directly from the TcpSocket class which kinda defeats the purpose of the Invoke() calls.

I assume that since frmMain is the startup form...it is creating the instances of TcpSocket?  If so, make TcpSocket raise a custom event that frmMain subscribes to.  From the event handler, use your Invoke() code:

    Public Class TcpSocket

        Public Event ShowForm()

        Public Sub ProcessCommands()
            Select case cmd
                Case "OpenForm"
                    RaiseEvent ShowForm()
            End Select
        End Sub

    End Class

Then in frmMain:

    Public Delegate Sub ShowFormDlg()

    Public Sub ShowForm() Handles ts.ShowForm ' <--- or wire it up with AddHandler()
        If Me.InvokeRequired Then
            Me.Invoke(New ShowFormDlg(AddressOf ShowForm), New Object() {})
        Else
            Me.Opacity = 1
            Me.ShowInTaskbar = True
            Me.BringToFront()
            Me.Refresh()
        End If
    End Sub
0
 
LVL 1

Author Comment

by:AWestEng
ID: 23726387
You did it again,, :)  thx Idle_Mind ... A perfect solution..
0
 
LVL 1

Author Closing Comment

by:AWestEng
ID: 31550506
PERFECT!!!
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Integration Management Part 2
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month18 days, 4 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question