Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
Hello,
I have a MainForm with 3 buttons("Start","Stop","New Form"),groupbox and one panel.
I create another Form-->(ModbusPollFormFull) with DataGridView1 which populate the data (from hardware) when i clicked "Start" (timer elapsed method).

i do have a groupbox(with all the settings to control the NewForm->(ModbusPollFormFull)) in the MainForm. I need a "New Form" button to create a new form and populate data with different settings. So, the users can view both data at the same time. But whenever i click the "New Button", the new (ModbusPollFormFull2) will start populate the data (starts with pollcount=1) but the existing (ModbusPollFormFull1) will stop the poll(pollcount stop). All i want is to be able to change the settings of the (ModbusPollFormFull1 or ModbusPollFormFull2) if i select the form and they all still polling until i click "Stop". I dont know how.

timer.Start will start the pollfunction()

I already change the code a little bit . but it still not working.

Private Sub StartPoll()
        pollCount = 0
        'Open COM port using provided settings:
        If mb.Open(PortNameCB.SelectedItem.ToString(), CInt(BaudrateCB.SelectedItem.ToString()), 8, Parity.Even, StopBits.One) Then
 
            'Disable double starts:
            mpf.NoConnectionLabel.Visible = False
            dataType = DataTypeCB.SelectedItem.ToString()
'Settings components
            StartButton.Enabled = False
            QuantityCB.Enabled = False
            SlaveIDTB.Enabled = False
            AddressCB.Enabled = False
            ScanRateCB.Enabled = False
            DataTypeCB.Enabled = False
            'Set polling flag
            isPolling = True
            'Start Timer using provided values:
            timer.AutoReset = True
 
            If ScanRateCB.Text <> " "  Then
                timer.Interval = CDbl(ScanRateCB.Text)
            Else
                timer.Interval = 1000
            End If
'pollfunction() starts
            timer.Start()
        End If
    End Sub
 
Private Sub StartButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartButton.Click
       StartPoll()
End Sub
 
Private Sub StopPoll()
        mpf.NoConnectionLabel.Visible = True
        isPolling = False
        timer.Stop()
        StartButton.Enabled = True
 
'Settings components
        QuantityCB.Enabled = True
        SlaveIDTB.Enabled = True
        AddressCB.Enabled = True
        ScanRateCB.Enabled = True
        DataTypeCB.Enabled = True
 
        mb.Close() 'Closing the serialport
    End Sub
 
 Private Sub StopButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StopButton.Click
        StopPoll()
      
    End Sub
 
Dim mpf As New ModbusPollFormFull
 
Private Sub NewButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewButton.Click
        Start += 1
        Inc += 30
        For i As Integer = 0 To Start
 
            mpf = New ModbusPollFormFull
            mpf.Text = "ModbusPoll:" & CStr(i)
            mpf.Name = "ModbusPollFormFull" & i
 
            Load_Columns()
 
            For j As Integer = 30 To Inc
                mpf.Left = j
            Next j
 
        Next i
 
        If Not StartButton.Enabled = True Then
            StartPoll()
        End If
 
        mpf.TopLevel = False
        Me.Panel1.Controls.Add(mpf)
        mpf.BringToFront()
        mpf.Focus()
        mpf.Show()
 
    End Sub
Posted 20-Nov-12 5:30am
Edited 22-Nov-12 3:41am
v3
Comments
Sergey Alexandrovich Kryukov at 20-Nov-12 13:37pm
   
Why using MDI at all?
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Here is the idea: who needs MDI, ever? Why torturing yourself and scaring off your users?
Do yourself a great favor: do not use MDI at all. You can do much easier to implement design without it, with much better quality. MDI is highly discouraged even by Microsoft, in fact, Microsoft dropped it out of WPF and will hardly support it. More importantly, you will scare off all your users if you use MDI. Just don't. Please see:
http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages[^],
How to Create MDI Parent Window in WPF?[^].

I can explain what to do instead. Please see my past answers:
How to Create MDI Parent Window in WPF? [Solution 2],
Question on using MDI windows in WPF[^],
MDIContainer giving error[^],
How to set child forms maximized, last childform minimized[^].

—SA
  Permalink  
Comments
juitanis22 at 22-Nov-12 8:27am
   
i already improve the question. following your advice to not using mdi..but i still encounter the same problem.
Sergey Alexandrovich Kryukov at 22-Nov-12 10:35am
   
Now, there is another, bigger problem. You strategically wrong. You are trying to use timer in a pretty complex situation. It rarely can be successful. Even in much simpler situations, you should avoid timers. It's difficult to explain at once, but the problems are most unpleasant. Too bad you did not show the handlers you've written to handle timer's clicks. You are using a timer to produce some relatively long-lasting effect, that's the bigger problem.

Worse, it looks you are using the timer System.Windows.Forms.Timer. Not only this timer cannot be used in almost all cases, unless you don't need any accuracy at all (and can tolerate 100% or more delays in timing), but it also is invoked in the UI thread. This is something which makes it easy to use, and this is its the only "benefit". By doing so, you clog you UI thread.

When you want two different population procedures to execute in parallel, you need parallelism. It means, you need threads. Instead, you are trying to simulate parallelism in much more complex and ad-hoc home-baked way. No wonder it's unsuccessful.

Don't even try; you will never get it to work. You need threads, period. Using them is reasonably simple and reliable.

Besides, look what you are doing. You are doing hardware control via Modbus. In this topic, even starting without thread makes no sense at all. This is totally a threading application field...

--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Please see my comment to Solution 1.

You are trying to simulate threads in a most naive and dangerous way, instead of using threads in a million times simpler way.

All the hardware control, as well as communication, networking, performing any linear-logic lengthy task and a lot of other things should be programmed in a separate thread.

Now, it looks like you ignored my advice to your very first question. You are mixing up UI and hardware control and data acquisition. You do need isolate UI from other functionality. The problem is not classes, the problem is layers. I cannot really teach how to do it if you have no idea in one short answer.

Let's come back to threads. You need to perform polling and populate some UI, right? One problem is: you cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Please again see my comments to your very first question and Solution 2. I'm talking about isolation of UI and layered architecture. Isolation of UI is especially important, because UI tend to change much more often than anything else, in much more radical ways.

I suggest you learn and analyze applicability of the following architectural patterns (http://en.wikipedia.org/wiki/Architectural_pattern_(computer_science)[^]):


MVVM — Model View View Model,
http://en.wikipedia.org/wiki/Model_View_ViewModel[^],

MVC — Model-View-Controller,
http://en.wikipedia.org/wiki/Model-view-controller[^]),

MVA — Model-View-Adapter,
http://en.wikipedia.org/wiki/Model–view–adapter[^],

MVP — Model-View-Presenter,
http://en.wikipedia.org/wiki/Model-view-presenter[^].


Pay attention for the motivation of those architectures. If you understand it, you would be able to create better design ideas.

—SA
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 199
1 Sergey Alexandrovich Kryukov 154
2 Sascha Lefévre 120
3 Abhinav S 104
4 Rana Waqas 78
0 Sergey Alexandrovich Kryukov 6,628
1 OriginalGriff 6,298
2 Peter Leow 2,534
3 Maciej Los 2,437
4 Abhinav S 2,378


Advertise | Privacy | Mobile
Web02 | 2.8.150414.1 | Last Updated 22 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100