C# WPF - Tasks vs Multithreading, and updating UI components from them

前端 未结 3 1940
忘掉有多难
忘掉有多难 2021-01-23 02:11

So basically I\'m using C# to write a port scanning WPF application, that will scan over a thousand combinations of ports and IPs (x.x.x.x:xx). As a result, I have to split this

3条回答
  •  鱼传尺愫
    2021-01-23 02:43

    What you SHOULD do is: Give some events CmdExec so that they can be fired when something changes, for example:

    public delegate void CombinationFoundDelegate(object sender, int port, string ip); 
    //note that it would be better to create class derived from EventArgs instead of passing bare arguments in this delegate
    public class CmdExec
    {
        public event CombinationFoundDelegate CombinationFound;
    
        public void Runcmd()
        {
           //do your work and call event when necessary:
           if(CanConnect(ip, port))
             CombinationFound?.Invoke(this, port, ip);
        }
    }
    

    And then on your form just listen to this event:

    cmdobj.CombinationFound += new CombinationFoundCallback;
    

    And all the GUI updates may happen in this callback. But, you have to be aware that this callback is called from different thread (as CmdExec runs on different thread), so you need to synchronize your controls. You can use it Dispatcher for that case:

    Action a = new Action(() => textBox.Text = ip); 
    
    if(!Dispatcher.CheckAccess())
       Dispatcher.Invoke(a);
    else
       a();
    

    So, note that CheckAccess is for some reason hidden in Intellisense, but it's really there. CheckAccess just cheks wheter synchronization is required or not (in Winforms it would be InvokeRequired).

    Next, if synchronization is required, you can run your UI update code inside dispatcher invoke method. However, if not synchronization is required, you can just run your ui code here. That's why I have created Action for this - not to duplicate the code.

    And that's simply it. (Code written from head, so there can be some difference with real code, but this is how it should look and work).

提交回复
热议问题