0

I am currently converting a Windows Forms application into a Console application in C#.

I am busy converting the code and I am having problems with some of the syntax which I am unsure of how to correctly convert. Here is the original code in the windows forms app:

    private eSkan.api.TeSkanAPI feSkanAPI = null;

    private void MessageFilter_AddRemove_Invoked(bool AddFilter, IMessageFilter Filter)
    {
        if (AddFilter)
        { 
            Application.AddMessageFilter(Filter); 
        }
        else 
        { 
            Application.RemoveMessageFilter(Filter); 
        }
    }
    private void MessageFilter_AddRemove(bool AddFilter, IMessageFilter Filter)
    {
        {
            IAsyncResult sr = BeginInvoke((ESKAN_ADD_REMOVE_MESSAGEFILTER)MessageFilter_AddRemove_Invoked,
                                          AddFilter, Filter);
            sr.AsyncWaitHandle.WaitOne(2000);
        }
    }

    private void IniteSkan()
    {
        lMSG.Items.Clear();
        feSkanAPI = new TeSkanAPI();
        // To add a message filter you have to add it from the MAIN thread - thus the main form
        feSkanAPI.AddRemoveMessageFilterProc = MessageFilter_AddRemove;
        feSkanAPI.OneSkanEvent += eScan_Callback;
        feSkanAPI.Thread_Start(true);
    }

    public Form1()
    {
        InitializeComponent();

    }


    private void eScan_Callback_safe(EeSkan_EventType command, object Data)
    {

        lMSG.Items.Insert(0, "++++++++");
        lMSG.Items.Insert(0, DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));
        lMSG.Items.Insert(0, "New Command "+command.ToString());

        if (Data is TeSkan_EventInfo)
        {
            lMSG.Items.Insert(0, "Data File : " + ((TeSkan_EventInfo)Data).DataFile);
            lMSG.Items.Insert(0, "Image File : " + ((TeSkan_EventInfo)Data).ImageFile);
        } else if (Data is string)
        {
            lMSG.Items.Insert(0, "Data String : " + (string) Data); 
        }

        //lMSG.Items.Insert(0, "Image File " + ScanInfo.ImageFile);
        //lMSG.Items.Insert(0, "Data File " + ScanInfo.ImageFile);
        lMSG.Items.Insert(0, "--------");
    }

    /// <summary>
    /// When accesing GUI/Form/Components - you cannot work cross-thread.
    /// This method will invoke eScan_Callback_safe in main/GUI thread if another thread called it, 
    /// or call eScan_Callback_safe directly if already on GUI/main thread
    /// 
    /// </summary>
    /// <param name="ScanInfo"></param>
    private void eScan_Callback(EeSkan_EventType command, object Data)
    {


        if (InvokeRequired)
        {
            this.Invoke((ESKAN_EVENT_CALLBACK)eScan_Callback_safe, command, Data);
        }
        else
        {
            eScan_Callback_safe(command, Data);
        }
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        feSkanAPI.Thread_Stop(10000);
        feSkanAPI.Dispose();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        IniteSkan();
    }

I am using Action to do the BeginInvoke part in the console application. And I would like to know what would be the best to use for "InvokeRequired" part in the "eScan_Callback()" function.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
GANDA1F
  • 307
  • 1
  • 4
  • 13

2 Answers2

1

InvokeRequired is to check if the current thread can safely access winforms controls. If you're converting it to a console application, you can probably just replace this with the standard method call in the 'else' block.

However, it looks like this is doing some threading things, so rather than a copy & paste job, I would look at what this form is doing overall, and rewrite it in a more 'console-y' way.

thecoop
  • 45,220
  • 19
  • 132
  • 189
0

Reading that your making a console application I don't think you need this code, just use: eScan_Callback_safe(command, Data);

ikwillem
  • 1,044
  • 1
  • 12
  • 24