我目前正在将 Windows 窗体应用程序转换为 C# 中的控制台应用程序。
我正忙于转换代码,并且遇到了一些我不确定如何正确转换的语法问题。这是 Windows 窗体应用程序中的原始代码:
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();
}
我正在使用 Action 在控制台应用程序中执行 BeginInvoke 部分。而且我想知道“eScan_Callback()”函数中的“InvokeRequired”部分最适合使用什么。