|
If you want "no dependencies", why are you creating PDF's?
Instead, create a plain text file; or an .RTF; or convert the PDF to XPS; or anything else that has "no dependencies" (outside of that provided by Windows and .NET).
If it's simply for "viewing in WinForms", then the format of the input should not be limited to only PDF's.
And why view in WinForms at all? Just "process start" whatever PDF viewer a user has installed (and probably prefers).
|
|
|
|
|
i am trying to understand async and await concept. so i am make sample program when i run this program take 8 seconds to execute if i execute without async and await it also take 8 second to execute. so what is wrong in this program.
protected async void btnSubmit_Click(object sender, EventArgs e)
{
Response.Write(DateTime.Now.ToString("dd-MM-yyyy:hh:mm:ss") + " <br/>");
//dowork();
await Task.Run(() => dowork());
// await Task.Run(() => DoAnotherWork());
DoAnotherWork();
Response.Write(DateTime.Now.ToString("dd-MM-yyyy:hh:mm:ss") + " <br/>");
// btnSubmit.Text="Done it";
}
void dowork()
{
Thread.Sleep(5000);
}
void DoAnotherWork()
{
Thread.Sleep(3000);
}
|
|
|
|
|
How long would you expect it to complete in? You have 8 seconds worth of operation in there. I assume that you think that dowork will happen at the same time as DoAnotherWork. That's not the way that await operates. It's not, despite what you may think, something that pushes an operation off to another thread and continues processing in that thread - instead, it suspends the operation of the current method until the awaited operation completes. So, you end up with an 8 second delay.
|
|
|
|
|
yes, i was thinking it should completed in 5 seconds. So please clarify to me await and async concept.
thanks your help
|
|
|
|
|
I just have clarified it for you. The await keyword suspends the method until the task it's waiting on completes, then it moves onto the next operation in the chain. I'm not sure how else I can put this for you.
|
|
|
|
|
thanks Peter for your help
|
|
|
|
|
Anil0007 wrote: i was thinking it should completed in 5 seconds Well, if you modify your code something like this, it "might" run in 5 seconds..
Task t0 = Task.Factory.StartNew(() => dowork());
Task t1 = Task.Factory.StartNew(() => DoAnotherWork());
await Task.WhenAll(t0, t1);
You have just been Sharapova'd.
|
|
|
|
|
Using await doesn't mean your code acts asynchronously. It still behaves as if the code is running synchronously (which is why it takes 8 seconds) but the difference is that when using await the underlying framework means the thread is available to use for other tasks if needed. If you didn't use await and instead just waited for the task to finish then your code would act no differently, but the thread that is waiting for the result can't be used for something else if needed. If you remove the "await" from the dowork then the method will take 3 seconds as dowork will be started on its own task and that will execute asynchronously and then DoAnotherWork will be called which will block for 3s synchronously.
dowork();
Task.Run(() => dowork());
await Task.Run(() => dowork());
|
|
|
|
|
It's not usually recommended to use Task.Run to convert a synchronous method to an async Task , especially if you're going to immediately await that Task :
Should I expose asynchronous wrappers for synchronous methods? | Stephen Toub[^]
Instead, it's better to push the asynchronous operation down, and make the method your calling return a Task .
Also, when you're using async , you should try to avoid Thread.Sleep ; use await Task.Delay(...) instead:
Visual C#: Thread.Sleep vs. Task.Delay[^]
And as Agent__007 said, if you want both methods to run in parallel, you need to use Task.WhenAll to wait for them.
protected async void btnSubmit_Click(object sender, EventArgs e)
{
Response.Write(DateTime.Now.ToString("dd-MM-yyyy:hh:mm:ss") + " <br/>");
Task first = dowork();
Task second = DoAnotherWork();
await Task.WhenAll(first, second);
Response.Write(DateTime.Now.ToString("dd-MM-yyyy:hh:mm:ss") + " <br/>");
}
async Task dowork()
{
await Task.Delay(5000);
}
async Task DoAnotherWork()
{
await Task.Delay(3000);
}
Since you're using ASP.NET WebForms, you should consider using the RegisterAsyncTask method[^] to run your async code, and avoid using async void where possible:
Using Asynchronous Methods in ASP.NET 4.5[^]
The Magic of using Asynchronous Methods in ASP.NET 4.5 plus an important gotcha[^]
Some useful resources:
Async and Await | Stephen Cleary[^]
Async/Await - Best Practices in Asynchronous Programming | Stephen Cleary[^]
Parallel Programming with .NET | MSDN Blogs[^]
await (C# Reference)[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hello!
sub-category table
category table
i am working on Point of sale application and i want to get sub-category data when i click on category tile view I tried alot but i couldn't find any good solution.
PLEASE guide me SO I CAN MOVE FORWARD .
|
|
|
|
|
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
So explain exactly what you have tried, where you are stuck, and what help you need.
Tell us what environment you are working in, what controls you are using, how you load data into them.
Without that, it's like trying to fix your car without even knowing where it is, much less what make or model it is, or what parts we need!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Why I can not save the error information in the table: TABERRORINFO. Suppose I have two: TABCUSTOMER and TABERRORINFO.
private void cmdSave_Click(object sender, EventArgs e)
{
strSQL = "INSERT INTO TABCUSTOMER(IDCUSTOMER, FULLNAME, ADDRESS, IMAGE) " +
"VALUES (@IDCUSTOME, @FULLNAME, @ADDRESS, @ImageData);";
SqlCommand cmd = new SqlCommand(strSQL, ClsConnection.objConnect);
try
{
cmd.Parameters.AddWithValue("@IDCUSTOMER", txtIDCustomer.Text);
cmd.Parameters.AddWithValue("@FULLNAME", txtFullName.Text);
cmd.Parameters.AddWithValue("@ADDRESS", txtAddress.Text);
//byte[] ImageData = ReadFile(sPath);
cmd.Parameters.Add(new SqlParameter("@ImageData", (object)ImageData));
if (ClsConnection.objConnect.State != System.Data.ConnectionState.Open)
{
ClsConnection.objConnect.Open();
}
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
ClsConnection.objConnect.Close();
}
catch (SqlException ex)
{
ClsConnection.objConnect.Close();
string sConError = "Save Messager Customer 1: " + ex.Message;
SaveMessageErrSQL(sConError, strSQL);
}
}
private void SaveMessageErrSQL(string MessErr, string SqlErr)
{
string sSQL = "UPDATE TABERRORINFO SET ";
sSQL = sSQL + " ERRDATE = @NgayErr";
sSQL = sSQL + ", ERRMESSAGE = @MesaErr";
sSQL = sSQL + ", ERRSQL = @SQLErr";
sSQL = sSQL + " WHERE USERNAME = N'" + sUSERNAME + "';";
Debug.Print(sSQL);
SqlCommand cmd = new SqlCommand(sSQL, objConnect);
try
{
cmd.Parameters.AddWithValue("@NgayErr", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
cmd.Parameters.AddWithValue("@MesaErr", MessErr);
cmd.Parameters.AddWithValue("@SQLErr", SqlErr);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
objConnect.Close();
DevExpress.XtraEditors.XtraMessageBox.Show("Saved" + MessErr);
}
catch (SqlException ex)
{<br />
objConnect.Close();
MessageBox.Show("No Save Message Errer into SQL: " + ex.Message);
}
}
|
|
|
|
|
You didn't post the error message so there's no way of knowing what err you actually encounter.
However, there are some issues with the code:
|
|
|
|
|
The follow I thought, you do not care about error codes because I intentionally create/caused the error by adding IMAGE and cmd.Parameters.Add(new SqlParameter("@ImageData", (object)ImageData)) to catch (SqlException ex) call SaveMessageErrSQL(...)
|
|
|
|
|
Okay, I see. But have you gone through and checked the 4 things I listed?
|
|
|
|
|
The follow your instructions I added this to
[CODE]
private void cmdSave_Click(object sender, EventArgs e)
{
…
try
{
...
int rowsAffected = cmd.ExecuteNonQuery();//Error here
if (rowsAffected != 1) {
// inform user etc...
}
// Debug I saw rowsAffected = 0
...
}
catch (SqlException ex) // Debug I saw ex {"Operand type clash: nvarchar is incompatible with image"} System.Data.SqlClient.SqlException
{
...
SaveMessageErrSQL(sConError, strSQL); // I wrote the wrong code this place
}
}
[/CODE]
Debug I saw :
I see the variable rowsAffected = 0
+ ex {"Operand type clash: nvarchar is incompatible with image"} System.Data.SqlClient.SqlException
what do I do now ?
|
|
|
|
|
Hi,
I have a user website, having pages Home-->Brands --> Properties --> Designs --> Objects. Each page having its own sets of selection value.
Using breadcrumb navigation (sitemap path) in master page, and all the pages of website uses this masterapge.
Problem: When there are multiple user logged in diffrent machines, the breadcrumb node value (for a user) changes according to the latest selected value, inside any page (by latest slection of any other user).
Example: User A clicked Home-->Brands(Accenture)-->Properties(Accentire Prop).... and in the same time if another User B clciked Home-->Brands(Samsung)-->Properties(Samsung Prop)....
Then navigation trail for user A changes(when he selects the Designs(Accenture Des)) to the latest selection by any other user (user B), i.e. Home-->Brands(Samsung)-->Properties(Samsung Prop) --> Designs (Accenture Des).
Please help why it happens.
CODE:-
IN WEB.SITEMAP:
<siteMapNode title="" description="">
<siteMapNode url="~/Secure/Home.aspx" title="HOME" description="" roles="*" >
<siteMapNode url="~/Secure/Properties.aspx" title="Properties" description="" roles="*" >
<siteMapNode url="~/Secure/Designs.aspx" title="Style Guides Designs" description="" roles="*" >
<siteMapNode url="~/Secure/Objects.aspx" title="Objects" description="" roles="*" />
</siteMapNode>
</siteMapNode>
</siteMapNode>
In WEB.CONFIG
<siteMap defaultProvider="XmlSiteMapProvider" enabled="true">
<providers>
<clear/>
<add name="XmlSiteMapProvider"
description="Default SiteMap provider."
type="System.Web.XmlSiteMapProvider"
siteMapFile="Web.sitemap" />
</providers>
</siteMap>
In Master page.master
<div id="SiteMapContainer" class="siteMapContainer" runat="server">
<asp:SiteMapPath ID="SiteMapPath1" runat="server" PathSeparator=" \ " PathDirection="RootToCurrent"
ParentLevelsDisplayed="10">
<CurrentNodeTemplate>
<%# Eval ("title") %>
</CurrentNodeTemplate>
<RootNodeTemplate>
<a href="Home.aspx">
<%# Eval ("title") %>
</a>
</RootNodeTemplate>
</asp:SiteMapPath>
</div>
In webpages(brands, Properties etc..):
if (SiteMap.CurrentNode != null)
{
SiteMapNode currentNode = SiteMap.CurrentNode;
currentNode.ParentNode.ReadOnly = true;
currentNode.ReadOnly = false;
currentNode.Title = _brand.BrandName ?? currentNode.Title.ToString();
currentNode.Url = BuildQueryString(currentNode.Key);
currentNode.ReadOnly = true;
currentNode.ParentNode.ReadOnly = false;
currentNode = currentNode.ParentNode;
currentNode.ReadOnly = false;
ItemSelectionLink.NavigateUrl = BuildQueryString(currentNode.Key);
currentNode.ReadOnly = true;
}
|
|
|
|
|
This is a pure ASP.NET question. You're most likely to get an answer there as that's a much more appropriate forum.
|
|
|
|
|
The SiteMap.CurrentNode property returns an object which is shared across all requests to your application. Your code is changing properties on the node, which will affect every subsequent request to your application.
The solution is simple: don't change the properties of the nodes in the site map.
Of course, you'll need to find another way to do what you're trying to achieve.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I have some C++ code which is based on WriteFile. To my understanding it uses this method to write: *both* to normal files on my filesystem and to USB also (HASP).
Basically this is the code in the constructor of the C++ class:
if(isOpen) return true ;
char PortNameUNC[256] ;
if(PortName[0] != '\\') strcpy(PortNameUNC, "\\\\.\\") ;
else *PortNameUNC = 0 ;
strcat(PortNameUNC, PortName) ;
*hDev = CreateFile(PortNameUNC, GENERIC_READ|GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(*hDev == INVALID_HANDLE_VALUE) return false ;
DCB *dcb = new DCB ;
memset(dcb, 0x00, sizeof(DCB)) ;
dcb->DCBlength = sizeof(DCB);
dcb->BaudRate = BaudRate;
dcb->Parity = Parity;
dcb->StopBits = StopBits;
dcb->ByteSize = ByteSize;
dcb->fBinary = TRUE;
dcb->fDsrSensitivity = 0;
dcb->fDtrControl = (DTR ? DTR_CONTROL_ENABLE : DTR_CONTROL_DISABLE) ;
dcb->fRtsControl = (RTS ? RTS_CONTROL_ENABLE : RTS_CONTROL_DISABLE) ;
dcb->fOutxCtsFlow = (CTS ? 1 : 0) ;
dcb->fOutxDsrFlow = (DSR ? 1 : 0) ;
dcb->fOutX = (XonnXoff ? 1 : 0) ;
dcb->fInX = 0 ;
if(!SetCommState(*hDev, dcb))
{
delete dcb ;
CloseHandle(*hDev) ;
*hDev = INVALID_HANDLE_VALUE ;
return false;
}
delete dcb ;
this->BaudRate = BaudRate;
this->Parity = Parity;
this->StopBits = StopBits;
this->ByteSize = ByteSize;
if(!SetTimeOut(readTimeOut, ReadIntervalTimeout) || !Reset())
{
CloseHandle(*hDev) ;
*hDev = INVALID_HANDLE_VALUE ;
return false;
}
isOpen = true ;
return true ;
And then there is method like write which simply calls `WriteFile` method - and passes the handle created in the constructor and data (I think this way it can write both to USB and my hard drive?)
Why is this `DCB` used additionally and also the `SetCommState` in the constructor?
What is the easiest way to mimic this behaviour in C#??? Do I need `SerialPort` class?
modified 28-Sep-15 9:32am.
|
|
|
|
|
The File[^] class has numerous methods that might suit you. As long as the USB device is mounted as a drive, you should easily be able to write to it using one of the Write... methods.
|
|
|
|
|
Dear Pete, I have changed my question a bit, and would very much appreciate if you can help! Thanks
|
|
|
|
|
"As long as the USB device is mounted as a drive,"
How do I ensure this happens?
|
|
|
|
|
That depends on what the USB device is?
USB is not a port. It is nothing like a serial or parallel port. It is a Bus, not unlike the expansion slots inside the machine. You have to talk to the device using whatever method it exposes to you. If it exposes itself as a serial device, you can use serial communication methods to talk to it. If it exposes itself as some kind of drive, you can use file methods.
|
|
|
|
|
How can I determine if it exposes as serial device or some kind of drive? It is HASP actually
(please see the updated question)
|
|
|
|