Click here to Skip to main content
13,046,337 members (111,692 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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)
                timer.Interval = 1000
            End If
'pollfunction() starts
        End If
    End Sub
Private Sub StartButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartButton.Click
End Sub
Private Sub StopPoll()
        mpf.NoConnectionLabel.Visible = True
        isPolling = False
        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
    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
            For j As Integer = 30 To Inc
                mpf.Left = j
            Next j
        Next i
        If Not StartButton.Enabled = True Then
        End If
        mpf.TopLevel = False
    End Sub
Posted 20-Nov-12 4:30am
Updated 22-Nov-12 2:41am
Sergey Alexandrovich Kryukov 20-Nov-12 13:37pm
Why using MDI at all?
Rate this: bad
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:[^],
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[^].

juitanis22 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 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...

Rate this: bad
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[^],
Control events not firing after enable disable + multithreading[^].

Rate this: bad
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 ([^]):

MVVM — Model View View Model,[^],

MVC — Model-View-Controller,[^]),

MVA — Model-View-Adapter,–view–adapter[^],

MVP — Model-View-Presenter,[^].

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


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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 22 Nov 2012
Copyright © CodeProject, 1999-2017
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