Auto Connect SQLConnection
-
It uses System.Threading.Task. Is it possible if I want to use it with backgroundworker?
-
It uses System.Threading.Task. Is it possible if I want to use it with backgroundworker?
-
No idea, but I suggest you read the documentation, and try a few experiments.
Veni, vidi, abiit domum
Oh ok ok. There are no example of code inside but I will try googling. Thank you very much. :)
-
It uses System.Threading.Task. Is it possible if I want to use it with backgroundworker?
Why would you want to use BackgroundWorker when the Task is a better choice?
Chill _Maxxx_
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier -
Why would you want to use BackgroundWorker when the Task is a better choice?
Chill _Maxxx_
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easierThank you very much, I'm new to task and just found it today. But I'll keep searching.
After calling OpenAsync, State must return Connecting until the returned Task is completed. Then, if the connection was successful, State must return Open. If the connection fails, State must return Closed.
Sorry that english is not my first language and I'm not that good in programming. So what I understand above is like this.
this.cnn.OpenASync();
if (this.cnn.State == ConnectionState.Connecting)
{
//cnn is busy connecting
}Then if it's busy, then I will return; instead of OpenASync() again.
if (this.cnn.State == ConnectionState.Closed)
{
//cnn failed to open
}And if it's closed, then I assume cnn failed and finished processing. But it's not working. It's ok, I'll search about task. Thank you very much btw. :)
-
Why would you want to use BackgroundWorker when the Task is a better choice?
Chill _Maxxx_
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easierThis is what I do,
static async Task Method(SqlConnection cnn)
{
await cnn.OpenAsync();
return cnn.State;
}private void SQLClientLoader_Load(object sender, EventArgs e)
{
do
{
this.cnn = new SqlConnection(this.par.Constr);try { ConnectionState cst = Method(cnn).Result; if (cst == ConnectionState.Open) { this.par.sqSuccess = true; } else { } } catch (Exception ex) { this.par.sqSuccess = false; this.par.Exception = ex.Message; } finally { } } while ((bool)this.par.sqSuccess != true); }
It freeze my application everytime I this form load executed. Should I combine Task with BackgroundWorker to prevent freeze? Or maybe something is wrong in my code? Can you explain to me cause I'm new and confuse of this.
-
This is what I do,
static async Task Method(SqlConnection cnn)
{
await cnn.OpenAsync();
return cnn.State;
}private void SQLClientLoader_Load(object sender, EventArgs e)
{
do
{
this.cnn = new SqlConnection(this.par.Constr);try { ConnectionState cst = Method(cnn).Result; if (cst == ConnectionState.Open) { this.par.sqSuccess = true; } else { } } catch (Exception ex) { this.par.sqSuccess = false; this.par.Exception = ex.Message; } finally { } } while ((bool)this.par.sqSuccess != true); }
It freeze my application everytime I this form load executed. Should I combine Task with BackgroundWorker to prevent freeze? Or maybe something is wrong in my code? Can you explain to me cause I'm new and confuse of this.
I'm not surprised your app is freezing, you have a tight loop here that's hogging the CPU. In the Load event, you could wrap the while loop inside a Task so that the rest of the app can start up. If, however, you need the database connection immediately, then you have to block.
Chill _Maxxx_
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier -
I'm not surprised your app is freezing, you have a tight loop here that's hogging the CPU. In the Load event, you could wrap the while loop inside a Task so that the rest of the app can start up. If, however, you need the database connection immediately, then you have to block.
Chill _Maxxx_
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easierThank you very much for your instruction, This is what I done.
private void SQLClientLoader_Load(object sender, EventArgs e)
{
this.GetSQLState();
}async private System.Threading.Tasks.Task GetStateAsync()
{
do
{
this.cts = new CancellationTokenSource();
this.cnn = new SqlConnection(this.par.Constr);try { await Task.Delay(2500); await this.cnn.OpenAsync(cts.Token); } catch (Exception ex) { cts.Cancel(false); this.cnn.Dispose(); this.cts.Dispose(); } } while (this.cnn.State != ConnectionState.Open); return this.cnn.State; }
async private void GetSQLState()
{if (await GetStateAsync() == ConnectionState.Open) { this.Close(); } await Task.Delay(2500); }
It's working. I used cancellation token, but why the OpenAsync() is not cancelled and still queue up? Edit : Found my solution. Clear the connection pool, made my day guys. Thank you helping me. :laugh:
-
No idea, but I suggest you read the documentation, and try a few experiments.
Veni, vidi, abiit domum
Oh cmon, I tried a lot of example and still not working. I'm really really stuck here. I'm not asking someone to do the code for me, at least tell me what is wrong in my code. Please :(
-
Oh cmon, I tried a lot of example and still not working. I'm really really stuck here. I'm not asking someone to do the code for me, at least tell me what is wrong in my code. Please :(
Midnight Ahri wrote:
I tried a lot of example and still not working.
So you want us to guess a) what your code is doing, and b) what errors occur.
Midnight Ahri wrote:
at least tell me what is wrong in my code.
As I said before, I have no idea. However, re-reading your original message, it would seem much more sensible to post a message to the user when the connection fails. At least then someone can phone the help desk and alert them of the problem.
Veni, vidi, abiit domum
-
Midnight Ahri wrote:
I tried a lot of example and still not working.
So you want us to guess a) what your code is doing, and b) what errors occur.
Midnight Ahri wrote:
at least tell me what is wrong in my code.
As I said before, I have no idea. However, re-reading your original message, it would seem much more sensible to post a message to the user when the connection fails. At least then someone can phone the help desk and alert them of the problem.
Veni, vidi, abiit domum
Richard MacCutchan wrote:
So you want us to guess a) what your code is doing, and b) what errors occur.
I've explained everything about my code. I'm just asking about why the sql queue up. By the way, thank you very much. At least you teach me async and await technique. And I appreciate that. I'm not good at programming, so maybe I'll ask you something again. See ya. :thumbsup: