I am trying to manage a thread bot situation. I want to add them in a dictionary for later access (shutdown, pause, pass variables) but I always get them as null even after the bot started (the null variable is temp_bot).
private static
Dictionary<string, Bot> BotsOnline = new Dictionary<string, Bot>();
Bot temp_bot;
new Thread(
() =>
{
int crashes = 0;
while (crashes < 1000)
{
try
{
temp_bot = new Bot(config, sMessage, ConvertStrDic(offeredItems),
ConvertStrDic(requestedItems), config.ApiKey,
(Bot bot, SteamID sid) =>
{
return (SteamBot.UserHandler)System.Activator.CreateInstance(
Type.GetType(bot.BotControlClass), new object[] { bot, sid });
},
false);
}
catch (Exception e)
{
Console.WriteLine("Error With Bot: " + e);
crashes++;
}
}
}).Start();
//wait for bot to login
while (temp_bot == null || !temp_bot.IsLoggedIn)
{
Thread.Sleep(1000);
}
//add bot to dictionary
if (temp_bot.IsLoggedIn)
{
BOTSdictionary.Add(username, temp_bot);
}
The new thread doesn't know about the temp_bot object as you need to pass it in to the lambda expression. Try:
new Thread(
(temp_bot) =>
{
int crashes = 0;
while (crashes < 1000)
{
try
{
temp_bot = new Bot(config, sMessage, ConvertStrDic(offeredItems),
ConvertStrDic(requestedItems), config.ApiKey,
(Bot bot, SteamID sid) =>
{
return (SteamBot.UserHandler)System.Activator.CreateInstance(
Type.GetType(bot.BotControlClass), new object[] { bot, sid });
},
false);
}
catch (Exception e)
{
Console.WriteLine("Error With Bot: " + e);
crashes++;
}
}
}
).Start();
Related
I have created a Windows forms application which able to publish OPC tags into an MQTT broker, Now I'm trying to do the reverse, write MQTT tags into an OPC server. when I have started both the agent (the publisher) and the transfer (the subscriber) the two threads do the same work which is publishing I don't know what is the problem.
The start method is below :
public byte Start()
{
try
{
byte connectResult;
if (IsLWT == false)
{
connectResult = this._MqttClient.Connect(ClientID, Username, Password, IsCleanSession, KeepAlivePeriode);
}
else
{
connectResult = this._MqttClient.Connect(ClientID, Username, Password, willRetain, willQos, true, willTopic, willMessage, IsCleanSession, KeepAlivePeriode);
}
// 0 means that the connection suceeded
if (connectResult == 0)
{
this.Rate = GetRateFromOPCGroups();
this._publisherThread = new Thread(() => Publish());
this._publisherThread.IsBackground = true;
this._publisherThread.Start();
IsStarted = true;
}
if (connectResult == 0)
{
//this.Rate = GetRateFromOPCGroups();
this._SubscriberThread = new Thread(() => Subscribe(topics));
this._SubscriberThread.IsBackground = true;
this._SubscriberThread.Start();
IsStarted = true;
}
return connectResult;
}
catch (IntegrationObjects.Networking.M2Mqtt.Exceptions.MqttClientException ex)
{
MQTTServiceLogger.TraceLog(MessageType.Error, MQTTServiceMessages.startAgentFailed(this.Name,ex.Message));
return 11;
}
catch (IntegrationObjects.Networking.M2Mqtt.Exceptions.MqttCommunicationException ex)
{
MQTTServiceLogger.TraceLog(MessageType.Error, MQTTServiceMessages.startAgentFailed(this.Name, ex.Message));
return 11;
}
catch (Exception ex)
{
MQTTServiceLogger.TraceLog(MessageType.Error, MQTTServiceMessages.startAgentFailed(this.Name, ex.Message));
return 11;
}
}
1.This is the publish code:
private void Publish()
{
while (true)
{
if (IsStarted)
{
try
{
if (_MqttClient.IsConnected)
{
isConnected = true;
if (this.OPCItems.Count != 0)
{
JsonMQTTMessage JsonMessage = new JsonMQTTMessage();
JsonMessage.Timestamp = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff");
JsonMessage.ListofValues = new List<UpdatedOPCItem>();
lock (lockOPCItems)
{
foreach (OPCItem Item in OPCItems.ToList())
{
if (Item != null)
{
UpdatedOPCItem upItem = new UpdatedOPCItem();
upItem.ID = Item.ItemID;
upItem.value = Item.ItemCurrentValue;
upItem.quality = Item.ItemQuality;
upItem.timestamp = Item.ItemTimeStamp.ToString("yyyy/MM/dd HH:mm:ss.fff");
upItem.DataType = Item.ItemDataType;
JsonMessage.ListofValues.Add(upItem);
}
}
}
var messageTopublish = Newtonsoft.Json.JsonConvert.SerializeObject(JsonMessage);
ushort res = _MqttClient.Publish(Topic, Encoding.UTF8.GetBytes(messageTopublish), Qos, Retain);
ResetValues();
Thread.Sleep(Rate);
This is the Subscribe code:
public void Subscribe(List topics)
{
while (true)
{
if (IsStarted)
{
try
{
if (_MqttClient.IsConnected)
{
isConnected = true;
foreach (string topic in topics)
{
ushort msggId = _MqttClient.Subscribe(new string[] { $"{ topic }" },
new byte[] { MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE });
}
Thread.Sleep(Rate);
}
else
Hi guy's i'm trying to figure out a way to display a DisplayAlert Popup when Someone entered the Wrong details into the Login Process.
Issue is, The login's a little "Weird" I'm not comparing against a Database i'm comparing against a List of Customers I get from a APi So I have to loop threw to then find if the detail's are correct.
I have a login Phase 1() which Checks against the Wordpress API but I want to have this be able to notify on Phase 1 and 2, Phase 1 is a Username = Username Password = Password Where phase 2 is just a Username = Username , I know it's not secure The Login is more of a Formality then anything.
public async void Login_Phase1()
{
try
{
#region Login Phase 1
var client = new WordPressClient("http://XXX.co.za/wp-json/");
client.AuthMethod = AuthMethod.JWT;
try
{
await client.RequestJWToken(Usernamelabel.Text, Password.Text);
}
catch (Exception e)
{
await App.Current.MainPage.DisplayAlert("Something Went wrong", e.ToString(), "OK");
}
var x = client;
var isValidToken = await client.IsValidJWToken();
WpApiCredentials.token = client.GetToken();
if (isValidToken)
{
Login_Phase2();
}
else
{
await App.Current.MainPage.DisplayAlert("Detail's are Incorect", "Token not Found", "OK");
}
}
catch (Exception ex)
{
Crashes.TrackError(ex);
}
#endregion
}
public void Login_Phase2()
{
try
{
#region login Phase 2
var list = FetchCustomers.customers.ToList();
foreach (var user in list)
{
if (user.username == Usernamelabel.Text)
{
Preferences.Set("CId", user.id.ToString());
Preferences.Set("Token", WpApiCredentials.token);
Preferences.Set("CUsername", user.username);
Preferences.Set("CEmail", user.email);
Users.Loggedin = true;
Application.Current.SavePropertiesAsync();
App.Current.MainPage.DisplayAlert("Complete", "Login Process Complete, Enjoy", "OK");
App.Current.MainPage = new Home("Mica Market");
}
//Want to add a Display popup Here to say the information is entered incorrectly, but
not have it repeat 200 Time's
}
}
catch (Exception ex)
{
Crashes.TrackError(ex);
}
#endregion
}
Fetching all the Customers to check against
private async void ExtractWooData(List<Customer> x)
{
try
{
#region FetchC
RestAPI rest = new RestAPI("http://xxxxxx/wp-json/wc/v3/", "ck_a25xxxxxxxxxxx0", "cs_8xxxxxxxx8xxxx");
WCObject wc = new WCObject(rest);
int pageNum = 1;
var isNull = false;
List<Customer> oldlist;
while (!isNull)
{
var page = pageNum.ToString();
x = await wc.Customer.GetAll(new Dictionary<string, string>() {
{
"page", page
}, {
"per_page", "100"
}
});
oldlist = FetchCustomers.customers ?? new List<Customer>();
if (x.Count == 0)
{
break;
}
else
{
//1st loop customers needs to = 100
//2nd loop oldist needs to = 40+
//If count = 0 then => Combine Customers + Oldist
pageNum++;
FetchCustomers.customers = oldlist.Union(x).ToList();
}
}
}
catch (Exception ex)
{
Crashes.TrackError(ex);
}
#endregion
}
Any Advice?
you can replace the foreach with a LINQ query
// find the first match
var found = list.Where(user => user.username == Usernamelabel.Text).FirstOrDefault();
if (found != null)
{
// set preferences and navigation
}
else
{
// DisplayAlert
}
I would like to cancel a Task.run in a clean and simple way here is my code:
bool NTAG_isHere = false;
// CODE (...)
private async Task Dump_NTAG(object sender, EventArgs eventArgs)
{
// while NTAG PRESENT DUMP START:
await Task.Run(() => {while (NTAG_isHere()) { } });
// CODE (...)
// If NTAG NOT PRESENT or not detected stop the dump and kill thread:
if(!NTAG_isHere)
{
// kill the thread
}
{
Thank you,
Edit 4, my complete method :
private void Dump_NTAG(object sender, EventArgs eventArgs)
{
try
{
richTextBox_debug.Invoke(new UpdateTextCallback(CleanText), new object[] {string.Empty});
WriteLine(NTAG_isHere());
if (!NTAG_isHere())
{
Task.Factory.StartNew(() =>
{
richTextBox_debug.Invoke(new UpdateTextCallback(CleanText), new object[] { string.Empty });
byte[] dump = new byte[540];
int i;
richTextBox_debug.Invoke(new UpdateTextCallback(UpdateText), new object[] { "START" + Environment.NewLine + Environment.NewLine });
for (i = 0; i < 135; i++)
{
string Result = arduino.SendCommand("/READ " + i);
string[] SplitResult = Result.Split('/', ' ');
if (SplitResult.Length > 1)
{
if (Result.Split('/', ' ')[1] == "ERROR")
richTextBox_debug.Invoke(new UpdateTextCallback(UpdateText), new object[] { string.Format("NFC_Error" + Result.Substring(1) + Environment.NewLine) });
else
richTextBox_debug.Invoke(new UpdateTextCallback(UpdateText), new object[] { string.Format("NFC_Unknown_Response" + Result + Environment.NewLine) });
i = 135;
}
else
{
string page = "Page";
richTextBox_debug.Invoke(new UpdateTextCallback(UpdateText), new object[] { string.Format(page + " {0} : {1}", i, Result) + Environment.NewLine });
}
}
if (i == 135) { richTextBox_debug.Invoke(new UpdateTextCallback(UpdateText), new object[] { "Dump success !" + Environment.NewLine + Environment.NewLine }); }
arduino.SendCommand("/NTAG_HALT");
arduino.Close();
});
}
else
{
MessageBox.Show("ERROR !!");
}
}
catch (Exception e)
{
WriteLine(e.Message);
}
}
my concern is that i can no longer dump, the boolean would still be wrong with this new code, but my method NTAG_isHere () returns good value, so is not in question. and that never do I pass in MessageBox.Show ("ERROR !!"); with or without ntag on the player
looking at your code now , you can simply do this , there is no need of cancellation thing , just check the method returns true than run task else dont
private void Read_amiibo(object sender, EventArgs eventArgs)
{
bool ishere = NTAG_isHere();
//store reference of local thread
if(ishere)
{
Task.Factory.StartNew(() =>
{
//do operation
}
}
else {
//exit
}
}
one point to add here : if you make use of async/await your call get return when await encounters and code below will not get executed
Below solution will work in case when there is no async/await
make use of CancellationTokenSource with task that will do for you, CancellationToken you can pass as argument to run method and monitor it for cancellation.
This is cleaner way given by Microsoft team.
var tokenSource = new CancellationTokenSource();
var token = tokenSource.Token;
var t = Task.Run( () => {
//perform task
if (token.IsCancellationRequested)
token.ThrowIfCancellationRequested();
} , token);
if(!NTAG_isHere)
{
// kill the thread
tokenSource.Cancel();
}
other way not much recommended ,
//store reference of local thread
Thread threadToCancel = null;
Task.Factory.StartNew(() =>
{
//Capture the thread
threadToCancel = Thread.CurrentThread;
}
//you can put timer also , means check value after some time to avoid while loop
while(true) {
if(NTAG_isHere){
threadToCancel.Abort();
}
}
Discussed this approach here : Abort/Cancel Task
reference check here : Task.Run Method (Action, CancellationToken)
This is killing me. I'm new to android/Xamarin. I can't find anything on the web that explains what is going on here.
I'm using xamarin forms for my application. I have a page that synchronizes the device with a web service.
The method simply retrieves 100 records at a time from the web service and updates a sqlite table on the device. I randomly get this error. I'm running 5000 records for my test sample.
Here is the button click:
public async void OnSyncCachedData_Clicked(object sender, EventArgs e)
{
activityIndicatorAll.IsRunning = true;
try
{
actIndSyncItems.IsRunning = true;
SyncAllFinished += SynchAllFinishedProcessing;
await Task.Run(async () => await App.ItemsRepo.LoadCacheDataFromCacheAsync(DbPath).ConfigureAwait(false));
}
catch (BAL.Exceptions.NetworkException nex)
{
await DisplayAlert(Messages.TitleError, nex.Message, Messages.MsgOk);
}
catch (Exception ex)
{
await DisplayAlert(Messages.TitleError, string.Format(Messages.MsgAreYouConnectedParm1, ex.Message), Messages.MsgOk);
}
finally
{
EventHandler handler = SyncAllFinished;
if (handler != null)
{
handler(this, new EventArgs());
}
SyncAllFinished -= SynchAllFinishedProcessing;
}
}
the main worker method:
public async Task<bool> LoadCacheDataFromCacheAsync(string dbPath)
{
WebSvcManagers.ItemsManager itemsWebServiceManager = new WebSvcManagers.ItemsManager();
List<Models.WebServiceItems> consumedRecords = new List<Models.WebServiceItems>() { };
int bufferSize = 100;
Log.Debug(TAG, "LoadCacheDataFromCacheAsync starting");
try
{
{
int lastID = 0;
IEnumerable<Models.WebServiceItems> remoteRecords = await BAL.DataAccessHelper.GetItemsFromGPCacheAsync(App.Login, lastID, bufferSize, itemsWebServiceManager).ConfigureAwait(false);
while (remoteRecords.Count() != 0)
{
foreach (Models.WebServiceItems remoteItem in remoteRecords)
{
// DbActionTypes dbAction = (DbActionTypes)remoteItem.DbAction;
Models.Items itemRecord = new Models.Items() { ItemNumber = remoteItem.ItemNumber.ToUpper().Trim(), Description = remoteItem.Description.Trim() };
Log.Debug(TAG, "Processing {0}", remoteItem.ItemNumber.Trim());
bool success = await AddRecordAsync(itemRecord).ConfigureAwait(false);
if (success)
consumedRecords.Add(remoteItem);
}
lastID = remoteRecords.Max(r => r.RecordID) + 1;
remoteRecords = await BAL.DataAccessHelper.GetItemsFromGPCacheAsync(App.Login, lastID, bufferSize, itemsWebServiceManager).ConfigureAwait(false);
}
}
// await UpdateConsumedRecords(consumedRecords).ConfigureAwait(false);
return true;
}
catch (Exception ex)
{
this.StatusMessage = ex.Message;
Log.Debug(TAG, "Error Catch: {0}", StatusMessage);
return false;
}
finally
{
itemsWebServiceManager = null;
HandleSyncFinished(this, new EventArgs());
SyncAllFinished -= HandleSyncFinished;
}
}
My simple webservice manager:
public static async Task<IEnumerable<Models.WebServiceItems>> GetItemsFromGPCacheAsync(Models.Login login, int offset, int bufferCount, WebSvcManagers.ItemsManager manager)
{
try
{
return await manager.GetCacheRecordsAsync(login, offset, bufferCount).ConfigureAwait(false);
}
catch (Exception)
{
throw;
}
}
And the code for the interaction with the web service:
const int bufferSize = 100;
public async Task<IEnumerable<Models.WebServiceItems>> GetCacheRecordsAsync(Models.Login login, int offSet, int bufferCount)
{
string deviceID = App.ConfigSettings.DeviceID.ToString("D");
try
{
///* Testing start */
//return await DataStore(bufferCount, offSet).ConfigureAwait(false);
///* Testing end */
if (!App.IsConnected)
throw new BAL.Exceptions.NetworkException(Messages.ExceptionNetworkConnection);
string user = login.UserName;
string password = login.Password;
HttpClient client = HttpClientExtensions.CreateHttpClient(user, password);
try
{
List<Models.WebServiceItems> items = new List<Models.WebServiceItems>() { };
int lastID = offSet;
int i = 0;
string uri = string.Format("{0}", string.Format(Messages.WebRequestItemsCacheParms3, deviceID, lastID, Math.Min(bufferCount, bufferSize)));
Log.Debug(TAG, string.Format("Webservice {0}", uri));
string response = await client.GetStringAsync(uri).ConfigureAwait(false);
while (i < bufferCount && response != null && response != "[]")
{
while (response != null && response != "[]")
{
dynamic array = JsonConvert.DeserializeObject(response);
foreach (var item in array)
{
i++;
items.Add(new Models.WebServiceItems()
{
ItemNumber = item["ITEMNMBR"].Value.Trim(),
Description = item["ITEMDESC"].Value.Trim(),
DbAction = (int)(item["DbAction"].Value),
RecordID = (int)(item["DEX_ROW_ID"].Value),
});
lastID = (int)(item["DEX_ROW_ID"].Value);
Log.Debug(TAG, string.Format("Webservice {0}", item["ITEMNMBR"].Value.Trim()));
}
if (i < Math.Min(bufferCount, bufferSize))
{
uri = string.Format("{0}", string.Format(Messages.WebRequestItemsCacheParms3, deviceID, lastID + 1, Math.Min(bufferCount, bufferSize)));
Log.Debug(TAG, string.Format("Webservice {0}", uri));
response = await client.GetStringAsync(uri).ConfigureAwait(false);
}
else
break;
}
}
Log.Debug(TAG, string.Format("Webservice return {0} items", items.Count()));
return items;
}
catch (Exception ex)
{
Log.Debug(TAG, "Error Catch: {0}", ex.Message);
throw ex;
}
}
catch (System.Net.Http.HttpRequestException nex)
{
throw new Exception(string.Format(Messages.ExceptionWebServiceLoginParm1, nex.Message));
}
catch (Exception)
{
throw;
}
}
I've had assistance from a Xamarin instructor and we thought we got it, (because this is random), but it clearly is not swatted. I've been hitting my head up against a wall for over 3 weeks on this. It smells like a memory leak, but I have no idea how to find it with such a generic error message that doesn't appear on web searches.
Somebody out there with some major brains, Please help!
Error:
08-03 12:41:11.281 D/X:ItemsRepositiory(16980): UpdateRecordAsync 65702-40710
08-03 12:41:11.306 D/X:ItemsManager(16980): Webservice DeleteCacheRecordAsync 20497
08-03 12:41:11.406 D/X:ItemsManager(16980): Webservice api/InventoryItems?DeviceID=7c5bb45d-2ea0-45b9-ae50-92f2e25a2983&OffSet=20498&Max=100&cached=true
Thread finished: <Thread Pool> #7 08-03 12:41:11.521 E/art (16980): Nested signal detected - original signal being reported The thread 'Unknown' (0x7) has exited with code 0 (0x0).
This might be a problem with the VS Android Emulator. As we have shown in tests, this is not reproducible on the Google Android Emulators, nor on Xamarin Android Player, nor on a physical Android device.
I want to make my WinForms-App to use the SingleSign-On (SSO) feature with Microsoft Accounts.
I created a LiveApp and I'm able to Login to my App with the LiveSDK 5.4.
But everytime I click on my Login-Button the permissions list appears and I need to accept it again.
This is my code:
private const string ClientID = "{MY_CLIENT_ID}";
private LiveAuthClient liveAuthClient;
private LiveConnectClient liveConnectClient;
string[] scopes = new string[] { "wl.offline_access", "wl.emails", "wl.signin" };
private void buttonLogin_Click(object sender, EventArgs e)
{
liveAuthClient = new LiveAuthClient(ClientID);
webBrowser1.Navigate(liveAuthClient.GetLoginUrl(scopes));
}
private async void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
if (this.webBrowser1.Url.AbsoluteUri.StartsWith("https://login.live.com/oauth20_desktop.srf"))
{
AuthResult authResult = new AuthResult(this.webBrowser1.Url);
if (authResult.AuthorizeCode != null)
{
try
{
LiveConnectSession session = await liveAuthClient.ExchangeAuthCodeAsync(authResult.AuthorizeCode);
this.liveConnectClient = new LiveConnectClient(session);
LiveOperationResult meRs = await this.liveConnectClient.GetAsync("me");
dynamic meData = meRs.Result;
if(string.Equals(meData.emails.account, MyAppUser.EmailAddress))
MessageBox.Show("Successful login: " + meData.name);
}
catch (LiveAuthException aex)
{
MessageBox.Show("Failed to retrieve access token. Error: " + aex.Message);
}
catch (LiveConnectException cex)
{
MessageBox.Show("Failed to retrieve the user's data. Error: " + cex.Message);
}
}
else
{
MessageBox.Show(string.Format("Error received. Error: {0} Detail: {1}", authResult.ErrorCode, authResult.ErrorDescription));
}
}
}
What I need to change? I don't want the User to accept the permissions on each login.
You can use IRefreshTokenHandler to save token as following example:
public class RefreshTokenHandler : IRefreshTokenHandler
{
private string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\oneDrive\\RefreshTokenHandler\\RefreshTokenInfo.RefreshToken-me";
public Task<RefreshTokenInfo> RetrieveRefreshTokenAsync()
{
return Task.Factory.StartNew<RefreshTokenInfo>(() =>
{
if (File.Exists(path))
{
return new RefreshTokenInfo(File.ReadAllText(path));
}
return null;
});
}
public Task SaveRefreshTokenAsync(RefreshTokenInfo tokenInfo)
{
// Note:
// 1) In order to receive refresh token, wl.offline_access scope is needed.
// 2) Alternatively, we can persist the refresh token.
return Task.Factory.StartNew(() =>
{
if (File.Exists(path)) File.Delete(path);
if (!Directory.Exists(Path.GetDirectoryName(path))) Directory.CreateDirectory(Path.GetDirectoryName(path));
File.AppendAllText(path, tokenInfo.RefreshToken);
});
}
}
after that you get session as following:
RefreshTokenHandler handler = new RefreshTokenHandler();
liveAuthClient = new LiveAuthClient(ClientID, handler);
var Session = liveAuthClient.InitializeAsync(scopes).Result.Session;
if (Session == null)
{
webBrowser1.Navigate(liveAuthClient.GetLoginUrl(scopes));
}
else
{
try
{
this.liveConnectClient = new LiveConnectClient(Session);
LiveOperationResult meRs = await this.liveConnectClient.GetAsync("me");
dynamic meData = meRs.Result;
if (string.Equals(meData.emails.account, MyAppUser.EmailAddress))
MessageBox.Show("Successful login: " + meData.name);
}
catch (LiveAuthException aex)
{
MessageBox.Show("Failed to retrieve access token. Error: " + aex.Message);
}
catch (LiveConnectException cex)
{
MessageBox.Show("Failed to retrieve the user's data. Error: " + cex.Message);
}
}
I've only used this API in Objective C, but I had to follow two steps.
Use the wl.offline_access scope. (Which you're already doing)
You only show the login screen if the sessions object is null. If your session object is already populated, you can proceed as you would if the sign in was successful.