I'm using an assembly reference dll in c# that controls some network devices! when I try to connect network device, the user interface halts till connection establish! To solving this problem I did use threads/ async-await (.Net Framework 4.0) but the problem is still not solved...
here is my code (using thread):
public void startListening(string IP, string Port)
{
if (!ThreadBusy)
{
if (IP.Trim() == "" || Port.Trim() == "") return;
Conn = new Thread(() => start(IP, Port));
Conn.Start();
ThreadBusy = true;
}
}
private void start(string IP, string Port)
{
if (!bIsConnected && !isListening)
{
//This Line Will Make UI Freez...
bIsConnected = axCZKEM1.Connect_Net(IP, Convert.ToInt32(Port));
if (bIsConnected == true)
{
iMachineNumber = 1;
deviceType = axCZKEM1.IsTFTMachine(iMachineNumber) ? 1 : 2;
this.IP = IP;
this.isListening = true;
if (axCZKEM1.RegEvent(iMachineNumber, 65535))
{
switch (deviceType)
{
case 1:
EnableTFTEvents();
break;
case 2:
EnableBWEvents();
break;
case 3:
//---
break;
}
ConnectionFailedArgs args = new ConnectionFailedArgs();
args.IP = IP;
OnConnectionSucceed(args);
}
}
else
{
ConnectionFailedArgs args = new ConnectionFailedArgs();
args.IP = IP;
OnConnectionFailed(args);
}
ThreadBusy = false;
}
}
I want to know what happening on UI while using this thread?
I Upgrade my VS from 2010 to 2012 (.Net Framework 4.5) but The problem is still not solved!
Here is my code (using async-await) :
public async void startListening(string IP, string Port)
{
if (!ThreadBusy)
{
if (IP.Trim() == "" || Port.Trim() == "") return;
//Conn = new Thread(() => start(IP, Port));
//Conn.Start();
ThreadBusy = true;
await Task.Run(() => start(IP,Port));
}
}
private async Task start(string IP, string Port)
{
if (!bIsConnected && !isListening)
{
//This Line Will Make UI Freez...
bIsConnected = axCZKEM1.Connect_Net(IP, Convert.ToInt32(Port));
if (bIsConnected == true)
{
deviceType = axCZKEM1.IsTFTMachine(iMachineNumber) ? 1 : 2;
this.IP = IP;
this.isListening = true;
if (axCZKEM1.RegEvent(iMachineNumber, 65535))
switch (deviceType)
{
case 1:
EnableTFTEvents();
break;
case 2:
EnableBWEvents();
break;
case 3:
//---
break;
}
ConnectionFailedArgs args = new ConnectionFailedArgs();
args.IP = IP;
OnConnectionSucceed(args);
}
}
else
{
ConnectionFailedArgs args = new ConnectionFailedArgs();
args.IP = IP;
OnConnectionFailed(args);
}
ThreadBusy = false;
}
}