|
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.
|
|
|
|
|
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
|
|
|
|
|
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.
|
|
|
|
|
Why would you want to use BackgroundWorker when the Task is a better choice?
|
|
|
|
|
Thank 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)
{
}
Then if it's busy, then I will return; instead of OpenASync() again.
if (this.cnn.State == ConnectionState.Closed)
{
}
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.
|
|
|
|
|
This is what I do,
static async Task<ConnectionState> 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.
|
|
|
|
|
Thank 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<ConnectionState> 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.
modified 13-Sep-13 21:43pm.
|
|
|
|
|
I need to Install SQL Server 2008 R2 Express Edition without the user interaction.
Basically i want to run my .msi setup along with SQL express . the user just will click once to run and both of the setup files will be installed. Please guide me ...
|
|
|
|
|
This is not a C# issue, please try a more appropriate forum.
Veni, vidi, abiit domum
|
|
|
|
|
|
No you don't, that won't happen, and the user probably wouldn't want it to happen anyway.
The user may already have SQL Server installed (perhaps a full version), on this machine or another. Or the user may want to install your software after a newer version of SQL Server is released.
Just state in the documentation that SQL Server is required and that it's up to the user to ensure that it is available.
Perhaps include a link to the Express download for users who don't already have it installed.
|
|
|
|
|
i need something like MSDE that we have for sql server 2000. in which user just click once. User don't know which options should be selected and he will just install the application and my msi package will include both of these. but when it will run the setup for sql express it require the options to be selected. but if the user is a common person and does not know more about computer then he will just require me to provide a once click setup.
|
|
|
|
|
WaseemAmin wrote: if the user is a common person
That's a big if. Better not assume your users are idiots.
Additionally, SQL Server CE[^] may be a better fit.
|
|
|
|
|
Although I'm still a newbie, I think I can safely say I'm an intermediate newbie probably really close of crossing the threshold of an advanced newbie…
I understand most things about inheritance, encapsulation, constructors, methods and parameters, properties, accessors and access modifiers (keep in mind I said, "most things")…
I know I still have a long way to go, but…
readonly
Was this created as some sort of safety and precautionary practice?
If we don't want something to change, we can use:
const
Or properties utilizing
protected or even
private
.Net I realize is always evolving, because of this… Is this the reason for the "readonly" keyword?
|
|
|
|
|
There's a subtle difference between readonly and const which it sort of emulates. If you like, you can think of readonly as a runtime constant as opposed to const which is a compile time constant. What does this mean? Well, a readonly field can be set in a constructor, so the value that is held in the readonly field is made constant once the class is initialised, as opposed to const where it's constant from the start. In other words, you can change the value in a readonly field at runtime, but you can't with a const field. A practical example might help. The first class defines the ANSWER, which can never change.
public class H2G2
{
private const int Answer = 42;
public H2G2()
{
Console.WriteLine("The answer to Life, the Universe, Everything is {0}", Answer);
}
} A couple of things to notice in this example. The first is that you can't change the value of Answer while the application is running. The second thing (unsaid), is that you can't make the const static. Okay, now here's a version of readonly.
public class TaxCalc
{
private readonly double taxRate;
public TaxCalc(double taxRate)
{
this.taxRate = taxRate;
}
public void Calculate(double value)
{
double total = value * taxRate;
Console.WriteLine("The total is {0}", total);
}
}
|
|
|
|
|
Pete O'Hanlon wrote: There's a subtle difference between readonly and const which it sort of emulates. If you like, you can think of readonly as a runtime constant as opposed to const which is a compile time constant.
Would that also mean, like a struct… Once it goes out of scope, it gets destroyed?
|
|
|
|
|
Yes. It exists per instance, so once the instance is out of scope, the readonly is out of scope.
|
|
|
|
|
|
readonly exists solely to prevent anyone from, either accidentally or intentionally, changing the value of a variable once it's set.
It is enforced at run-time.
|
|
|
|
|
In addition to the answers you have already been given, you can't create const s of your own class es/struct s but you can create static readonly instances which are commonly used to serve much the same purpose.
|
|
|
|
|
hi there
i have an issue
i have a record that is moving next each time timer interval started >>> also i have a delay button that it must delay a specific record for an interval let say(2 min)
after two minutes its displayed the rest of records in datetable
my idea is >>>
when delay button clicked i move the current record to another datatabbe and after 2 min i restore it back to the original datatable but unfortunately i get an error
"that deleted records cant be inserted " ? even if i refreshed my datasource
not i am using timers
can some one help or give me another idea or a solution
here is my code
delay button
<pre lang="cs">int x;
x = Convert.ToInt16(pationt_noLabel1.Text);
var rows = dt.Select("pationt_no=" + x);
foreach (var r in rows)
{
delayed.ImportRow(r);
r.Delete();
}
var rowss=delayed.Select("pationt_no="+x);
foreach (var row in rowss)
{
row["test_time"] = DateTime.Now.ToString("hh:mm");
}
testsBindingSource.DataSource = dt;
testsBindingSource.MoveNext();
and my delay method
testsBindingSource.EndEdit();
string time1s = DateTime.Now.ToString("hh:mm");
if(delayed.Rows.Count==0)
return;
else
foreach (DataRow dr in delayed.Rows)
{
string time2s = dr["test_time"].ToString();
TimeSpan interval = DateTime.Parse(time2s).AddMinutes(delay_time) - DateTime.Parse(time1s);
double tot=interval.TotalMinutes;
if (tot >= 0)
{
dt.ImportRow(dr);
testsBindingSource.DataSource = dt;
dr.Delete();
MessageBox.Show(dt.Rows.Count.ToString());
}
else
{
}
}
and timer tick
testsBindingSource.DataSource = dt;
get_printed_tests(dt);
is_delayed(2);
|
|
|
|